From e8a4ffe2f646e857a6bf048e41bdb3d477ecfb82 Mon Sep 17 00:00:00 2001 From: CyJaySong Date: Wed, 16 Dec 2020 16:15:03 +0800 Subject: [PATCH] add iocr support --- README.md | 2 ++ vision/ocr/default.go | 12 ++++++++++++ vision/ocr/ocr.go | 39 +++++++++++++++++++++++---------------- vision/ocr/param.go | 14 ++++++++++++++ 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index de4f438..d3a6b1c 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ - [x] 驾驶证识别 - [x] 行驶证识别 - [ ] 表格文字识别 +- [x] iOCR自定义模板文字识别 通用版 +- [x] iOCR自定义模板文字识别 财会版 #### 人脸识别 diff --git a/vision/ocr/default.go b/vision/ocr/default.go index 04dd65d..d7543d5 100644 --- a/vision/ocr/default.go +++ b/vision/ocr/default.go @@ -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,自定义模板或预置模板的唯一标示,可用于调用指定的识别模板进行结构化识别,可在「模板管理」页查看并复制使用 +} diff --git a/vision/ocr/ocr.go b/vision/ocr/ocr.go index f76fffd..52d96f2 100644 --- a/vision/ocr/ocr.go +++ b/vision/ocr/ocr.go @@ -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 通用文字识别 @@ -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) { @@ -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...) } @@ -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...) @@ -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") diff --git a/vision/ocr/param.go b/vision/ocr/param.go index 72b3ec0..66d4ac6 100644 --- a/vision/ocr/param.go +++ b/vision/ocr/param.go @@ -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 + } +}