前面第一個版本實現了基本的ocr功能,可以對某圖像文件進行處理,將ocr結果以同名txt文件的方式保存在圖像文件同路徑下;
然後在第二個版本中又實現了對文件夾參數的支持,也即可以對某個包含大量圖像文件的文件夾進行處理;同時還支持參數配置文件,以json文件的形式支持關鍵參數的配置,例如:設置txt文件的保存結果(當然為空時就還是以前的保存在圖像文件同目錄下)、設置排除字符(離線ocr過程中容易出現無意義的亂碼的字符)等;
但是,系統開發的小伙伴們又提出了新需求了,說實際應用中發現雖然大部分人員檔案都是掃描後保存為圖像格式,但也有一些人的檔案由於歷史原因是以pdf圖像的方式保存為一個pdf文件的,能不能也支持對這類文件的離線ocr?當然可以了,本質上都是圖像格式嘛!只需要找一個可以將pdf轉換為圖像的工具即可(當然,對於本身就是文字類型的pdf文件,可以利用其它工具直接提取其中的文字部分,不需要脫褲子放屁多此ocr識別一舉)!於是,對img2txt.py的第三次升級改造來了——
#!/home/super/miniconda3/bin/python
#encoding=utf-8
#author: superchao1982, [email protected]
#幫助信息
strhelp='''
img2txt is one program to get ocr texts from image files!
default threshold is 0.1;
default langpath is '/home/langdata' for linux and 'C:\ocr\langdata' for win;
default remove char is '| _^~`&';
default path storing the ocr texts are the same directory with images;
default settings above can be changed in the file 'config.json' which stored in langpath;
contents in config.json like:
{
"threshold": 0.1,
"batchsize": 2,
"workernum": 4,
"maximgsize": 2000,
"allowlist": "",
"langpath": "/home/langdata",
"removechar": " _^~`&"
"txtpath": ""
}
------------------------------------
e.g.
./img2txt.py img1.jpg jmg2.jpg #follow by one or more image files
./img2txt.py ./img1 ./img home/usr/Document/img #follow by one or more directory contain image files
./img2txt.py --help #output the help info
./img2txt.py --config #generate the default config.json file in the langpath
------------------------------------
'''
import sys
import json
import os
import pdf2image
import numpy as np
#------------------默認參數設置----------------------
threshold=0.1 #(default = 0.1)阈值
batchsize=2 # (default = 1) - batch_size>1 will make EasyOCR faster but use more memory
workernum=4 # (default = 0) - Number thread used in of dataloader
maximgsize=2000 #(default = 1000) - Max image width & height when using pdf
allowlist='' # (string) - Force EasyOCR to recognize only subset of characters
removechar='| _^~`&'#待刪除無效字符
txtpath='' #ocr識別後同名txt文件存放的位置:空表示同一目錄,點表示相對目錄,其他表示絕對目錄
#根據系統設置默認的語言包路徑
if sys.platform.lower().startswith('linux'):
langpath='/home/langdata'
elif sys.platform.lower().startswith('win'):
langpath='C:\ocr\langdata'
else:
print('Error: Unknow System!')
sys.exit()
#配置參數字典
config={
"threshold": threshold,
"batchsize": batchsize,
"workernum": workernum,
"maximgsize": maximgsize,
"allowlist": allowlist,
"langpath": langpath,
"removechar": removechar,
"txtpath": txtpath
}
#------------------命令行參數處理----------------------
#首先對輸入的命令行參數進行處理,在加載ocr包之前排查的好處是避免臨處理時出錯白白浪費時間
for i in range(1,len(sys.argv)):#獲取命令行參數:argv[0]表示可執行文件本身
if sys.argv[i] in ['-h', '--help']:
print(strhelp)
sys.exit()
elif sys.argv[i] in ['-c', '--config']:
#保存字典到文件
try:
with open(os.path.join(langpath,'config.json'), 'w') as jsonfile:
json.dump(config, jsonfile, ensure_ascii=False,indent=4)
print('Genrerating config.json success! ---> ', os.path.join(langpath,'config.json'))
except(Exception) as e:
print('\tSaving config file config.json Error: ', e)#輸出異常錯誤
sys.exit()
else:
#check the image file or directory is valid-提前校驗,免得浪費時間加載easyocr模型
if not os.path.exists(sys.argv[i]):
print(sys.argv[i], ' is invalid, please input the correct file or directory path!')
sys.exit()
#檢查語言包路徑是否正確check the langpath is valid
if not os.path.exists(langpath):
print('Error: Invalid langpath! Checking the path again!')
sys.exit()
#判斷是否存在配置文件config.json,存在就使用,格式如下:
configfile=os.path.join(langpath,'config.json')
if os.path.exists(configfile):
try:
with open(configfile, 'r') as jsonfile:
configdict=json.load(jsonfile)
threshold=configdict['threshold']
batchsize=configdict['batchsize']
workernum=configdict['workernum']
maximgsize=configdict['maximgsize']
langpath=configdict['langpath']
allowlist=configdict['allowlist']
removechar=configdict['removechar']
txtpath=configdict['txtpath']
print('using the config in ', configfile)
except(Exception) as e:
print('\tReading config file ', configfile ,' Error: ', e)#輸出異常錯誤
print('\tCheck the json file, or remove the config.json file to use defaulting configs!')
sys.exit()
else:
print('\tusing the default config in ', langpath)
print(configdict)
#如果用戶在config.json中指定的txt文件保存路徑不存在就生成一個
if len(txtpath)>0 and not os.path.exists(txtpath):
print('txtpath in config.json is not exists, generating ', txtpath, '!\n')
os.system('mkdir '+txtpath)
#------------------開始OCR識別----------------------
import easyocr
ocrreader=easyocr.Reader(['ch_sim', 'en'], model_storage_directory=langpath)#Linux: r'/home/langdata', Windows: r'C:\ocr\langdata'
for ind in range(1,len(sys.argv)):#獲取命令行參數:argv[0]表示可執行文件本身
argpath=sys.argv[ind]
#如果是文件...
if os.path.isfile(argpath):
paper=''
#獲取文件後綴名
filext=os.path.splitext(argpath)[-1]
if filext.upper() not in ['.JPG','.JPEG','.PNG','.BMP','.PDF']:#轉換為大寫後再比對
print('\t', argpath, ' 不是有效圖片格式(jpg/jpeg/png/bmp/pdf)!')
continue
if filext.upper() in['.PDF']:#如果是pdf文檔
images=pdf2image.convert_from_path(argpath)#將pdf文檔轉換為圖像序列
for i in range(len(images)):#如果圖片尺寸過大,縮小到特定尺寸,避免內存崩潰
ratio=max(images[i].width, images[i].height)/maximgsize
if ratio>1:
images[i]=images[i].resize((round(images[i].width/ratio),round(images[i].height/ratio)))
result = ocrreader.readtext(np.asarray(images[i]),batch_size=batchsize,workers=workernum)
for w in result:
if w[2]>threshold:#設置一定的置信度阈值
paper = paper+w[1]
else:
result = ocrreader.readtext(argpath,batch_size=batchsize,workers=workernum)
for w in result:
if w[2]>threshold:#設置一定的置信度阈值
paper = paper+w[1]
#print(paper)
for item in removechar:
paper=paper.replace(item, '')
paper=paper.replace('\r', '')
paper=paper.replace('\n', '')
#記錄當前文件的識別結果,保存為同名的txt文件
if(len(txtpath)>0):#如果設置了txt文件目錄
basename=os.path.basename(argpath)+'.txt'#與原文件同名的txt文件(不含目錄僅文件名)
txtfilename=os.path.join(txtpath, basename)
else:
txtfilename=os.path.splitext(argpath)[0]+'.txt'#與原文件同名的txt文件(包括目錄)
print('saving file ---> ', txtfilename)#保存的文件名字
try:
with open(txtfilename, 'w') as txtfile:
txtfile.write(paper)
except(Exception) as e:
print('\t', txtfilename, ' Saving txt File Error: ', e)#輸出異常錯誤
continue
#如果是文件夾...
if os.path.isdir(argpath):
for root, _, filenames in os.walk(argpath):
for imgfile in filenames:
paper=''
filext=os.path.splitext(imgfile)[-1]#文件後綴名
if filext.upper() not in ['.JPG','.JPEG','.PNG','.BMP','.PDF']:
print('\t', imgfile, '的後綴名不是有效的圖像格式,跳過該文件!')
continue
imgfilepath=os.path.join(root, imgfile)#文件絕對路徑
if filext.upper() in['.PDF']:#如果是pdf文檔
images=pdf2image.convert_from_path(imgfilepath)#將pdf文檔轉換為圖像序列
for i in range(len(images)):#如果圖片尺寸過大,縮小到特定尺寸,避免內存崩潰
ratio=max(images[i].width, images[i].height)/maximgsize
if ratio>1:
images[i]=images[i].resize((round(images[i].width/ratio),round(images[i].height/ratio)))
result = ocrreader.readtext(np.asarray(images[i]),batch_size=batchsize,workers=workernum)
for w in result:
if w[2]>threshold:#設置一定的置信度阈值
paper = paper+w[1]
else:
result = ocrreader.readtext(imgfilepath,batch_size=batchsize,workers=workernum)
for w in result:
if w[2]>threshold:#設置一定的置信度阈值
paper = paper+w[1]
#print(paper)
for item in removechar:
paper=paper.replace(item, '')
paper=paper.replace('\r', '')
paper=paper.replace('\n', '')
#記錄當前文件的識別結果,保存為同名的txt文件
basename=os.path.splitext(imgfile)[0]+'.txt'#與原文件同名的txt文件(不包括目錄)
if(len(txtpath)>0):#如果設置了txt文件目錄
txtfilename=os.path.join(txtpath, basename)
else:
txtfilename=os.path.join(root, basename)#需要加上絕對路徑
print('saving file ---> ', txtfilename)#保存的文件名字
try:
with open(txtfilename, 'w') as txtfile:
txtfile.write(paper)
except(Exception) as e:
print('\t', txtfilename, ' Saving txt File Error: ', e)#輸出異常錯誤
continue