看到一段基于Python的OCR文字识别程序,记录备查
import pytesseract
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import os
from sys import platform
class MYOCR(QWidget):
def __init__(self):
super(MYOCR, self).__init__()
XSIZE, YSIZE= 1400, 850
self.resize(XSIZE, YSIZE)
self.setWindowTitle("OCR文字识别小工具(星未)")
self.imageFile=""
btn_open = QPushButton(self)
btn_open.setText("打开图片")
btn_open.move(10, 10)
btn_open.clicked.connect(self.openimage)
self.language='chi_sim'
self.choice_lang = '语言'
self.choice_list_lang = ['简体汉语','繁体汉语', '英语','OSD多语言']
self.combobox_lang = QComboBox(self)
self.combobox_lang.move(100,10)
self.combobox_lang.addItem(self.choice_lang) # 4
self.combobox_lang.addItems(self.choice_list_lang) # 5
self.combobox_lang.currentIndexChanged.connect(lambda: self.on_combobox_func(self.combobox_lang))
self.configLabel=QLabel(self)
self.configLabel.move(250, 10)
self.configLabel.setFixedSize(100, 25)
self.configLabel.setText('config:')
configTip1="""-oem:\n
0 Legacy engine only.
1 Neural nets LSTM engine only.
2 Legacy + LSTM engines.
3 Default, based on what is available.\n"""
configTip2="""--psm:\n
0 Orientation and script detection (OSD) only.
1 Automatic page segmentation with OSD.
2 Automatic page segmentation, but no OSD, or OCR. (not implemented)
3 Fully automatic page segmentation, but no OSD. (Default)
4 Assume a single column of text of variable sizes.
5 Assume a single uniform block of vertically aligned text.
6 Assume a single uniform block of text.
7 Treat the image as a single text line.
8 Treat the image as a single word.
9 Treat the image as a single word in a circle.
10 Treat the image as a single character.
11 Sparse text. Find as much text as possible in no particular order.
12 Sparse text with OSD.
13 Raw line. Treat the image as a single text line,
bypassing hacks that are Tesseract-specific."""
configTip = configTip1 + configTip2
self.defaulet_config=r'--oem 3 --psm 6'
self.configbox = QTextEdit(self)
self.configbox.move(300, 10)
self.configbox.setFixedSize(250, 25)
self.configbox.setText(self.defaulet_config)
self.configbox.setToolTip(configTip)
self.custom_oem_psm_config = self.configbox.toPlainText()
self.config=self.custom_oem_psm_config
btn_ocr = QPushButton(self)
btn_ocr.setText("开始识别")
btn_ocr.move(570, 10)
btn_ocr.clicked.connect(self.run_terseract)
self.saveFilePath= os.path.abspath(os.getcwd())
self.saveFile='text.txt'
self.saveFileFull=""
if platform == "linux" or platform == "linux2":
self.saveFileFull= self.saveFilePath + "/" + self.saveFile
elif platform == "darwin":
# OS X
self.saveFileFull=self.saveFilePath + "/" +self.saveFile
elif platform == "win32":
self.saveFileFull=self.saveFilePath + "\\" + self.saveFile
btn_save = QPushButton(self)
btn_save.setText("保存到文件:")
btn_save.move( int(XSIZE/2.0)+5, 10)
btn_save.clicked.connect(self.save_text_to_file)
self.filebox = QTextEdit(self)
self.filebox.move( int(XSIZE/2.0)+100, 10)
self.filebox.setFixedSize(540, 25)
self.filebox.setText(self.saveFile)
################################################################
### 图片
self.xsize=690
self.ysize=800
self.label = QLabel(self)
self.label.setText(" ")
self.label.move(10, 40)
self.label.setFixedSize(self.xsize, self.ysize)
self.label.setScaledContents(True)
self.label.setStyleSheet("QLabel{background:white;}"
"QLabel{color:rgb(255,255,255);}"
)
### 文字
self.textbox = QTextEdit(self)
self.textbox.move(705, 40)
self.textbox.setFixedSize(self.xsize, self.ysize)
def on_combobox_func(self, combobox):
# 8
if combobox == self.combobox_lang:
dir_lang = {'语言':'chi_sim','简体汉语':'chi_sim', '繁体汉语':'chi_tra', '英语':'eng', 'OSD多语言':'osd'}
all_lang = pytesseract.get_languages(config='')
self.language=dir_lang[ combobox.currentText() ]
elif combobox == self.combobox_method:
dir_method=[]
self.lineedit.setFont(combobox.currentFont())
else:
pass
#QMessageBox.information(self, 'ComboBox lang', '{}: {}'.format(combobox.currentIndex(), combobox.currentText()))
def save_text_to_file(self):
filename = self.filebox.toPlainText()
with open(filename,'w') as f:
ocr_text=self.textbox.toPlainText()
f.write(ocr_text)
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText("文字已保存到 " + filename + " 中!")
msg.exec_()
def openimage(self):
imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.png;;*.jpg;;All Files(*)")
imagefile= QtGui.QPixmap(imgName).scaled(self.label.width(), self.label.height())
self.label.setPixmap(imagefile)
self.imageFile = imgName
def run_terseract(self):
self.custom_oem_psm_config = self.configbox.toPlainText()
self.config=self.custom_oem_psm_config
if self.language=='语言':
self.language='chi_sim'
self.textbox.setText("<font color='red'> 设别中,请稍后.\n"+ \
self.language + "\n"+ \
self.config + ".</font>")
QApplication.processEvents()
XImage = self.imageFile
xstring= pytesseract.image_to_string(XImage, lang=self.language, config=self.config)
if self.language=='chi_sim' or self.language=='chi_tra':
self.textbox.setText(xstring.replace(' ',''))
else:
self.textbox.setText(xstring)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
my = MYOCR()
my.show()
sys.exit(app.exec_())
网友评论