Call Ali API to realize image verification code recognition

1. About the author

Wang Kexin, female, School of Electronic Information, Xi'an Polytechnic University, 2021 graduate student
Research direction: Pattern Recognition and Artificial Intelligence
Email: [email protected]

Meng Liping, female, School of Electronic Information, Xi'an Polytechnic University, 2021 graduate student, Zhang Hongwei's artificial intelligence research group
Research direction: machine vision and artificial intelligence
Email: [email protected]

2. Introduction to Alibaba Cloud OCR Algorithm

2.1 Introduction to OCR

OCR is the recognition and understanding of image text; the early stage of OCR is to recognize the text in the picture, that is, to find the position of the text, and then identify what word it is. In the later stage, text comprehension is carried out on the basis of the original text recognition. Text comprehension is an application of text recognition. OCR outputs the results the user wants based on the understanding of spatial relationships and semantics.

2.2 Introduction to OCR Algorithm Based on Alibaba Cloud

Alibaba Cloud Visual Intelligence Open Platform (vision.aliyun.com) is a comprehensive visual AI capability platform that provides visual intelligence API services based on Alibaba's practical experience in visual intelligence technology to help enterprises and developers quickly build the application capabilities of visual intelligence technology. Based on Alibaba Cloud's OCR algorithm (AI technology), it can quickly and accurately recognize irregular text in various network pictures, support handwritten text recognition, and maintain a very high recognition accuracy in the case of complex fonts and complex backgrounds.
Application scenarios: air tickets, verification code recognition, posters, entity advertising recognition, micro-business advertising, Baidu Tieba, Taobao graphic recognition, concert tickets, etc.
3. Call Ali API to realize image verification code recognition

3.1 Experimental process

Go to Alibaba Cloud's official website (https://www.aliyun.com/) to register an account and log in
insert image description here

Click "Cloud Market" → "API Market" → "Artificial Intelligence"
insert image description here

Find the product: "General Text Recognition - High Definition OCR Text Recognition"
insert image description here

Register and activate the text recognition free quota
insert image description here

Find "Cloud Marketplace" in "Console" - "Products & Services"
insert image description here

In the purchased universal text recognition service, find the AppCode, which needs to be pasted in the code later
insert image description here

In addition, we can query the interface address, request method, and verification method through the interface file
insert image description here

Next, a dataset is established, and the selected datasets are relatively simple captcha images. All local images are saved here, and the image format is .png
insert image description here

3.2 Experimental code

Use python to API of Alibaba Cloud's image recognition algorithm to recognize the text in the verification code. Image data can use base64 encoding and url parameters (only one format can be used at the same time). The size of the base64 encoding is required to be no more than 4M, and the smaller the image, the more efficient The higher it is, the jpg/png/bmp format is supported.

#python3
#导入需要的库
import urllib.request
import urllib.parse
import json
import time
import base64
 #加载本地图片
image_path = '输入图片地址'
with open(image_path, 'rb') as f:  # 以二进制读取本地图片
    data = f.read()
    encodestr = str(base64.b64encode(data),'utf-8') # base64编码图片
#设置请求头
headers={
    
    'Authorization':'APPCODE 申请到的个人APPCODE',  # 填入个人APPCODE
 'Content-Type': 'application/json; charset=UTF-8'# 定义接口格式
    }
def posturl(url,data={
    
    }):
    try:
        params=json.dumps(dict).encode(encoding='UTF-8')
        req = urllib.request.Request(url, params, headers)
        r = urllib.request.urlopen(req)
        html =r.read()
        r.close();
        return html.decode("utf8")
    except urllib.error.HTTPError as e:
        print(e.code)
        print(e.read().decode("utf8"))
    time.sleep(1)
if __name__=="__main__":
url_request="https://gjbsb.market.alicloudapi.com/ocrservice/advanced"   # 对应官网APIURL
    dict = {
    
    'img': encodestr}
    html = posturl(url_request, data=dict)
    jos = json.loads(html.encode("utf-8").decode("utf-8"))    # str转json对象,<class 'dict'>
    result = jos['content']  # 获得参数结果
    print('识别的结果:',result)

3.3 Experimental results

After the program runs successfully, the verification code can basically be correctly recognized, and the recognition result is shown in the following figure
insert image description here
insert image description here

From the analysis of the experimental results, it can be seen that the image recognition accuracy rate is high, but there are slight errors, and the created data set samples are few. This experiment can quickly and accurately identify irregular text in various network images, and supports handwritten text recognition. Remains very high recognition accuracy with complex fonts and complex backgrounds

Guess you like

Origin blog.csdn.net/m0_37758063/article/details/123643861