利用文字定位模塊與文字識別模塊的接口批量處理圖片生成OCR結果

程序流程圖

Created with Raphaël 2.2.0開始讀取所有文件列表文件是否處理完?結束與ctpn模塊進行交互等待執行完畢與crnn模塊進行交互等待執行完畢保存處理後的OCR文字結果保存圖片定位結果保存到確定目錄yesno

代碼片

通過訪問遍歷文件夾中的所有圖片名字,找到名字裏面不帶‘_’符號的爲原始圖片,然後將這個圖片複製到確定的路徑下並覆蓋原來的圖片,以便ctpn算法識別。


import json
from flask import Flask, request
from flask_cors import CORS
import os
import time
#OCR TEST
#from ocr_ctpn_xd import get_chi
import base64
import cv2

def ocr_test():
   path='路徑1'
   file_img=os.listdir(path)
   count=0
   print(len(file_img))
   for f_i in file_img:
       img_path=path+f_i
       f_count = f_i.split('.')[0]
       if len(f_count.split('_'))>1:
           continue
       if os.path.exists('路徑2'+f_count+'_ctpn.jpg'):
          continue
       if os.path.exists('路徑3/s.jpg'):
          os.remove('路徑3/s.jpg')

       os.system('cp '+img_path+' 路徑3/s.jpg')

通過讀取done.txt表示ctpn可以進行處理了。

       file_read_result=open('路徑4/done.txt','w')
       file_read_result.write(str(1))

       file_read_result.close()


接下來ctpn模塊執行文字定位工作,ctpn_count.txt裏面存放的是標記信息,ctpn與調用函數進行交互。


       '''write to ctpn,start to count'''
       if os.path.exists('路徑5/ctpn_count.txt'):
          file_read_ctpn_count=open('路徑5/ctpn_count.txt','r')
          c_ctpn=file_read_ctpn_count.readline()
          print(c_ctpn)
          if (c_ctpn=='')|(c_ctpn=='0'):
             c_ctpn='0'
             file_read_ctpn_count.close()
             os.remove('路徑5/ctpn_count.txt')
             file_write_ctpn_count = open('/home/xudong/ocr/text_detection-master/text-detection-ctpn/ctpn_count.txt', 'w')

             file_write_ctpn_count.write(str(int(c_ctpn)+1))
             file_write_ctpn_count.close()    

ctpn模塊執行完畢之後開始執行crnn模塊。
以下代碼中的done.txt中存放的是crnn是否處理完成。

        while 1:
          if os.path.exists('路徑4/done.txt')==False:
             time.sleep(0.1)
             continue
          else:
             break
       while 1:
          file_read_result=open('路徑4/done.txt','r')
          res_crnn=file_read_result.readline()
          result=''
          if res_crnn=='1':
             file_read_result.close()
             time.sleep(0.1)
             continue
          elif res_crnn=='0':
             file_read_result.close()
             break

處理完後開始讀取crnn結果,通過讀取路徑6/crnn_result.txt中的識別結果來獲取文字識別信息。

       while 1:
          if os.path.exists('路徑6/crnn_result.txt')==False:
             time.sleep(0.1)
             continue
          else:
             break
       file_read_result=open('路徑6/crnn_result.txt','r')
       res_crnn=file_read_result.readline()
       result=''
       file_result=open('ocr_result.txt','a+')
       while res_crnn!='':
          result+=res_crnn
          res_crnn = file_read_result.readline()
       file_read_result.close()
       os.remove('路徑6/crnn_result.txt')
       file_result.write(f_i+',')
       file_result.write(result+'\n')
       file_result.close()
       f_count=f_i.split('.')[0]

將ctpn中生成的文字定位圖片保存到原路徑下同時名字後面加後綴‘_ctpn’。

       os.system('cp 路徑7/s.jpg 路徑8/img10000/'+f_count+'_ctpn.jpg')

       file_print=open('print.txt','a+')
       file_print.write(str(count))
       file_print.close()

       count+=1

if __name__ == '__main__':
  ocr_test()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章