forked from shenghui0779/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocr.go
500 lines (414 loc) · 13.8 KB
/
ocr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package offia
import (
"encoding/json"
"io"
"os"
"path/filepath"
"github.com/liniu/gochat/urls"
"github.com/liniu/gochat/wx"
)
// OCRMode 识别模式
type OCRMode string
// 微信支持的识别模式
var (
OCRPhoto OCRMode = "photo" // 拍照模式
OCRScan OCRMode = "scan" // 扫描模式
)
// OCRPosition 识别位置
type OCRPosition struct {
Pos ImagePosition `json:"pos"`
}
// ResultIDCardFrontOCR 身份证前面识别结果
type ResultIDCardFrontOCR struct {
Name string `json:"name"`
ID string `json:"id"`
Addr string `json:"addr"`
Gender string `json:"gender"`
Nationality string `json:"nationality"`
}
// OCRIDCardFront 智能接口 - 身份证前面识别
func OCRIDCardFront(mode OCRMode, imgPath string, result *ResultIDCardFrontOCR) wx.Action {
_, filename := filepath.Split(imgPath)
return wx.NewPostAction(urls.OffiaOCRIDCard,
wx.WithQuery("type", string(mode)),
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(imgPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("img", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// OCRIDCardFrontByURL 智能接口 - 身份证前面识别
func OCRIDCardFrontByURL(mode OCRMode, imgURL string, result *ResultIDCardFrontOCR) wx.Action {
return wx.NewPostAction(urls.OffiaOCRIDCard,
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// ResultIDCardBackOCR 身份证背面识别结果
type ResultIDCardBackOCR struct {
ValidDate string `json:"valid_date"`
}
// OCRIDCardBack 智能接口 - 身份证背面识别
func OCRIDCardBack(mode OCRMode, imgPath string, result *ResultIDCardBackOCR) wx.Action {
_, filename := filepath.Split(imgPath)
return wx.NewPostAction(urls.OffiaOCRIDCard,
wx.WithQuery("type", string(mode)),
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(imgPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("img", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// OCRIDCardBackByURL 智能接口 - 身份证背面识别
func OCRIDCardBackByURL(mode OCRMode, imgURL string, result *ResultIDCardBackOCR) wx.Action {
return wx.NewPostAction(urls.OffiaOCRIDCard,
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// ResultBankCardOCR 银行卡识别结果
type ResultBankCardOCR struct {
Number string `json:"number"`
}
// OCRBankCard 智能接口 - 银行卡识别
func OCRBankCard(mode OCRMode, imgPath string, result *ResultBankCardOCR) wx.Action {
_, filename := filepath.Split(imgPath)
return wx.NewPostAction(urls.OffiaOCRBankCard,
wx.WithQuery("type", string(mode)),
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(imgPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("img", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// OCRBankCardByURL 智能接口 - 银行卡识别
func OCRBankCardByURL(mode OCRMode, imgURL string, result *ResultBankCardOCR) wx.Action {
return wx.NewPostAction(urls.OffiaOCRBankCard,
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// ResultPlateNumberOCR 车牌号识别结果
type ResultPlateNumberOCR struct {
Number string `json:"number"`
}
// OCRPlateNumber 智能接口 - 车牌号识别
func OCRPlateNumber(mode OCRMode, imgPath string, result *ResultPlateNumberOCR) wx.Action {
_, filename := filepath.Split(imgPath)
return wx.NewPostAction(urls.OffiaOCRPlateNumber,
wx.WithQuery("type", string(mode)),
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(imgPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("img", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// OCRPlateNumberByURL 智能接口 - 车牌号识别
func OCRPlateNumberByURL(mode OCRMode, imgURL string, result *ResultPlateNumberOCR) wx.Action {
return wx.NewPostAction(urls.OffiaOCRPlateNumber,
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// ResultDriverLicenseOCR 驾照识别结果
type ResultDriverLicenseOCR struct {
IDNum string `json:"id_num"` // 证号
Name string `json:"name"` // 姓名
Sex string `json:"sex"` // 性别
Nationality string `json:"nationality"` // 国籍
Address string `json:"address"` // 住址
BirthDate string `json:"birth_date"` // 出生日期
IssueDate string `json:"issue_date"` // 初次领证日期
CarClass string `json:"car_class"` // 准驾车型
ValidFrom string `json:"valid_from"` // 有效期限起始日
ValidTo string `json:"valid_to"` // 有效期限终止日
OfficialSeal string `json:"official_seal"` // 印章文字
}
// OCRDriverLicense 智能接口 - 驾照识别
func OCRDriverLicense(mode OCRMode, imgPath string, result *ResultDriverLicenseOCR) wx.Action {
_, filename := filepath.Split(imgPath)
return wx.NewPostAction(urls.OffiaOCRDriverLicense,
wx.WithQuery("type", string(mode)),
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(imgPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("img", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// OCRDriverLicenseByURL 智能接口 - 驾照识别
func OCRDriverLicenseByURL(mode OCRMode, imgURL string, result *ResultDriverLicenseOCR) wx.Action {
return wx.NewPostAction(urls.OffiaOCRDriverLicense,
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// ResultVehicleLicenseOCR 行驶证识别结果
type ResultVehicleLicenseOCR struct {
VehicleType string `json:"vhicle_type"` // 车辆类型
Owner string `json:"owner"` // 所有人
Addr string `json:"addr"` // 住址
UseCharacter string `json:"use_character"` // 使用性质
Model string `json:"model"` // 品牌型号
VIN string `json:"vin"` // 车辆识别代号
EngineNum string `json:"engine_num"` // 发动机号码
RegisterDate string `json:"register_date"` // 注册日期
IssueDate string `json:"issue_date"` // 发证日期
PlateNum string `json:"plate_num"` // 车牌号码
PlateNumB string `json:"plate_num_b"` // 车牌号码
Record string `json:"record"` // 号牌
PassengersNum string `json:"passengers_num"` // 核定载人数
TotalQuality string `json:"total_quality"` // 总质量
PrepareQuality string `json:"prepare_quality"` // 整备质量
OverallSize string `json:"overall_size"` // 外廓尺寸
CardPositionFront OCRPosition `json:"card_position_front"` // 卡片正面位置(检测到卡片正面才会返回)
CardPositionBack OCRPosition `json:"card_position_back"` // 卡片反面位置(检测到卡片反面才会返回)
}
// OCRVehicleLicense 智能接口 - 行驶证识别
func OCRVehicleLicense(mode OCRMode, imgPath string, result *ResultVehicleLicenseOCR) wx.Action {
_, filename := filepath.Split(imgPath)
return wx.NewPostAction(urls.OffiaOCRVehicleLicense,
wx.WithQuery("type", string(mode)),
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(imgPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("img", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// OCRVehicleLicenseByURL 智能接口 - 行驶证识别
func OCRVehicleLicenseByURL(mode OCRMode, imgURL string, result *ResultVehicleLicenseOCR) wx.Action {
return wx.NewPostAction(urls.OffiaOCRVehicleLicense,
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// ResultBusinessLicenseOCR 营业执照
type ResultBusinessLicenseOCR struct {
RegNum string `json:"reg_num"` // 注册号
Serial string `json:"serial"` // 编号
LegalRepresentative string `json:"legal_representative"` // 法定代表人姓名
EnterpriseName string `json:"enterprise_name"` // 企业名称
TypeOfOrganization string `json:"type_of_organization"` // 组成形式
Address string `json:"address"` // 经营场所/企业住所
TypeOfEnterprise string `json:"type_of_enterprise"` // 公司类型
BusinessScope string `json:"business_scope"` // 经营范围
RegisteredCapital string `json:"registered_capital"` // 注册资本
PaidInCapital string `json:"paid_in_capital"` // 实收资本
ValidPeriod string `json:"valid_period"` // 营业期限
RegisteredDate string `json:"registered_date"` // 注册日期/成立日期
CertPosition OCRPosition `json:"cert_position"` // 营业执照位置
ImgSize ImageSize `json:"img_size"` // 图片大小
}
// OCRBusinessLicense 智能接口 - 营业执照识别
func OCRBusinessLicense(mode OCRMode, imgPath string, result *ResultBusinessLicenseOCR) wx.Action {
_, filename := filepath.Split(imgPath)
return wx.NewPostAction(urls.OffiaOCRBusinessLicense,
wx.WithQuery("type", string(mode)),
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(imgPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("img", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// OCRBusinessLicenseByURL 智能接口 - 营业执照识别
func OCRBusinessLicenseByURL(mode OCRMode, imgURL string, result *ResultBusinessLicenseOCR) wx.Action {
return wx.NewPostAction(urls.OffiaOCRBusinessLicense,
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// CommOCRItem 通用印刷体内容项
type CommOCRItem struct {
Text string `json:"text"`
Pos ImagePosition `json:"pos"`
}
// ResultCommOCR 通用印刷体识别结果
type ResultCommOCR struct {
Items []*CommOCRItem `json:"items"`
ImgSize ImageSize `json:"img_size"`
}
// OCRComm 智能接口 - 通用印刷体识别
func OCRComm(mode OCRMode, imgPath string, result *ResultCommOCR) wx.Action {
_, filename := filepath.Split(imgPath)
return wx.NewPostAction(urls.OffiaOCRComm,
wx.WithQuery("type", string(mode)),
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(imgPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("img", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// OCRCommByURL 智能接口 - 通用印刷体识别
func OCRCommByURL(mode OCRMode, imgURL string, result *ResultCommOCR) wx.Action {
return wx.NewPostAction(urls.OffiaOCRComm,
wx.WithQuery("type", string(mode)),
wx.WithQuery("img_url", imgURL),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}