簡體   English   中英

當我使用CREATE_NO_WINDOW運行帶有pytesseract的tesseract時,如何隱藏控制台窗口

[英]How to hide the console window when I run tesseract with pytesseract with CREATE_NO_WINDOW

我正在使用tesseract在screengrabs上執行OCR。 我有一個應用程序使用tkinter窗口在我的類的初始化中利用self.after來執行常量圖像擦除並更新tkinter窗口中的標簽等值。 我搜索了好幾天,找不到任何具體的例子,如何在Windows平台上使用Python3.6來使用CREATE_NO_WINDOW,並使用pytesseract調用tesseract。

這與這個問題有關:

當我使用pytesser運行tesseract時,如何隱藏控制台窗口

我只編寫了2周的Python編程,並且不明白在上述問題中執行的步驟是什么/如何執行。 我打開了pytesseract.py文件並查看並找到了proc = subprocess.Popen(命令,stderr = subproces.PIPE)行但是當我嘗試編輯它時,我得到了一堆我無法弄清楚的錯誤。

#!/usr/bin/env python

'''
Python-tesseract. For more information: https://github.com/madmaze/pytesseract

'''

try:
    import Image
except ImportError:
    from PIL import Image

import os
import sys
import subprocess
import tempfile
import shlex


# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = 'tesseract'

__all__ = ['image_to_string']


def run_tesseract(input_filename, output_filename_base, lang=None, boxes=False,
                  config=None):
    '''
    runs the command:
        `tesseract_cmd` `input_filename` `output_filename_base`

    returns the exit status of tesseract, as well as tesseract's stderr output

    '''
    command = [tesseract_cmd, input_filename, output_filename_base]

    if lang is not None:
        command += ['-l', lang]

    if boxes:
        command += ['batch.nochop', 'makebox']

    if config:
        command += shlex.split(config)

    proc = subprocess.Popen(command, stderr=subprocess.PIPE)
    status = proc.wait()
    error_string = proc.stderr.read()
    proc.stderr.close()
    return status, error_string


def cleanup(filename):
    ''' tries to remove the given filename. Ignores non-existent files '''
    try:
        os.remove(filename)
    except OSError:
        pass


def get_errors(error_string):
    '''
    returns all lines in the error_string that start with the string "error"

    '''

    error_string = error_string.decode('utf-8')
    lines = error_string.splitlines()
    error_lines = tuple(line for line in lines if line.find(u'Error') >= 0)
    if len(error_lines) > 0:
        return u'\n'.join(error_lines)
    else:
        return error_string.strip()


def tempnam():
    ''' returns a temporary file-name '''
    tmpfile = tempfile.NamedTemporaryFile(prefix="tess_")
    return tmpfile.name


class TesseractError(Exception):
    def __init__(self, status, message):
        self.status = status
        self.message = message
        self.args = (status, message)


def image_to_string(image, lang=None, boxes=False, config=None):
    '''
    Runs tesseract on the specified image. First, the image is written to disk,
    and then the tesseract command is run on the image. Tesseract's result is
    read, and the temporary files are erased.

    Also supports boxes and config:

    if boxes=True
        "batch.nochop makebox" gets added to the tesseract call

    if config is set, the config gets appended to the command.
        ex: config="-psm 6"
    '''

    if len(image.split()) == 4:
        # In case we have 4 channels, lets discard the Alpha.
        # Kind of a hack, should fix in the future some time.
        r, g, b, a = image.split()
        image = Image.merge("RGB", (r, g, b))

    input_file_name = '%s.bmp' % tempnam()
    output_file_name_base = tempnam()
    if not boxes:
        output_file_name = '%s.txt' % output_file_name_base
    else:
        output_file_name = '%s.box' % output_file_name_base
    try:
        image.save(input_file_name)
        status, error_string = run_tesseract(input_file_name,
                                             output_file_name_base,
                                             lang=lang,
                                             boxes=boxes,
                                             config=config)
        if status:
            errors = get_errors(error_string)
            raise TesseractError(status, errors)
        f = open(output_file_name, 'rb')
        try:
            return f.read().decode('utf-8').strip()
        finally:
            f.close()
    finally:
        cleanup(input_file_name)
        cleanup(output_file_name)


def main():
    if len(sys.argv) == 2:
        filename = sys.argv[1]
        try:
            image = Image.open(filename)
            if len(image.split()) == 4:
                # In case we have 4 channels, lets discard the Alpha.
                # Kind of a hack, should fix in the future some time.
                r, g, b, a = image.split()
                image = Image.merge("RGB", (r, g, b))
        except IOError:
            sys.stderr.write('ERROR: Could not open file "%s"\n' % filename)
            exit(1)
        print(image_to_string(image))
    elif len(sys.argv) == 4 and sys.argv[1] == '-l':
        lang = sys.argv[2]
        filename = sys.argv[3]
        try:
            image = Image.open(filename)
        except IOError:
            sys.stderr.write('ERROR: Could not open file "%s"\n' % filename)
            exit(1)
        print(image_to_string(image, lang=lang))
    else:
        sys.stderr.write('Usage: python pytesseract.py [-l lang] input_file\n')
        exit(2)


if __name__ == '__main__':
    main()

我正在利用的代碼類似於類似問題中的示例:

def get_string(img_path):
    # Read image with opencv
    img = cv2.imread(img_path)
    # Convert to gray
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Apply dilation and erosion to remove some noise
    kernel = np.ones((1, 1), np.uint8)
    img = cv2.dilate(img, kernel, iterations=1)
    img = cv2.erode(img, kernel, iterations=1)
    # Write image after removed noise
    cv2.imwrite(src_path + "removed_noise.png", img)
    #  Apply threshold to get image with only black and white
    # Write the image after apply opencv to do some ...
    cv2.imwrite(src_path + "thres.png", img)
    # Recognize text with tesseract for python

    result = pytesseract.image_to_string(Image.open(src_path + "thres.png"))

    return result

當它到達以下行時,黑色控制台窗口閃爍不到一秒鍾,然后在運行命令時關閉。

result = pytesseract.image_to_string(Image.open(src_path + "thres.png"))

這是控制台窗口的圖片:

程序文件(x86)_Tesseract

以下是其他問題的建議:

您目前正在使用IDLE,在這種情況下,如果出現控制台窗口,我認為這並不重要。 如果您計划使用此庫開發GUI應用程序,則需要修改pytesser.py中的subprocess.Popen調用以隱藏控制台。 我首先嘗試CREATE_NO_WINDOW進程創建標志。 - eryksun

我非常感謝有關如何使用CREATE_NO_WINDOW修改pytesseract.py庫文件中的subprocess.Popen調用的任何幫助。 我也不確定pytesseract.py和pytesser.py庫文件之間的區別。 我會對另一個問題發表評論,要求澄清,但我不能在這個網站上有更多的聲譽。

我做了更多的研究,並決定更多地了解subprocess.Popen:

子流程的文檔

我還引用了以下文章:

使用python subprocess.popen ..不能阻止exe停止工作提示

我更改了pytesseract.py中的原始代碼行:

proc = subprocess.Popen(command, stderr=subprocess.PIPE)

以下內容:

proc = subprocess.Popen(command, stderr=subprocess.PIPE, creationflags = CREATE_NO_WINDOW)

我運行代碼並得到以下錯誤:

Tkinter回調中的異常回溯(最近一次調用最后一次):
文件“C:\\ Users \\ Steve \\ AppData \\ Local \\ Programs \\ Python \\ Python36-32 \\ lib \\ tkinter__init __。py”,第1699行,在調用返回self.func(* args)文件“C:\\ Users \\ Steve \\ Documents \\ Stocks \\ QuickOrder \\ QuickOrderGUI.py“,第403行,在gather_data update_cash_button()文件”C:\\ Users \\ Steve \\ Documents \\ Stocks \\ QuickOrder \\ QuickOrderGUI.py“,第208行,在update_cash_button currentCash = get_string(src_path + “cash.png”)文件“C:\\ Users \\ Steve \\ Documents \\ Stocks \\ QuickOrder \\ QuickOrderGUI.py”,第150行,在get_string中結果= pytesseract.image_to_string(Image.open(src_path +“thres.png”))文件“C:\\ Users \\ Steve \\ AppData \\ Local \\ Programs \\ Python \\ Python36-32 \\ lib \\ site-packages \\ pytesseract \\ pytesseract.py”,第125行,在image_to_string中配置config = config)文件“C:\\ Users \\ Steve \\ AppData \\ Local \\ Programs \\ Python \\ Python36-32 \\ lib \\ site-packages \\ pytesseract \\ pytesseract.py“,第49行,在run_tesseract proc = subprocess.Popen(命令,stderr = subprocess.PIPE,creationflags = CREATE_NO_WINDOW) NameError:未定義名稱“CREATE_NO_WINDOW”

然后我定義了CREATE_NO_WINDOW變量:

#Assignment of the value of CREATE_NO_WINDOW
CREATE_NO_WINDOW = 0x08000000

我從上面鏈接的文章中得到了0x08000000的值。 添加定義后,我運行了應用程序,我沒有得到任何更多的控制台窗口彈出窗口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM