2017-06-24 9 views
1

私はscreengrabsでOCRを実行するためにtesseractを使用しています。私はtkinterウインドウを使って、一定のイメージスクラップを実行し、tkinterウインドウのラベルなどの値を更新するクラスの初期化でself.afterを利用するアプリケーションを持っています。私は複数の日を検索しており、pytesseractでtesseractを呼び出すWindowsプラットフォーム上でCREATE_NO_WINDOWをPython3.6でどのように活用するかについて具体的な例は見つけられません。CREATE_NO_WINDOWでpytesseractでtesseractを実行すると、コンソールウィンドウを非表示にする方法

これは、この質問に関連している:

How can I hide the console window when I run tesseract with pytesser

私は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")) 

は、コンソールウィンドウの画像です:ここで

Program Files (x86)_Tesseract

が他の質問から提案されたものである。

You're currently working in IDLE, in which case I don't think it really matters if a console window pops up. If you're planning to develop a GUI app with this library, then you'll need to modify the subprocess.Popen call in pytesser.py to hide the console. I'd first try the CREATE_NO_WINDOW process creation flag. – eryksun

私は非常にどのようにのために任意の助けをいただければ幸いですCREATE_NO_WINDOWを使用してpytesseract.pyライブラリファイルのsubprocess.Popen呼び出しを変更します。 pytesseract.pyとpytesser.pyのライブラリファイルの違いについてもわかりません。私はもう一つの質問にコメントを残して、明確にするよう求めていますが、私はこのサイトでより多くの評判を得るまで私はできません。

答えて

3

私はより多くの研究を行なったし、subprocess.Popenについての詳細を学ぶことにしました:

Documentation for subprocess

私はまた、以下の記事参照:私は、コードの元の行を変更し

using python subprocess.popen..can't prevent exe stopped working prompt

をpytesseractで。PY:

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

私は、コードを実行し、次のエラーを得た:次へ

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

#Assignment of the value of CREATE_NO_WINDOW 
CREATE_NO_WINDOW = 0x08000000 
:私はその後、CREATE_NO_WINDOW変数を定義し

Exception in Tkinter callback Traceback (most recent call last):
File "C:\Users\Steve\AppData\Local\Programs\Python\Python36-32\lib\tkinter__init__.py", line 1699, in call return self.func(*args) File "C:\Users\Steve\Documents\Stocks\QuickOrder\QuickOrderGUI.py", line 403, in gather_data update_cash_button() File "C:\Users\Steve\Documents\Stocks\QuickOrder\QuickOrderGUI.py", line 208, in update_cash_button currentCash = get_string(src_path + "cash.png") File "C:\Users\Steve\Documents\Stocks\QuickOrder\QuickOrderGUI.py", line 150, in get_string result = pytesseract.image_to_string(Image.open(src_path + "thres.png")) File "C:\Users\Steve\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pytesseract\pytesseract.py", line 125, in image_to_string config=config) File "C:\Users\Steve\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pytesseract\pytesseract.py", line 49, in run_tesseract proc = subprocess.Popen(command, stderr=subprocess.PIPE, creationflags = CREATE_NO_WINDOW) NameError: name 'CREATE_NO_WINDOW' is not defined

私は0x08000000の値を得ました上記のリンクされた記事から。定義を追加した後、アプリケーションを実行しましたが、コンソールウィンドウのポップアップが表示されませんでした。

関連する問題