[character recognition applet] it can quickly recognize characters. It is a treasure artifact that can no longer be separated from when it is used ~ (i.e. the peak is permanently free)

preface

hello everyone! I'm classmate pear!

I hope you can support me! ha-ha

To thank everyone who cares about me: 💓 The project source code of each article is shared free of charge 💓👇👇👇👇

Click here for the blue font. What source code do you need? Remember to say the title and name! I can also write private letters!

Xiaobian has also been learning programming. If there are errors in the code applet, please leave a message in the comment area!

Finally - if the article helps you, remember to "pay attention", "like" and "comment"~

text

Do you often encounter these difficulties in your daily office:

â‘  Baidu Library VIP articles cannot be copied and pasted? The purchase cost is not low. It's prohibitive

â‘¡ Do you want to convert scanned documents into editable documents?

â‘¢ Extract the text in the picture? Taking photos is cool for a while. One by one, it turns into words. I'm worried to death

â‘£ Pdf files cannot be copied. How to extract text from PDF?

When it comes to picture to text, many people need to use it. Today, Xiaobian will teach you to make your own [picture character recognition] program

Preface~

 

Environment installation——

1) Prepare the corresponding identified pictures. Here are the material pictures randomly searched on the Internet!

2) The python 3.0 environment is ready for installation, and basically all Python versions can be edited 7. Pycharm2021, interfacial

Programming module Pyqt5, and then some of the self-contained can be imported directly without tube.

The common method of installing modules, that is, the small compilation of third-party modules, is: pip install + module name or speed up requires the use of image source,

Baidu or csdn search will come out a lot of image sources for installing modules, which will not be introduced one by one here!

Introduction to program principle:

python Use Baidu character recognition function to scan the uploaded pictures and obtain the text information of the pictures.

Import module——

import requests, base64
from PIL import Image

Select picture type as jpg ,. png.

# Select the image execution method def openfile(self): 
 
# Start the select file dialog and find jpg and png pictures 
 
self.download_path = QFileDialog.getOpenFileName(self, "Select a picture to recognize", os.getcwd(), "Image Files(*.jpg *.png)")

Image recognition: image recognition (API) ''

 request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage" 
 
 # Open the picture 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()

Effect display——

Code display——

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
 
# Introducing custom modules
import dc
# Introduction of built-in modules
import sys
import os
# Introduction of third-party modules
import requests, base64
from PIL import Image
 
class parentWindow(QWidget, dc.Ui_Form):
    # Initialization method
    def __init__(self):
        # Find parent class Homepage
        super(parentWindow, self).__init__()
        # Initialize page method
        self.setupUi(self)
        # Click to select a picture
        self.selectImg.clicked.connect(self.openfile)
        # Click to view the picture
        self.viewImg.clicked.connect(self.viewbtn)
 
    # Select Picture execution method
    def openfile(self):
        # Start the select file dialog and find jpg and png pictures
        self.download_path = QFileDialog.getOpenFileName(self, "Select a picture to recognize", 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 parse picture
            pixmap = QPixmap(self.download_path[0])
            # Set picture
            self.imgLabel.setPixmap(pixmap)
            # Let the picture adapt to the label size
            self.imgLabel.setScaledContents(True)
            try:
                # Identify the business card picture and return the recognition result
                content = self.recgImg()
            except:
                QMessageBox.information(self, 'Prompt information', 'Recognition error, please reselect the picture')
 
            # Identify the data assignment of the picture
            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 picture
    def recgImg(self):
        # Get Baidu token
        apikey = 'Yours apikey'
        seckey = 'Yours 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 picture 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 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 view picture to display large picture function
    def viewbtn(self):
        if self.download_path:
            # Open the picture using the picture viewing tool in the computer
            img = Image.open(self.download_path[0])
            # display picture
            img.show()
        else:
            QMessageBox.information(self, 'Prompt information', 'Select business card picture first')
 
if __name__ == '__main__':
    # Each PyQt5 application must create an application object
    app = QApplication(sys.argv)
    # Initialization page
    window = parentWindow()
    # Show home page
    window.show()
    sys.exit(app.exec_())

summary

This treasure artifact: character recognition and So easy can be achieved by scanning! Interested little partner 2, come to me to get the source code~

Follow Xiaobian for more wonderful content!

It's not easy to make. Remember to click three times!! If you need packaged source code + materials, share them for free!! Portal

 

Keywords: Python Programmer AI image processing image identification

Added by mapleshilc on Mon, 17 Jan 2022 19:21:49 +0200