|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class OCR |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://www.icy2003.com/ |
|
6
|
|
|
* @author icy2003 <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2017, icy2003 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace icy2003\php\iapis\baidu; |
|
10
|
|
|
|
|
11
|
|
|
use icy2003\php\I; |
|
12
|
|
|
use icy2003\php\ihelpers\Arrays; |
|
13
|
|
|
use icy2003\php\ihelpers\Http; |
|
14
|
|
|
use icy2003\php\ihelpers\Json; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* 图像识别 |
|
18
|
|
|
* |
|
19
|
|
|
* @link https://ai.baidu.com/docs#/OCR-API/top |
|
20
|
|
|
*/ |
|
21
|
|
|
class OCR extends Base |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* 选项列表 |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $_options = [ |
|
30
|
|
|
'image' => null, |
|
31
|
|
|
'language_type' => 'CHN_ENG', |
|
32
|
|
|
'detect_direction' => false, |
|
33
|
|
|
'detect_language' => false, |
|
34
|
|
|
'probability' => false, |
|
35
|
|
|
'recognize_granularity' => 'big', |
|
36
|
|
|
'vertexes_location' => false, |
|
37
|
|
|
'words_type' => null, |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* 设置选项 |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $options |
|
44
|
|
|
* - image:图片 base64 |
|
45
|
|
|
* - language_type:识别语言类型,默认为CHN_ENG。可选值包括:CHN_ENG:中英文混合;ENG:英文;POR:葡萄牙语;FRE:法语;GER:德语;ITA:意大利语;SPA:西班牙语;RUS:俄语;JAP:日语;KOR:韩语 |
|
46
|
|
|
* - detect_direction:是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:true,检测朝向;false,不检测朝向 |
|
47
|
|
|
* - detect_language:是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语) |
|
48
|
|
|
* - probability:是否返回识别结果中每一行的置信度 |
|
49
|
|
|
* - recognize_granularity:是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置 |
|
50
|
|
|
* - vertexes_location:是否返回文字外接多边形顶点位置,不支持单字位置。默认为false |
|
51
|
|
|
* |
|
52
|
|
|
* @return static |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setOptions($options) |
|
55
|
|
|
{ |
|
56
|
|
|
return parent::setOptions($options); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* 通用文字识别 |
|
61
|
|
|
* |
|
62
|
|
|
* 用户向服务请求识别某张图中的所有文字 |
|
63
|
|
|
* - setOptions():image、language_type、detect_direction、detect_language、probability |
|
64
|
|
|
* |
|
65
|
|
|
* @return static |
|
66
|
|
|
*/ |
|
67
|
|
|
public function generalBasic() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->requestToken(); |
|
70
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic', Arrays::some($this->_options, [ |
|
71
|
|
|
'image', |
|
72
|
|
|
'language_type', |
|
73
|
|
|
'detect_direction', |
|
74
|
|
|
'detect_language', |
|
75
|
|
|
'probability', |
|
76
|
|
|
]), [ |
|
77
|
|
|
'access_token' => $this->_token, |
|
78
|
|
|
])); |
|
79
|
|
|
$this->_toArrayCall = function ($result) { |
|
80
|
|
|
return Arrays::column(I::get($result, 'words_result'), 'words'); |
|
|
|
|
|
|
81
|
|
|
}; |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* 通用文字识别(高精度版) |
|
88
|
|
|
* |
|
89
|
|
|
* 用户向服务请求识别某张图中的所有文字,相对于通用文字识别该产品精度更高,但是识别耗时会稍长 |
|
90
|
|
|
* - setOptions():image、detect_direction、probability |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $options |
|
93
|
|
|
* @return static |
|
94
|
|
|
*/ |
|
95
|
|
|
public function accurateBasic() |
|
96
|
|
|
{ |
|
97
|
|
|
$this->requestToken(); |
|
98
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic', Arrays::some($this->_options, [ |
|
99
|
|
|
'image', |
|
100
|
|
|
'detect_direction', |
|
101
|
|
|
'probability', |
|
102
|
|
|
]), [ |
|
103
|
|
|
'access_token' => $this->_token, |
|
104
|
|
|
])); |
|
105
|
|
|
$this->_toArrayCall = function ($result) { |
|
106
|
|
|
return Arrays::column(I::get($result, 'words_result'), 'words'); |
|
|
|
|
|
|
107
|
|
|
}; |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* 通用文字识别(含位置信息版) |
|
114
|
|
|
* |
|
115
|
|
|
* 用户向服务请求识别某张图中的所有文字,并返回文字在图中的位置信息 |
|
116
|
|
|
* |
|
117
|
|
|
* @param array $options |
|
118
|
|
|
* - setOptions():image、recognize_granularity、language_type、detect_direction、detect_language、vertexes_location、probability |
|
119
|
|
|
* @return static |
|
120
|
|
|
*/ |
|
121
|
|
|
public function general() |
|
122
|
|
|
{ |
|
123
|
|
|
$this->requestToken(); |
|
124
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/rest/2.0/ocr/v1/general', Arrays::some($this->_options, [ |
|
125
|
|
|
'image', |
|
126
|
|
|
'recognize_granularity', |
|
127
|
|
|
'language_type', |
|
128
|
|
|
'detect_direction', |
|
129
|
|
|
'detect_language', |
|
130
|
|
|
'vertexes_location', |
|
131
|
|
|
'probability', |
|
132
|
|
|
]), [ |
|
133
|
|
|
'access_token' => $this->_token, |
|
134
|
|
|
])); |
|
135
|
|
|
$this->_toArrayCall = function ($result) { |
|
136
|
|
|
return I::get($result, 'words_result'); |
|
137
|
|
|
}; |
|
138
|
|
|
|
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* 通用文字识别(高精度含位置版) |
|
144
|
|
|
* |
|
145
|
|
|
* 用户向服务请求识别某张图中的所有文字,并返回文字在图片中的坐标信息,相对于通用文字识别(含位置信息版)该产品精度更高,但是识别耗时会稍长 |
|
146
|
|
|
* |
|
147
|
|
|
* @param array $options |
|
148
|
|
|
* - setOptions():image、recognize_granularity、detect_direction、vertexes_location、probability |
|
149
|
|
|
* @return static |
|
150
|
|
|
*/ |
|
151
|
|
|
public function accurate() |
|
152
|
|
|
{ |
|
153
|
|
|
$this->requestToken(); |
|
154
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/rest/2.0/ocr/v1/accurate', Arrays::some($this->_options, [ |
|
155
|
|
|
'image', |
|
156
|
|
|
'recognize_granularity', |
|
157
|
|
|
'detect_direction', |
|
158
|
|
|
'vertexes_location', |
|
159
|
|
|
'probability', |
|
160
|
|
|
]), [ |
|
161
|
|
|
'access_token' => $this->_token, |
|
162
|
|
|
])); |
|
163
|
|
|
$this->_toArrayCall = function ($result) { |
|
164
|
|
|
return I::get($result, 'words_result'); |
|
165
|
|
|
}; |
|
166
|
|
|
|
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* 手写文字识别 |
|
172
|
|
|
* |
|
173
|
|
|
* 对手写中文汉字、数字进行识别 |
|
174
|
|
|
* |
|
175
|
|
|
* @param array $options |
|
176
|
|
|
* - setOptions():image、recognize_granularity、words_type |
|
177
|
|
|
* @return static |
|
178
|
|
|
*/ |
|
179
|
|
|
public function handwriting() |
|
180
|
|
|
{ |
|
181
|
|
|
$this->requestToken(); |
|
182
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting', Arrays::some($this->_options, [ |
|
183
|
|
|
'image', |
|
184
|
|
|
'recognize_granularity', |
|
185
|
|
|
'words_type', |
|
186
|
|
|
]), [ |
|
187
|
|
|
'access_token' => $this->_token, |
|
188
|
|
|
])); |
|
189
|
|
|
$this->_toArrayCall = function ($result) { |
|
190
|
|
|
return I::get($result, 'words_result'); |
|
191
|
|
|
}; |
|
192
|
|
|
|
|
193
|
|
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* 身份证识别 |
|
198
|
|
|
* |
|
199
|
|
|
* 支持对大陆居民二代身份证正反面的所有字段进行结构化识别,包括姓名、性别、民族、出生日期、住址、身份证号、签发机关、有效期限;同时,支持对用户上传的身份证图片进行图像风险和质量检测,可识别图片是否为复印件或临时身份证,是否被翻拍或编辑,是否存在正反颠倒、模糊、欠曝、过曝等质量问题 |
|
200
|
|
|
* |
|
201
|
|
|
* @param array $options |
|
202
|
|
|
* - id_card_side:front:身份证含照片的一面;back:身份证带国徽的一面 |
|
203
|
|
|
* - detect_direction:是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:true,检测朝向;false,不检测朝向 |
|
204
|
|
|
* - detect_risk:是否开启身份证风险类型(身份证复印件、临时身份证、身份证翻拍、修改过的身份证)功能,默认不开启,即:false。可选值:true-开启;false-不开启 |
|
205
|
|
|
* @return static |
|
206
|
|
|
*/ |
|
207
|
|
|
public function idcard($options = [ |
|
208
|
|
|
'id_card_side' => 'front', |
|
209
|
|
|
'detect_direction' => false, |
|
210
|
|
|
'detect_risk' => false, |
|
211
|
|
|
]) { |
|
212
|
|
|
$options = Arrays::merge($this->_options, $options); |
|
213
|
|
|
$this->requestToken(); |
|
214
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/rest/2.0/ocr/v1/idcard', $options, [ |
|
215
|
|
|
'access_token' => $this->_token, |
|
216
|
|
|
])); |
|
217
|
|
|
$this->_toArrayCall = function ($result) { |
|
218
|
|
|
$return = []; |
|
219
|
|
|
$words = I::get($result, 'words_result', []); |
|
220
|
|
|
foreach ($words as $name => $word) { |
|
221
|
|
|
$return[$name] = I::get($word, 'words'); |
|
222
|
|
|
} |
|
223
|
|
|
return $return; |
|
224
|
|
|
}; |
|
225
|
|
|
|
|
226
|
|
|
return $this; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* 银行卡识别 |
|
231
|
|
|
* |
|
232
|
|
|
* 识别银行卡并返回卡号、有效期、发卡行和卡片类型 |
|
233
|
|
|
* |
|
234
|
|
|
* @return static |
|
235
|
|
|
*/ |
|
236
|
|
|
public function bankcard() |
|
237
|
|
|
{ |
|
238
|
|
|
$options = $this->_options; |
|
239
|
|
|
$this->requestToken(); |
|
240
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard', $options, [ |
|
241
|
|
|
'access_token' => $this->_token, |
|
242
|
|
|
])); |
|
243
|
|
|
$this->_toArrayCall = function ($result) { |
|
244
|
|
|
return I::get($result, 'result'); |
|
245
|
|
|
}; |
|
246
|
|
|
return $this; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
} |
|
250
|
|
|
|