Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Python call Baidu AI to realize the function of form recognition on pictures?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Xiaobian to share with you how Python calls Baidu AI to achieve the table recognition function on the picture, I hope you have something to gain after reading this article, let's discuss it together!

Steps to install Baidu AI library! pip install baidu-aip Register Baidu AI Open Platform

First register Baidu AI, get ID and key. Registration method can refer to: registration method just go to "1.6 get the key" can be. Then record your APP_ID, API_KEY, SECRET_KEY, and you can start.

Call AipOcr library identification table text from aip import AipOcr #Import AipOcr module, used to do Optical Character Recognition APP_ID = '*******' #API_KEY you applied for ='*******'#SECRET_KEY you applied for = AipOcr(APP_ID, API_KEY, SECRET_KEY)

The selected picture is a chemical equation table (from the network)

Open the picture in binary mode and read:

file = "table picture\\chemical equation table.jpg"pic = open(file,'rb')#open picture in binary img = pic.read () #read table = client. tableRecognizationAsync (img) #Call table recognition module print(table)

Then call tableRecognizationAsync (), and store the return value in the variable table and see:

table is a dictionary with two keys, one called result and one called log_id. What we need is the request_id in result, which can be obtained by:

request_id = table['result'][0]['request_id']request_id

With this ID, you can obtain the download address of the Excel form saved after identification. Pass request_id into getTableRecognizationResult () to get the result.

result = client.getTableRecognitionResult(request_id)print(result)

Print the result and you will see the following. The URL corresponding to result_data is the download address of Excel.

Call the webbrowser library and use the webbrowser.open(url) statement to automatically open the URL for download:

url = result['result']['result_data']import webbrowser #Open browser webbrowser.open(url)

Attachment: Python opens a specified URL through a browser

1. Open web pages via default browser

import webbrowserwebbrowser.open("http://www.baidu.com")

webbrowser.open, new=0, autoraise=True) Access url address in system default browser, if new=0, url will be in same

browser window; if new=1, a new browser window opens;new=2, a new browser tab opens

2. Start the browser and open the specified web page through the os module

import osos.system('"C:\Program Files\internet explorer\iexplore.exe" http://www.baidu.com')

3. using selenium

from selenium import webdriverurl='http://www.baidu.com'driver = webdriver.Firefox()driver.get(url)

The identification results are as follows:

This Excel file contains 6 worksheets, which correspond to the table contents in the picture as follows. The body stores the contents of the table section, the header stores the text of the header, and the footer stores the text of the footer.

The recognition effect is not perfect, but overall it seems OK. Of course, the clearer the manuscript, the better the recognition effect.

the problems that may be encountered

'error_msg': 'image size error, image is too big or too small, upper limit 4M, lower limit 1k, please check your param'

bulk operation

First get the path of all pictures and save it in the pictures list. The results are as follows.

#Get all the picture files under the path and store them in the list import oswork_path = "table pictures\\"pictures=[] #Store the path of all files in the folder (including files in subdirectories) for root, dirs, files in os.walk(work_path): path = [os.path.join(root, name) for name in files] pictures.extend(path)pictures

Then all the pictures are transferred to the table recognition interface one by one to obtain the request ID and the download address of the Excel file containing the recognition result. Before extracting the Excel download path, it is necessary to determine whether the recognition is complete. This is determined by identifying the value corresponding to 'ret_msg' in the dictionary returned by the result. Download links are available only if it is "completed." The while loop is used here, refreshing the status every 2 seconds until the status is "completed" before extracting the link. Then use requests.get() to get download link information and write it to Excel file. Excel automatic naming and download results are shown in the figure.

from aip import AipOcr #Import AipOcr module, used for Optical Character Recognition import time #Time module import requests #For HTTP requests APP_ID = '24800359' #API_KEY = 'PrmTtmrqkeaqhvxOPEN4eZVt'#SECRET_KEY = 'LOFpCH6wpLV7xZPG0DTcvV4x1Sqyvmk9'#Client = AipOcr(APP_ID, API_KEY, SECRET_KEY)#Submit identification requests and store all request IDs for pictures in pictures: pic = open(picture,'rb')#Open picture in binary img = pic.read () #Read table = client.tableRecognitionAsync(img) #Call table recognition module request_id = table['result'][0]['request_id'] #Determine whether the recognition is complete, and obtain the Excel download path according to the request ID until it is completed result = client. getTableRecognizationResult (request_id) #Get recognition result by ID while result['result']['ret_msg'] != 'Completed': #Download address is only available if status is 'Completed' time.sleep(2) #pause for 2 seconds and refresh result = client. getTableRecognizationResult (request_id) #Keep refreshing until the condition is met download_path = result['result']['result_data'] #Download and set Excel file name to image name excel_name = picture.split(". ")[0] + ".xls" #Make excel file name the same as picture excel = requests.get(download_path) #grab download link file = open(excel_name, 'wb')#New excel file file.write(excel.content) #Write excel file and save After reading this article, I believe you have a certain understanding of "Python how to call Baidu AI to realize the table recognition function on pictures." If you want to know more about it, please pay attention to the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report