2017-08-07 84 views
0

我使用pytesseract與線:pytesseract臨時輸出文件「沒有這樣的文件或目錄」錯誤

text = image_to_string(temp_test_file, 
         lang='eng', 
         boxes=False, 
         config='-c preserve_interword_spaces=1 hocr') 

,並與收到錯誤

pytesseract.py 
135| f = open(output_file_name, 'rb') 

No such file or directory: 
/var/folders/j3/dn60cg6d42bc2jwng_qzzyym0000gp/T/tess_EDOHFP.txt 

查看源代碼pytesseract here ,它似乎無法找到它用來存儲tesseract命令輸出的臨時輸出文件。

我已經在這裏看到了其他的答案,通過檢查tesseract是否已經安裝並可以從命令終端調用並且對我來說已經解決了,所以這不是問題。任何想法這可能是什麼,以及如何解決它?

謝謝:)

回答

0

事實證明,這是pytesseract無法找到臨時的輸出文件是他們在被存儲比.txt或.box的其他擴展的原因(他們.hocr文件) 。從源代碼中,這些是pytesseract支持的唯一類型的tesseract輸出文件(或者更像是pytesseract的「查找」)。從源頭上相關的片段低於:

input_file_name = '%s.bmp' % tempnam() output_file_name_base = tempnam() if not boxes: output_file_name = '%s.txt' % output_file_name_base else: 123 output_file_name = '%s.box' % output_file_name_base

if status: errors = get_errors(error_string) raise TesseractError(status, errors) 135 f = open(output_file_name, 'rb')

在pytesseract的github上pulls來看,這似乎是對其它輸出類型,計劃但尚未實施的支持(我使用的源代碼以顯示爲什麼.hocr文件似乎未被發現是從pytesseract master分支複製/粘貼)。

在此之前,我對pytesseract腳本進行了一些篡改,以支持多種文件類型。

此版本沒有爲輸出文件設置擴展名(因爲tesseract會自動執行此擴展)並查看pytesseract將其臨時輸出文件存儲到的目錄,並查找以輸出文件名開頭的文件(最多「」通過pytesseract分配的第一個字符)(無需關心擴展名):

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


def image_to_string(image, lang=None, boxes=False, config=None, nice=0): 
    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, _) = tempnam() #'%s.bmp' % tempnam() 
    input_file_name += '.bmp' 
    (output_file_name_base, output_filename_base_dir) = tempnam() 
    if not boxes: 
     # Don’t put an extension on the output file name because Tesseract will do it automatically 
     output_file_name = '%s' % output_file_name_base 
    else: 
     output_file_name = '%s.box' % output_file_name_base 

    try: 
     ########## DEBUGGING 
     #print('input file name: %s' % input_file_name) 
     #print('temp output name: %s' % output_file_name) 
     #print('temp output dir: %s' % output_filename_base_dir) 
     ########## 

     image.save(input_file_name) 
     status, error_string = run_tesseract(input_file_name, 
              output_file_name_base, 
              lang=lang, 
              boxes=boxes, 
              config=config, 
              nice=nice) 

     if status: 
      errors = get_errors(error_string) 
      raise TesseractError(status, errors) 


     # find the temp output file in temp dir under whatever extension tesseract has assigned 
     output_file_name += '.' 
     output_file_name_leaf = os.path.basename(output_file_name) 
     print('**output file starts with %s, type: %s' % (output_file_name, type(output_file_name))) 
     l=os.listdir(output_filename_base_dir) 
     for f in l:    
      if f.startswith(output_file_name_leaf): 
       output_file_name_leaf = f 
       break 


     output_file_name_abs = os.path.join(output_filename_base_dir, output_file_name_leaf) 
     f = open(output_file_name_abs, 'rb') 
     try: 
      return f.read().decode('utf-8').strip() 
     finally: 
      f.close() 

    finally: 
     cleanup(input_file_name) 
     # if successfully created and opened temp output file 
     if 'output_file_name_abs' in locals(): 
      output_file_name = output_file_name_abs 
      print('**temp output file %s successfully created and deleted' % output_file_name) 
     cleanup(output_file_name) 

希望這有助於他人。

相關問題