Python calls the volcano engine - text recognition function to recognize the text of the picture

First you need to go to the official website to download the code library

Volcengine official code warehouse see volc-sdk-python, GitHub - volcengine/volc-sdk-python

Here are the libraries we need to call, which can be directly downloaded and placed locally

Download the volcengine file directly and put it locally

 

Then there is our code section

First, we need to convert the image we need for text recognition to base64 encoding

Then we need the AK and SK of our Volcano Engine account, which can be viewed on the Volcano Engine console. If you need help here, you can directly consult the customer service of Volcano Engine.

The last word is to extract the text

Not much to say, just look at the code

import base64

from volcengine.visual.VisualService import VisualService

img_path = 'D:\sdcard\lanya\ceshi.jpg'  # 图片

with open(img_path, 'rb') as f:
    image_data = f.read()
    base64_data = base64.b64encode(image_data)  # 图片转base64编码
visual_service = VisualService()
visual_service.set_ak('这里是你的AK')
visual_service.set_sk('这里是你的SK')
form = dict()
form["image_base64"] = base64_data

resp = visual_service.ocr_normal(form)
imageText = resp.get('data').get('line_texts')  # 获取文字
print(imageText)
num_list_string = " ".join('%s' % id for id in imageText)
res = num_list_string.find("车辆")  # 寻找图片里的车辆这两个字
print(res)

Below is the image I want to identify

The result of running the code is as follows:

 

The result is exactly what we want!

There will be a small pit here:

If there is an error when the script is running, the error is reported in Util.py, and you need to download crypto. If so, change the folder name to a capital C. After downloading, it should be crypto, and change it to Crypto.

Continue to run after modification

At this time we need to download pycryptodome

Download command pip install pycryptodome

Then run it again and it will be OK without error.

Guess you like

Origin blog.csdn.net/suixing6/article/details/128116206