指定した画像から文字を抽出して、文字認識を行いたいです。
(不必要なimportが混じっていますが、ご容赦願います)
書いているコードだと[tools]が空のため、[No OCR tool found]に繋がってしまいます。
どうすれば、文字抽出に進むことができるでしょうか。
【結果】
python
11 2No OCR tool found
【コード】
python
1 picno = 1 2 for i in range(5): 3 event_pic = testfld + '/Cut/988-3_no' + str(picno) + '.png' 4 print(picno) 5 6 #====================================== 7 # グレースケール 8 mov_in = cv2.imread(event_pic, cv2.IMREAD_GRAYSCALE) # 画像ファイrの読み込み 9 if mov_in is None: 10 print('ファイルが読み込めません') 11 import sys 12 sys.exit() 13 14 cv2.imwrite(testfld + '/mov_in_gray.png', mov_in) 15 cv2.waitKey(0) 16 17 #====================================== 18 # 2値化 19 img_gry = (testfld + '/mov_in_gray.png') 20 org_img = cv2.imread(img_gry, 0) 21 22 THRESHOLD = 215 # 閾値はNumpyで調べた 23 MAXVALUE = 255 # 255 = white 24 25 _, bin_cv2 = cv2.threshold(org_img, THRESHOLD, MAXVALUE, cv2.THRESH_BINARY_INV) 26 # [_INV]で背景白、黒文字にすることができる(Tesseractで読める絶対条件!) 27 28 bin_npy = np.zeros(org_img.shape, org_img.dtype) 29 bin_npy[np.where(org_img > THRESHOLD)] = MAXVALUE 30 31 cv2.imwrite(testfld + '/binary_no' +str(picno)+ '.png', bin_cv2) 32 33 #====================================== 34 # 文字認識 35 tools = pyocr.get_available_tools() 36 if len(tools) == 0: 37 print('No OCR tool found') 38 sys.exit() 39 tool = tools[0] 40## print("Will use tool '%s'" % (tool.get_name())) 41 42 langs = tool.get_available_languages() 43## print("Availble languages: %s" % ", ".join(langs)) 44 # 使用可能言語の確認なので、コメントアウト中 45 # [langs]に入れる略記の確認には使える 46 47 txt = tool.image_to_string( 48 Image.open(testfld + '/binary_no' +str(picno)+ '.png'), 49 lang = 'eng', 50 builder = pyocr.builders.TextBuilder() 51 ) 52 print(txt) 53 print('----------------------------') 54 55 if picno == 1: 56 ev01 = txt 57 elif picno == 2: 58 ev02 = txt 59 elif picno == 3: 60 ev03 = txt 61 elif picno == 4: 62 ev04 = txt 63 elif picno == 5: 64 ev05 = txt 65 66 picno = picno + 1 67 68 print('----------------------------') 69 print('Event5回取得') 70 print('----------------------------') 71 72 text = [ev01, ev02, ev03, ev04, ev05]
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。