Look at the picture to identify the font:Python implements image recognition text program-Font Tutorial免费ppt模版下载-道格办公

Python implements image recognition text program

Tesseract+ Baidu text recognition API. The following is a sample program that uses Python to realize the function of image recognition text: ```pythonimport requestsfrom PIL import Imageimport pytesseract# Set the relevant information of Baidu text recogn

Example Introduction

Python uses Baidu text recognition function to scan uploaded pictures, obtain text information of pictures, and generate executable programs.

Development environment: Windows7+python3.7+pycharm2018.2.4 (development tools);

Directory organization: the folders "build", "dist" and the file "discernImg.spec" are the data generated when the exe is generated;

Final effect:


Create application on Baidu side

Creation process reference"WeChat applet uses Baidu AI to realize the function of scanning ID card to obtain information" (put " Image recognition" was replaced by "text recognition").


Implementation process

1. Reader UI design

1. Install modules and configuration tools, refer to "Python Realizes Novel Reader";

2. Run the tool QtDesigner, use the QtDesigner toolbox to design the interface effect (the required controls can be viewed in the right area), and save the effect as a file dc.ui;

3. For the file dc .ui executes pyUIC (ui is converted into py code), and the file dc.py is generated after execution.

2. Code design

The code of the file "discernImg.py" is as follows, for specific code explanation, please refer to "Python Smart Contact Management";

Tips: replace tokenUrl with text recognition, please refer to for details

"https://cloud.baidu.com/doc/OCR/s/Sk3h7xyad";

from PyQt5.QtWidgets import *from PyQt5.QtGui import *# import custom module import dc# import built-in module import sysimport os# import third-party module import requests, base64from PIL import Imageclass parentWindow(QWidget, dc. Ui_Form): # Initialization method def __init__(self): # Find the parent class first page super(parentWindow, self).__init__() # Initialize the page method self.setupUi(self) # Click to select the picture self.selectImg.clicked.connect( self.openfile) # Click to view the image self.viewImg.clicked.connect(self.viewbtn) # Select the image execution method def openfile(self): # Start the file selection dialog empty, search for jpg and png images self.download_path = QFileDialog.getOpenFileName (self, "Select the picture to be recognized", os.getcwd(), "Image Files(*.jpg *.png)") # Determine whether to select a picture if not self.download_path[0].strip(): QMessageBox. information(self, 'prompt information', 'no business card picture selected') pass else: # pixmap parses the picture pixmap = QPixmap(self.download_path[0]) # set the picture self.imgLabel.setPixmap(pixmap) # make the picture adaptive Label size self.imgLabel.setScaledContents(True) try: # Recognize the business card image and return the recognition result content = self.recgImg() except: QMessageBox.information(self, 'prompt information', 'recognition error, please re-select the image') # Recognition Image data assignment words_result = content['words_result'] # print(words_result) text = '' for item in words_result: for v in item.values(): text = text + '\n' + v self.discernText.setText (text) # Identify business card image def recgImg(self): # Get baiduToken apikey = 'your apikey' seckey = 'your seckey' tokenUrl = 'https://aip.baidubce.com/oauth/2.0/token?grant_type =client_credentials&client_id=' + apikey + '&client_secret=' + seckey res = requests.get(url=tokenUrl, headers={'content-type': 'application/json; charset=UTF-8'}).json() baiduToken = res['access_token'] ''' Image recognition (API) ''' request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage" # Open image file in binary mode f = open (self.download_path[0], 'rb') img = base64.b64encode(f.read()) params = {"image": img} # access_token = '[token obtained by calling the authentication interface]' request_url = request_url + "?access_token=" + baiduToken headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(request_url, data=params, headers=headers) if response: # print (response.json()) return response.json() # Click to view the image to display a larger image function def viewbtn(self): if self.download_path: # Use the image viewing tool in the computer to open the image img = Image.open(self. download_path[0]) # Display image img.show() else: QMessageBox.information(self, 'prompt information', 'select business card picture first')if __name__ == '__main__': # Every PyQt5 application must create a Application object app = QApplication(sys.argv) # Initialize the page window = parentWindow() # Display the home page window.show() sys.exit(app.exec_())

Three, Packaged into exe

Execute the statement "pyinstaller -F discernImg.py" under the project. After the packaging is successful, a new dist file is added to the project, and the exe file is the packaged program.

Articles are uploaded by users and are for non-commercial browsing only. Posted by: Lomu, please indicate the source: https://www.daogebangong.com/en/articles/detail/Python%20implements%20image%20recognition%20text%20program.html

Like (810)
Reward 支付宝扫一扫 支付宝扫一扫
single-end

Related Suggestion