Tesseract対テーブル。文書の認識

すべての人のデジタル化にもかかわらず、人類がニューロインターフェースを作成しようとしているとき、AIが一般的になったとき、スキャン/画像からデータを取得するという古典的なタスクは依然として適切です。





良い一日。私の名前はアレクセイです。私は機器を販売する会社でプログラマーとして働いています。私には、データを認識して会計プログラムにロードするための独自のベストプラクティスがあり、EDFに簡単に転送できない数十ページのPDFドキュメントを手動で入力したのはマネージャーだけでした。私は彼らに私の解決策を試すように勧めました。





当初、ABBYY Cloudが認識に使用されていましたが、無料ではなく、トライアルモードは十分な長さではありません。私は、無料のtesseractaのすべての機能が使用されるPythonでAPIを作成することにしました。問題は、正八胞体がテキスト認識であり、テーブルを定義していないことです。それはほとんど役に立たないことがわかります。記事を読む前日https://vc.ru/ml/139816-povyshenie-kachestva-raspoznavaniya-skanov-dokumentov-s-tablicami-s-pomoshchyu-vychisleniya-koordinat-yacheekここですべてのテーブルセルが取得されますopenCVを使用すると、各セルが正八胞体を通過するため、正しいデータを取得できます。私はこの方法を試すことにしました。投稿は何が起こったのかについてです。





テストでは、デモベースの1cTORG-12から取得しました。このフォームは、かなり複雑な構造、多くのテーブル、多くのテキスト、多くのデータを持っています。必要なものだけ。





pdf , gostscript . ImageMagick, - . cmd , gostscript .





, openCV , QR-. pyzbar.





, . , . , , . - .





clahe = cv2.createCLAHE(clipLimit=50, tileGridSize=(50, 50))
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)  
l, a, b = cv2.split(lab) 
l2 = clahe.apply(l)  
lab = cv2.merge((l2, a, b))  
img2 = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR) 

gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 75, 255, cv2.THRESH_BINARY_INV )

kernel = np.ones((2, 2), np.uint8)
obr_img = cv2.erode(thresh, kernel, iterations=1)

obr_img = cv2.GaussianBlur(obr_img, (3,3), 0)
      
      



, . , . 5 , delta.





contours, hierarchy = cv2.findContours(obr_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_L1)
coordinates = []
ogr = round(max(img.shape[0], img.shape[1]) * 0.005)
delta = round(ogr/2 +0.5)
ind = 1;
for i in range(0, len(contours)):
	l, t, w, h = cv2.boundingRect(contours[i])
	if (h > ogr and w > ogr):
    # 
    # 
    # 
    #
    #
    #
    #
    #
  	coordinates.append((0, ind, 0, l, t, w, h, ''))
    ind = ind + 1
      
      



, . sqlite3 coordinates. . , hierarchy, , . .





, . - . , .





, , - . , , . . , , . , , , , , , .





2 :













. , . , . . , , , , . - . . , , . . . . / 2*. . , - , . .





. . 4 . , , "". "" , , . - , 4 , .









. . tesseract , 3 , . , "-". "-", "---00", . .





text1 = pytesseract.image_to_string(image[t1:t2,l1:l2], lang=lang, config='--psm 6')
text2 = pytesseract.image_to_string(image[t1:t2,l1:l2], lang=lang, config='')
text3 = pytesseract.image_to_string(image[t1+round(delta/2):t2-round(delta/2),l1+round(delta/2):l2-round(delta/2)], lang=lang, config='--psm 7')
text1 = text1.replace("\n", " ")
text2 = text2.replace("\n", " ")
text3 = text3.replace("\n", " ")
text1 = re.sub(' *[^ \(\)--\d\w\/\\\.\-,:; ]+ *', ' ', text1)
text2 = re.sub(' *[^ \(\)--\d\w\/\\\.\-,:; ]+ *', ' ', text2)
text3 = re.sub(' *[^ \(\)--\d\w\/\\\.\-,:; ]+ *', ' ', text3)

while text1.find('  ')!=-1:
    text1 = text1.replace('  ',' ')
while text2.find('  ') != -1:
    text2 = text2.replace('  ', ' ')
while text3.find('  ') != -1:
    text3 = text3.replace('  ', ' ')
      
      



. , . , . , -, , , . , , . , , , . , , ? , 2 , . . , , , . , ; ; , . .





. . 4 . "". , , . .





, , . , . API JSON, 1 . , . . . 1 pdf 20 , . , Tesserocr Pytesseract, .





https://github.com/Trim891/API. PyCharm "", GitHub, *.py requirements.txt. , , , , , ; " , ", , , - ; , , 2 . .





PSファイルにはたくさんのコメントがあり、不要なものがたくさんあります。一般的に、たわごとのコードは創造的な混乱です。それはすべて内部使用のためであり、ドレスアップする時間はありませんでした=)








All Articles