質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

3242閲覧

PythonでOCRを行う際にOpenCVが実行できません

nyumonsya

総合スコア34

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/06/13 18:29

前提・実現したいこと

ご教示を頂きたいですm(_ _)m
PythonでOCRを実行した際に、読み込んだ画像のどの部分を参照出来たかを、OpenCVで枠で囲んで示したいです。

しかしコードを実行すると、以下のエラーメッセージが・・・
後述のソースコードの、「★ ★ ★ ★ ★」の行以下が、問題が発生しているブロックです。
ここを丸ごと削除すると、問題無く完了します。

発生している問題・エラーメッセージ

Traceback (most recent call last): File "C:\python\16b_OCR.py", line 189, in <module> print(d.content) AttributeError: 'str' object has no attribute 'content'

該当のソースコード

#-*- encoding: utf-8 -*- from __future__ import print_function # coding: utf-8 #!/usr/bin/env python import os,matplotlib,re,MeCab,itertools,pydotplus,requests,io,string,cv2,json,re from PIL import Image import sys,pyocr,os,codecs,io import pyocr.builders import pytesseract from pdfminer.pdfinterp import PDFResourceManager from pdfminer.pdfinterp import PDFPageInterpreter from pdfminer.layout import LAParams, LTTextBox, LTTextLine, LTImage, LTFigure from pdfminer.converter import PDFPageAggregator from PIL import ImageOps from pdf2image import convert_from_path, convert_from_bytes path = u"C:/python/" tools = pyocr.get_available_tools() if len(tools) == 0: print("No OCR tool found") sys.exit(1) tool = tools[0] path = u"test11" images = convert_from_path(path + ".pdf") images[0].save(path + ".png", 'png') # OCR txt = tool.image_to_string( # 言語,オプションを指定する Image.open(path + ".png"), lang='jpn+Eng', builder=pyocr.builders.TextBuilder(tesseract_layout=3), ) # 分かち書きのスペースを削除 txt = re.sub(r'\s+', '', txt) # 出力ファイル名設定 filename = os.path.basename(path) filename = os.path.splitext(filename)[0] outname = "OCR_" + filename + ".txt" f = codecs.open(outname, 'w',"utf-8") f.write(txt) f.close() print(txt) #★ ★ ★ ★ ★ ★ ★ ★ ★ ★ out = cv2.imread(path + ".png") for d in txt: print(d.content) print(d.position) cv2.rectangle(out, d.position[0], d.position[1], (0, 0, 255), 2) cv2.imshow("img",out) cv2.waitKey(0) cv2.destroyALLWindows()

試したこと

検索では解決出来ず、該当部分をコメントアウトした所、今度は以下のエラーが発生してしまい自分には無理と判断しました・・

Traceback (most recent call last): File "C:\python\16b_OCR.py", line 190, in <module> print(d.position) AttributeError: 'str' object has no attribute 'position'

以上です。
どなた様か、お救いくださると幸いです。・・!!m(_ _)m

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

tool.image_to_string に渡すオプションの builder が pyocr.builders.TextBuilder() になっているため、string型のオブジェクトが返ってきます。
これは通常のstring型なので、今回のエラーの通り contentpositionなどのプロパティは持っていません。
代わりに pyocr.builders.WordBoxBuilder() を渡すことで、boxオブジェクトが返されます。

[参考]
https://riptutorial.com/python/example/28811/pyocr
https://qiita.com/nabechi6011/items/3a367ca94dbd208efcc7

投稿2020/06/14 00:37

Kapustin

総合スコア1186

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

nyumonsya

2020/06/14 14:42

Kapustin様 ご教示を頂きありがとうございます!!!!! お蔭様で、当問題は解決出来ました!!! 本当にありがとうございました!m(_ _)m
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問