tesseract_ocr.md

記事に戻る

# -*- coding: utf-8 -*-
import subprocess
from pathlib import Path, WindowsPath
from PyQt5 import QtCore, QtGui

class TesseractOCR(QtCore.QThread):
    nextSignal = QtCore.pyqtSignal(int, int, str)

    def __init__(self, parent, tesseractPath):
        super(TesseractOCR, self).__init__(parent)
        self.tesseract = list( Path(tesseractPath).glob('**/tesseract.exe') )[0]
        self.items = []
        self.whiteListFilePath = ''

    def run(self):
        
        imageFilePath = str( Path('__temp__.png').absolute() )
        ocredTextPath = Path('__temp__.txt')

        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        startupinfo.wShowWindow = subprocess.SW_HIDE

        for parentRow, fileItem in enumerate(self.items):
            for childRow in range(fileItem.rowCount()):
                rectPixmap = fileItem.child(childRow, 1).data()
                if rectPixmap is None:
                    continue

                rectPixmap.save(imageFilePath)
                
                cmd = [self.tesseract, imageFilePath, ocredTextPath.stem, self.whiteListFilePath]

                returncode = subprocess.Popen(cmd, startupinfo=startupinfo)
                returncode.wait()
                
                if ocredTextPath.exists():
                    with open(ocredTextPath, 'r', encoding='utf-8') as file:
                        outputText = file.readline()
                    ocredTextPath.unlink()
                else:
                    outputText = ''

                self.nextSignal.emit(parentRow, childRow, outputText)