Skip to content

Commit

Permalink
Merge pull request #19 from CyJaySong/master
Browse files Browse the repository at this point in the history
add iocr support
  • Loading branch information
cyjaysong committed Dec 16, 2020
2 parents cdd1234 + f7c8ecf commit 00f1044
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
- [x] 驾驶证识别
- [x] 行驶证识别
- [ ] 表格文字识别
- [x] iOCR自定义模板文字识别 通用版
- [x] iOCR自定义模板文字识别 财会版


#### 人脸识别
Expand Down
12 changes: 12 additions & 0 deletions vision/ocr/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ var defaultVATInvoiceParams = map[string]interface{}{
"accuracy": "normal", //normal(默认配置)对应普通精度模型,识别速度较快,在四要素的准确率上和 high 模型保持一致,high对应高精度识别模型,相应的时延会增加,因为超时导致失败的情况也会增加(错误码282000)
"type": "normal", //进行识别的增值税发票类型,默认为 normal,可缺省, - normal:可识别增值税普票、专票、电子发票, - roll:可识别增值税卷票
}

var defaultIocrRecogniseParams = map[string]interface{}{
"image": "", //图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式
"url": "", //图片存储的 BOS(百度云存储)url,暂仅支持BOS url,不支持其他图床,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式,当image字段存在时url字段失效,不支持https的图片链接
"templateSign": "", //模板 ID,自定义模板或预置模板的唯一标示,可用于调用指定的识别模板进行结构化识别,可在「模板管理」页查看并复制使用
"classifierId": "", //分类器Id,分类器的唯一标示,可用于调用指定的分类器对传入的图片进行自动分类及识别,与 templateSign 至少存在一个,如同时存在,则优先级 templateSign > classfierId
}

var defaultIocrRecogniseFinanceParams = map[string]interface{}{
"image": "", //图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式
"templateSign": "", //模板 ID,自定义模板或预置模板的唯一标示,可用于调用指定的识别模板进行结构化识别,可在「模板管理」页查看并复制使用
}
39 changes: 23 additions & 16 deletions vision/ocr/ocr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import (
)

const (
OCR_GENERAL_BASIC_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
OCR_ACCURATE_BASIC_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
OCR_GENERAL_WITH_LOCATION_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general"
OCR_GENERAL_ENHANCED_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_enhanced"
OCR_WEBIMAGE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage"
OCR_IDCARD_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"
OCR_BANKCARD_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard"
OCR_DRIVERLICENSE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license"
OCR_VEHICLELICENSE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license"
OCR_LICENSEPLATE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate"
OCR_FORM_URL = "https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/request"
OCR_VAT_INVOICE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice"
OCR_GENERAL_BASIC_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
OCR_ACCURATE_BASIC_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
OCR_GENERAL_WITH_LOCATION_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general"
OCR_GENERAL_ENHANCED_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_enhanced"
OCR_WEBIMAGE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage"
OCR_IDCARD_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"
OCR_BANKCARD_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard"
OCR_DRIVERLICENSE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license"
OCR_VEHICLELICENSE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license"
OCR_LICENSEPLATE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate"
OCR_FORM_URL = "https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/request"
OCR_VAT_INVOICE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice"
OCR_IOCR_RECOGNISE_URL = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise"
OCR_IOCR_RECOGNISE_FINANCE_URL = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise/finance"
)

//GeneralRecognizeBasic 通用文字识别
Expand All @@ -36,7 +38,6 @@ func (oc *OCRClient) AccurateRecognizeBasic(image *vision.Image, params ...Reque

}


//GeneralRecognizeWithLocation 通用文字识别(含位置信息)
//识别图片中的文字信息(包含文字区域的坐标信息)
func (oc *OCRClient) GeneralRecognizeWithLocation(image *vision.Image, params ...RequestParam) (*OCRResponse, error) {
Expand Down Expand Up @@ -112,7 +113,6 @@ func (oc *OCRClient) FormDataRecognize(image *vision.Image, params ...RequestPar

//VATInvoiceRecognize 增值税发票识别
func (oc *OCRClient) VATInvoiceRecognize(image *vision.Image, params ...RequestParam) (*OCRResponse, error) {

return oc.ocr(image, OCR_VAT_INVOICE_URL, defaultVATInvoiceParams, params...)

}
Expand All @@ -121,7 +121,15 @@ func (oc *OCRClient) VATInvoiceRecognize(image *vision.Image, params ...RequestP

//TODO:通用票据识别

//TODO:自定义模板文字识别
//IocrRecognise 自定义模板文字识别
func (oc *OCRClient) IocrRecognise(image *vision.Image, params ...RequestParam) (*OCRResponse, error) {
return oc.ocr(image, OCR_IOCR_RECOGNISE_URL, defaultIocrRecogniseParams, params...)
}

//IocrRecogniseFinance 自定义模板文字识别 财会版
func (oc *OCRClient) IocrRecogniseFinance(image *vision.Image, params ...RequestParam) (*OCRResponse, error) {
return oc.ocr(image, OCR_IOCR_RECOGNISE_FINANCE_URL, defaultIocrRecogniseFinanceParams, params...)
}

func (oc *OCRClient) ocr(image *vision.Image, url string, def map[string]interface{}, params ...RequestParam) (*OCRResponse, error) {
requestParams, err := parseRequestParam(image, def, params...)
Expand All @@ -133,7 +141,6 @@ func (oc *OCRClient) ocr(image *vision.Image, url string, def map[string]interfa
}

func parseRequestParam(image *vision.Image, def map[string]interface{}, params ...RequestParam) (map[string]interface{}, error) {

if image.Reader == nil {
if image.Url == "" {
return nil, errors.New("image source is empty")
Expand Down
14 changes: 14 additions & 0 deletions vision/ocr/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,17 @@ func MultiDetect() RequestParam {
m["multi_detect"] = true
}
}

//自定义模板文字识别 模板号
func TemplateSign(templateSign string) RequestParam {
return func(m map[string]interface{}) {
m["templateSign"] = templateSign
}
}

//自定义模板文字识别 分类器Id
func ClassifierId(classifierId int) RequestParam {
return func(m map[string]interface{}) {
m["classifierId"] = classifierId
}
}

0 comments on commit 00f1044

Please sign in to comment.