程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python case] OCR extracts the text in the picture

編輯:Python

Many software have built-in OCR function , That is, picture extraction text function . Some are free for everyone to use , But some are charged . Whether it's free or free , After all, I can't escape from the privacy issue . With others' OCR, You have to send the picture to the other party's server . Today we use Python To develop a OCR Software , As shown in the figure below .

1 Installation environment

This article is based on PaddleOCR Build a locally developed image extraction software , So you need to install PaddlePaddle Environmental Science .

1.1 install PaddlePaddle

If your machine is equipped with CUDA9 or CUDA10, Recommended installation GPU Version of PaddlePaddle, Enjoy faster running speed . Run the following command to install :

python -m pip install paddlepaddle-gpu -i https://mirror.baidu.com/pypi/simple

If your machine has only CPU Environmental Science , The running speed will be a little slower . Please run the following command to install

python -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple

1.2 install PaddleOCR whl package

With PaddlePaddle After environment , Next install PaddleOCR library , Recommended 2.0.1+ edition :

pip install "paddleocr>=2.0.1"

Be careful :

about Windows Environment user : Directly through pip Installed shapely Library may appear [winRrror 126] No problem found for the specified module . Suggest from here https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely download shapely The installation package completes the installation .

2 call OCR

call OCR The process is very simple , Import PaddleOCR after , Create directly PaddleOCR object :

from paddleocr import PaddleOCR
ocr = PaddleOCR(use_angle_cls=True, lang="ch")
ocr.ocr(img_path, cls=True)

The first 2 Line code ,use_angle_cls The parameter is used to determine whether to use the angle classification model , That is, whether to recognize the vertical text .lang The parameter represents the recognized language , We passed ch, Indicates recognition of Chinese characters .

The first 3 Line code , img_path Represents the image path ,cls Indicates whether the angle classification model is used .

3 Development interface

With the above code, you can complete OCR function , But it is not convenient to use , We will go further OCR Functions are encapsulated in software , Easy to interact . First installation PyQT5:

pip install PyQt5

Next call PyQT5 Complete interface interaction , The space for , Only calls... Are shown here ocr Part of the code , Readers can directly pull to the end to obtain the complete source code .

class OCRGUI(QWidget):
# Other codes are omitted ...
def run_ocr(self, img_path):
result = self.ocr.ocr(img_path, cls=True)
self.text.clear()
txts = [line[1][0] for line in result]
for txt in txts:
self.text.insertPlainText(txt + "\n")
self.loading.hide()

4 Access to the source code

  1. Official account :Python Learning from actual combat
  2. Official account chat interface reply :OCR, Get the full source code .

If you find this article helpful , Thank you for your free praise , Your effort will provide me with unlimited writing power ! Welcome to my official account. :Python Learning from actual combat , Get the latest articles first .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved