Skip to content

Commit

Permalink
Merge pull request #25 from GargantuaX/master
Browse files Browse the repository at this point in the history
OCR支持手写文字识别
  • Loading branch information
cyjaysong authored Jan 27, 2021
2 parents ae215ee + 6138c3b commit e20b539
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [x] 通用文字识别(含位置信息版)
- [x] 通用文字识别(高精度)
- [x] 通用文字识别(高精度含位置)
- [x] 手写文字识别
- [x] 网络图片文字识别
- [x] 身份证识别
- [x] 银行卡识别
Expand Down
13 changes: 13 additions & 0 deletions example/vision/ocr/ocr.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {
//AccurateRecognizeBasic()
//AccurateRecognize()
GeneralRecognizeBasic()
//HandWriting()
}

func GeneralRecognizeBasic() {
Expand All @@ -42,6 +43,18 @@ func GeneralRecognizeBasic() {
fmt.Println(rs.ToString())
}

func HandWriting() {
rs, err := client.HandWriting(
vision.MustFromFile("ocr_hand_writing.jpg"),
ocr.WithProbability(), //是否返回识别结果中每一行的置信度
)
if err != nil {
panic(err)
}

fmt.Println(rs.ToString())
}

func ConcurrentAccurateRecognizeBasic() {
images := []string{
"ocr_image_0.jpg",
Expand Down
Binary file added example/vision/ocr/ocr_hand_writing.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions vision/ocr/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ var defaultAccurateBasicParams = map[string]interface{}{
"probability": "false", //是否返回识别结果中每一行的置信度
}

var defaultAccurateRecognizeParams = map[string]interface{}{
"image": "", //图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
"detect_direction": "false", //是否检测图像朝向,默认不检测,即:false。朝向是指输入图像是正常方向、逆时针旋转90/180/270度。可选值包括: - true:检测朝向; - false:不检测朝向。
"probability": "false", //是否返回识别结果中每一行的置信度
}

var defaultHandWritingParams = map[string]interface{}{
"image": "", //图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
"recognize_granularity": "big", //是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置
"probability": "false", //是否返回识别结果中每一行的置信度
}

var defaultVATInvoiceParams = map[string]interface{}{
"image": "", //图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式
"accuracy": "normal", //normal(默认配置)对应普通精度模型,识别速度较快,在四要素的准确率上和 high 模型保持一致,high对应高精度识别模型,相应的时延会增加,因为超时导致失败的情况也会增加(错误码282000)
Expand Down
11 changes: 10 additions & 1 deletion vision/ocr/ocr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
OCR_GENERAL_ENHANCED_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_enhanced"
OCR_GENERAL_WITH_LOCATION_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general"
OCR_ACCURATE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate"
OCR_HANDWRITING_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting"
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"
Expand Down Expand Up @@ -66,7 +67,15 @@ func (oc *OCRClient) AccurateRecognizeBasic(image *vision.Image, params ...Reque
//识别图片中的文字信息
func (oc *OCRClient) AccurateRecognize(image *vision.Image, params ...RequestParam) (*OCRResponse, error) {

return oc.ocr(image, OCR_ACCURATE_URL, defaultAccurateBasicParams, params...)
return oc.ocr(image, OCR_ACCURATE_URL, defaultAccurateRecognizeParams, params...)

}

//HandWriting 手写体文字识别
//识别图片中的手写文字信息 ref: https://ai.baidu.com/ai-doc/OCR/hk3h7y2qq
func (oc *OCRClient) HandWriting(image *vision.Image, params ...RequestParam) (*OCRResponse, error) {

return oc.ocr(image, OCR_HANDWRITING_URL, defaultHandWritingParams, params...)

}

Expand Down

0 comments on commit e20b539

Please sign in to comment.