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 Exception; |
12
|
|
|
use icy2003\php\I; |
13
|
|
|
use icy2003\php\icomponents\file\LocalFile; |
14
|
|
|
use icy2003\php\ihelpers\Arrays; |
15
|
|
|
use icy2003\php\ihelpers\Base64; |
16
|
|
|
use icy2003\php\ihelpers\Http; |
17
|
|
|
use icy2003\php\ihelpers\Json; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* OCR |
21
|
|
|
* |
22
|
|
|
* @link https://ai.baidu.com/docs#/OCR-API/top |
23
|
|
|
*/ |
24
|
|
|
class Ocr extends Base |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* 选项列表 |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $_options = [ |
33
|
|
|
'image' => null, |
34
|
|
|
'language_type' => 'CHN_ENG', |
35
|
|
|
'detect_direction' => false, |
36
|
|
|
'detect_language' => false, |
37
|
|
|
'probability' => false, |
38
|
|
|
'recognize_granularity' => 'big', |
39
|
|
|
'vertexes_location' => false, |
40
|
|
|
'words_type' => null, |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* 加载一个图片 |
45
|
|
|
* |
46
|
|
|
* - base64:图像数据,大小不超过4M,最短边至少15px,最长边最大4096px,支持jjpg/jpeg/png/bmp格式 |
47
|
|
|
* - 文件 URL:图片完整URL,URL长度不超过1024字节,对应的 base64 数据限制如上,不支持https的图片链接 |
48
|
|
|
* |
49
|
|
|
* @param string $image |
50
|
|
|
* |
51
|
|
|
* @return static |
52
|
|
|
*/ |
53
|
|
|
public function image($image) |
54
|
|
|
{ |
55
|
|
|
if (Base64::isBase64($image)) { |
56
|
|
|
$this->_options['image'] = $image; |
57
|
|
|
} elseif ((new LocalFile())->isFile($image)) { |
58
|
|
|
$this->_options['image'] = Base64::fromFile($image); |
59
|
|
|
} else { |
60
|
|
|
throw new Exception("错误的图片类型"); |
61
|
|
|
} |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* 设置选项 |
67
|
|
|
* |
68
|
|
|
* @param array $options |
69
|
|
|
* - image:图片 base64 |
70
|
|
|
* - language_type:识别语言类型,默认为CHN_ENG。可选值包括:CHN_ENG:中英文混合;ENG:英文;POR:葡萄牙语;FRE:法语;GER:德语;ITA:意大利语;SPA:西班牙语;RUS:俄语;JAP:日语;KOR:韩语 |
71
|
|
|
* - detect_direction:是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:true,检测朝向;false,不检测朝向 |
72
|
|
|
* - detect_language:是否检测语言,默认不检测。当前支持(中文、英语、日语、韩语) |
73
|
|
|
* - probability:是否返回识别结果中每一行的置信度 |
74
|
|
|
* - recognize_granularity:是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置 |
75
|
|
|
* - vertexes_location:是否返回文字外接多边形顶点位置,不支持单字位置。默认为false |
76
|
|
|
* |
77
|
|
|
* @return static |
78
|
|
|
*/ |
79
|
|
|
public function setOptions($options) |
80
|
|
|
{ |
81
|
|
|
$this->_options = Arrays::merge($this->_options, $options); |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* 通用文字识别 |
87
|
|
|
* |
88
|
|
|
* 用户向服务请求识别某张图中的所有文字 |
89
|
|
|
* - setOptions():image、language_type、detect_direction、detect_language、probability |
90
|
|
|
* |
91
|
|
|
* @return static |
92
|
|
|
*/ |
93
|
|
|
public function generalBasic() |
94
|
|
|
{ |
95
|
|
|
$this->requestToken(); |
96
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic', Arrays::some($this->_options, [ |
97
|
|
|
'image', |
98
|
|
|
'language_type', |
99
|
|
|
'detect_direction', |
100
|
|
|
'detect_language', |
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
|
|
|
* - setOptions():image、detect_direction、probability |
117
|
|
|
* |
118
|
|
|
* @param array $options |
119
|
|
|
* @return static |
120
|
|
|
*/ |
121
|
|
|
public function accurateBasic() |
122
|
|
|
{ |
123
|
|
|
$this->requestToken(); |
124
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic', Arrays::some($this->_options, [ |
125
|
|
|
'image', |
126
|
|
|
'detect_direction', |
127
|
|
|
'probability', |
128
|
|
|
]), [ |
129
|
|
|
'access_token' => $this->_token, |
130
|
|
|
])); |
131
|
|
|
$this->_toArrayCall = function ($result) { |
132
|
|
|
return Arrays::column(I::get($result, 'words_result'), 'words'); |
|
|
|
|
133
|
|
|
}; |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* 通用文字识别(含位置信息版) |
140
|
|
|
* |
141
|
|
|
* 用户向服务请求识别某张图中的所有文字,并返回文字在图中的位置信息 |
142
|
|
|
* |
143
|
|
|
* @param array $options |
144
|
|
|
* - setOptions():image、recognize_granularity、language_type、detect_direction、detect_language、vertexes_location、probability |
145
|
|
|
* @return static |
146
|
|
|
*/ |
147
|
|
|
public function general() |
148
|
|
|
{ |
149
|
|
|
$this->requestToken(); |
150
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/rest/2.0/ocr/v1/general', Arrays::some($this->_options, [ |
151
|
|
|
'image', |
152
|
|
|
'recognize_granularity', |
153
|
|
|
'language_type', |
154
|
|
|
'detect_direction', |
155
|
|
|
'detect_language', |
156
|
|
|
'vertexes_location', |
157
|
|
|
'probability', |
158
|
|
|
]), [ |
159
|
|
|
'access_token' => $this->_token, |
160
|
|
|
])); |
161
|
|
|
$this->_toArrayCall = function ($result) { |
162
|
|
|
return I::get($result, 'words_result'); |
163
|
|
|
}; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* 通用文字识别(高精度含位置版) |
170
|
|
|
* |
171
|
|
|
* 用户向服务请求识别某张图中的所有文字,并返回文字在图片中的坐标信息,相对于通用文字识别(含位置信息版)该产品精度更高,但是识别耗时会稍长 |
172
|
|
|
* |
173
|
|
|
* @param array $options |
174
|
|
|
* - setOptions():image、recognize_granularity、detect_direction、vertexes_location、probability |
175
|
|
|
* @return static |
176
|
|
|
*/ |
177
|
|
|
public function accurate() |
178
|
|
|
{ |
179
|
|
|
$this->requestToken(); |
180
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/rest/2.0/ocr/v1/accurate', Arrays::some($this->_options, [ |
181
|
|
|
'image', |
182
|
|
|
'recognize_granularity', |
183
|
|
|
'detect_direction', |
184
|
|
|
'vertexes_location', |
185
|
|
|
'probability', |
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
|
|
|
* - setOptions():image、recognize_granularity、words_type |
203
|
|
|
* @return static |
204
|
|
|
*/ |
205
|
|
|
public function handwriting() |
206
|
|
|
{ |
207
|
|
|
$this->requestToken(); |
208
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting', Arrays::some($this->_options, [ |
209
|
|
|
'image', |
210
|
|
|
'recognize_granularity', |
211
|
|
|
'words_type', |
212
|
|
|
]), [ |
213
|
|
|
'access_token' => $this->_token, |
214
|
|
|
])); |
215
|
|
|
$this->_toArrayCall = function ($result) { |
216
|
|
|
return I::get($result, 'words_result'); |
217
|
|
|
}; |
218
|
|
|
|
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* 身份证识别 |
224
|
|
|
* |
225
|
|
|
* 支持对大陆居民二代身份证正反面的所有字段进行结构化识别,包括姓名、性别、民族、出生日期、住址、身份证号、签发机关、有效期限;同时,支持对用户上传的身份证图片进行图像风险和质量检测,可识别图片是否为复印件或临时身份证,是否被翻拍或编辑,是否存在正反颠倒、模糊、欠曝、过曝等质量问题 |
226
|
|
|
* |
227
|
|
|
* @param array $options |
228
|
|
|
* - id_card_side:front:身份证含照片的一面;back:身份证带国徽的一面 |
229
|
|
|
* - detect_direction:是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括:true,检测朝向;false,不检测朝向 |
230
|
|
|
* - detect_risk:是否开启身份证风险类型(身份证复印件、临时身份证、身份证翻拍、修改过的身份证)功能,默认不开启,即:false。可选值:true-开启;false-不开启 |
231
|
|
|
* @return static |
232
|
|
|
*/ |
233
|
|
|
public function idcard($options = [ |
234
|
|
|
'id_card_side' => 'front', |
235
|
|
|
'detect_direction' => false, |
236
|
|
|
'detect_risk' => false, |
237
|
|
|
]) { |
238
|
|
|
$options = Arrays::merge($this->_options, $options); |
239
|
|
|
$this->requestToken(); |
240
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/rest/2.0/ocr/v1/idcard', $options, [ |
241
|
|
|
'access_token' => $this->_token, |
242
|
|
|
])); |
243
|
|
|
$this->_toArrayCall = function ($result) { |
244
|
|
|
$return = []; |
245
|
|
|
$words = I::get($result, 'words_result', []); |
246
|
|
|
foreach ($words as $name => $word) { |
247
|
|
|
$return[$name] = I::get($word, 'words'); |
248
|
|
|
} |
249
|
|
|
return $return; |
250
|
|
|
}; |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* 银行卡识别 |
257
|
|
|
* |
258
|
|
|
* 识别银行卡并返回卡号、有效期、发卡行和卡片类型 |
259
|
|
|
* |
260
|
|
|
* @return static |
261
|
|
|
*/ |
262
|
|
|
public function bankcard() |
263
|
|
|
{ |
264
|
|
|
$options = $this->_options; |
265
|
|
|
$this->requestToken(); |
266
|
|
|
$this->_result = Json::decode(Http::post('https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard', $options, [ |
267
|
|
|
'access_token' => $this->_token, |
268
|
|
|
])); |
269
|
|
|
$this->_toArrayCall = function ($result) { |
270
|
|
|
return I::get($result, 'result'); |
271
|
|
|
}; |
272
|
|
|
return $this; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
} |
276
|
|
|
|