Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在进行文本检测、识别串联推理时,出现错误 #250

Closed
hsyy673150343 opened this issue Jun 22, 2020 · 17 comments
Closed

在进行文本检测、识别串联推理时,出现错误 #250

hsyy673150343 opened this issue Jun 22, 2020 · 17 comments

Comments

@hsyy673150343
Copy link

输入以下命令:
python3 tools/infer/predict_system.py --image_dir=/home/hs/OCR_Projects/PaddleOCR/train_data/tibetan/text_localization/test_image/img_9.jpg --det_model_dir="./inference/det_db/" --rec_model_dir="./inference/rec_crnn/"
出现以下错误:
Traceback (most recent call last):
File "tools/infer/predict_system.py", line 118, in
dt_boxes, rec_res = text_sys(img)
File "tools/infer/predict_system.py", line 81, in call
rec_res, elapse = self.text_recognizer(img_crop_list)
File "/home/hs/OCR_Projects/PaddleOCR/tools/infer/predict_rec.py", line 101, in call
preds_text = self.char_ops.decode(rec_idx_tmp)
File "tools/infer/../../ppocr/utils/character.py", line 97, in decode
char_list.append(self.character[text_index[idx]])
IndexError: list index out of range

@tink2123
Copy link
Collaborator

应该是因为预测模型使用的是中文模型,解码时指定了英文的字符解码方式。请问您使用的是哪一个识别模型呢?

@hsyy673150343
Copy link
Author

hsyy673150343 commented Jun 22, 2020 via email

@tink2123
Copy link
Collaborator

是中文模型吗?请试试预测时重新指定下解码方式:
python3 tools/infer/predict_system.py --image_dir=/home/hs/OCR_Projects/PaddleOCR/train_data/tibetan/text_localization/test_image/img_9.jpg --det_model_dir="./inference/det_db/" --rec_char_type='ch'

@hsyy673150343
Copy link
Author

是中文模型吗?请试试预测时重新指定下解码方式:
python3 tools/infer/predict_system.py --image_dir=/home/hs/OCR_Projects/PaddleOCR/train_data/tibetan/text_localization/test_image/img_9.jpg --det_model_dir="./inference/det_db/" --rec_char_type='ch'

python3 tools/infer/predict_system.py --image_dir=./doc/imgs/img_9.jpg --det_model_dir="./inference/det_db/" --rec_model_dir="./inference/rec_crnn/" --rec_char_type='ch'
还是会出现错误:
Traceback (most recent call last):
File "tools/infer/predict_system.py", line 118, in
dt_boxes, rec_res = text_sys(img)
File "tools/infer/predict_system.py", line 81, in call
rec_res, elapse = self.text_recognizer(img_crop_list)
File "/home/hs/OCR_Projects/PaddleOCR/tools/infer/predict_rec.py", line 102, in call
preds_text = self.char_ops.decode(rec_idx_tmp)
File "tools/infer/../../ppocr/utils/character.py", line 96, in decode
char_list.append(self.character[text_index[idx]])
IndexError: list index out of range

@tink2123
Copy link
Collaborator

打印下解码时的字典个数和预测的idx看一下呢?

char_num = self.get_char_num()

在81行后:print(char_num)

char_list.append(self.character[text_index[idx]])

96行前:print("text_index:{}, idx:{}".format(text_index,idx))

@hsyy673150343
Copy link
Author

print("text_index:{}, idx:{}".format(text_index,idx))

char_num: 6623
text_index:[82262 81698 82095 0 82262 82249 81770 81698 82262 81698 82262 81698
82009 81698 82262 82149 81698], idx:0

@tink2123
Copy link
Collaborator

inference model 是从官方下载的吗?看起来预测结果有误,中文dict共有6623类,但是预测出了索引值都在8w+

@hsyy673150343
Copy link
Author

我是自己训练的模型然后按照文档里的说明转换成的inference model!
我的中文字典也是自定义的,我在rec_icdar15_train.yml中也指定了character_dict_path
但从char_num: 6623可以看出,程序还是用的自带的ppcor_keys_v1.txt

@tink2123
Copy link
Collaborator

如果使用了自己的字典文件,在使用system预测时,需要修改字典路径,预测时添加: --rec_char_dict_path="{your text dict path}"
图片

@hsyy673150343
Copy link
Author

谢谢您的耐心解答,我还有个问题。同一张图片,为什么我单独进行检测的时候能正常检测出文字,而在进行文本检测、识别串联的时候连最基本的检测都检测不出来?

@tink2123
Copy link
Collaborator

您的图片是什么样子的呢,方便发一张样图给我们看下吗?

连最基本的检测都检测不出来

是指没有检测结果,还是检测结果错误呢

@tink2123
Copy link
Collaborator

tink2123 commented Jun 22, 2020

可能是因为识别得分低于0.5,置信度过低没有返回结果。可以把这个地方的阈值设置为0.0 看是否能显示出结果:

image, boxes, txts, scores, draw_txt=True, drop_score=0.5)

@hsyy673150343
Copy link
Author

可能是因为识别得分低于0.5,置信度过低没有返回结果。可以把这个地方的阈值设置为0.0 看是否能显示出结果:

image, boxes, txts, scores, draw_txt=True, drop_score=0.5)

改了之后报错啦:
Predict time of ./doc/imgs_words/ch/img_9.jpg: 1.049s
Traceback (most recent call last):
File "tools/infer/predict_system.py", line 137, in
image, boxes, txts, scores, draw_txt=True, drop_score=0)
File "tools/infer/../../tools/infer/utility.py", line 154, in draw_ocr
txts, scores, img_h=img.shape[0], img_w=600, threshold=drop_score)
File "tools/infer/../../tools/infer/utility.py", line 241, in text_visual
draw_txt.text((0, gap * count), new_txt, txt_color, font=font)
File "/home/hs/.local/lib/python3.6/site-packages/PIL/ImageDraw.py", line 351, in text
draw_text(ink)
File "/home/hs/.local/lib/python3.6/site-packages/PIL/ImageDraw.py", line 316, in draw_text
**kwargs,
File "/home/hs/.local/lib/python3.6/site-packages/PIL/ImageFont.py", line 471, in getmask2
size, offset = self.font.getsize(text, direction, features, language)
OSError: invalid face handle
段错误 (核心已转储)

@LDOUBLEV
Copy link
Collaborator

LDOUBLEV commented Jun 22, 2020

可能是因为识别得分低于0.5,置信度过低没有返回结果。可以把这个地方的阈值设置为0.0 看是否能显示出结果:

image, boxes, txts, scores, draw_txt=True, drop_score=0.5)

改了之后报错啦:
Predict time of ./doc/imgs_words/ch/img_9.jpg: 1.049s
Traceback (most recent call last):
File "tools/infer/predict_system.py", line 137, in
image, boxes, txts, scores, draw_txt=True, drop_score=0)
File "tools/infer/../../tools/infer/utility.py", line 154, in draw_ocr
txts, scores, img_h=img.shape[0], img_w=600, threshold=drop_score)
File "tools/infer/../../tools/infer/utility.py", line 241, in text_visual
draw_txt.text((0, gap * count), new_txt, txt_color, font=font)
File "/home/hs/.local/lib/python3.6/site-packages/PIL/ImageDraw.py", line 351, in text
draw_text(ink)
File "/home/hs/.local/lib/python3.6/site-packages/PIL/ImageDraw.py", line 316, in draw_text
**kwargs,
File "/home/hs/.local/lib/python3.6/site-packages/PIL/ImageFont.py", line 471, in getmask2
size, offset = self.font.getsize(text, direction, features, language)
OSError: invalid face handle
段错误 (核心已转储)

你是只测试识别模型?imgs_words/ch下的是识别数据,高度太小。测试检测用/doc/imgs下的图像试试??

@hsyy673150343
Copy link
Author

可能是因为识别得分低于0.5,置信度过低没有返回结果。可以把这个地方的阈值设置为0.0 看是否能显示出结果:

image, boxes, txts, scores, draw_txt=True, drop_score=0.5)

改了之后报错啦:
Predict time of ./doc/imgs_words/ch/img_9.jpg: 1.049s
Traceback (most recent call last):
File "tools/infer/predict_system.py", line 137, in
image, boxes, txts, scores, draw_txt=True, drop_score=0)
File "tools/infer/../../tools/infer/utility.py", line 154, in draw_ocr
txts, scores, img_h=img.shape[0], img_w=600, threshold=drop_score)
File "tools/infer/../../tools/infer/utility.py", line 241, in text_visual
draw_txt.text((0, gap * count), new_txt, txt_color, font=font)
File "/home/hs/.local/lib/python3.6/site-packages/PIL/ImageDraw.py", line 351, in text
draw_text(ink)
File "/home/hs/.local/lib/python3.6/site-packages/PIL/ImageDraw.py", line 316, in draw_text
**kwargs,
File "/home/hs/.local/lib/python3.6/site-packages/PIL/ImageFont.py", line 471, in getmask2
size, offset = self.font.getsize(text, direction, features, language)
OSError: invalid face handle
段错误 (核心已转储)

你是只测试识别模型?imgs_words/ch下的是识别数据,高度太小。测试检测用/doc/imgs下的图像试试??

测试的是检测和识别串联推理,我用的测试命令如下:
python3 tools/infer/predict_system.py --image_dir=./doc/de_rec_test_img/img_9.jpg --det_model_dir="./inference/det_db/" --rec_model_dir="./inference/rec_crnn/" --rec_char_type="ch" --rec_char_dict_path="./ppocr/utils/all_tibetan_word.txt"

测试的图片是我自己收集的数据集,图片大小为608*608

报错信息为:
Traceback (most recent call last):
File "tools/infer/predict_system.py", line 139, in
image, boxes, txts, scores, draw_txt=True, drop_score=0.0)
File "tools/infer/../../tools/infer/utility.py", line 154, in draw_ocr
txts, scores, img_h=img.shape[0], img_w=600, threshold=drop_score)
File "tools/infer/../../tools/infer/utility.py", line 241, in text_visual
draw_txt.text((0, gap * count), new_txt, txt_color, font=font)
File "/home/hs/.local/lib/python3.6/site-packages/PIL/ImageDraw.py", line 351, in text
draw_text(ink)
File "/home/hs/.local/lib/python3.6/site-packages/PIL/ImageDraw.py", line 316, in draw_text
**kwargs,
File "/home/hs/.local/lib/python3.6/site-packages/PIL/ImageFont.py", line 471, in getmask2
size, offset = self.font.getsize(text, direction, features, language)
OSError: invalid face handle
段错误 (核心已转储)

@tink2123
Copy link
Collaborator

报错说明返回了检测框,在画图了。这好像是Pillow库的bug,可能因为您的字典中有特殊字符?可以参考这个issue尝试重新安装编译:python-pillow/Pillow#3066

或者跳过draw_text函数: draw_txt=False

new_img = draw_ocr(image, boxes, txts, scores, draw_txt=True)

返回结果可以通过修改阈值,在终端打印出来,score>=0.0 :

if score >= 0.5:

@hsyy673150343
Copy link
Author

报错说明返回了检测框,在画图了。这好像是Pillow库的bug,可能因为您的字典中有特殊字符?可以参考这个issue尝试重新安装编译:python-pillow/Pillow#3066

或者跳过draw_text函数: draw_txt=False

new_img = draw_ocr(image, boxes, txts, scores, draw_txt=True)

返回结果可以通过修改阈值,在终端打印出来,score>=0.0 :

if score >= 0.5:

谢谢您的耐心解答~!我的问题解决了。
我首先卸载了pillow----》pip uninstall pillow==7.1.2
然后将score改为0
最后使用conda重新下载了pillow----》conda install pillow==7.1.2
这个bug就没有了

an1018 pushed a commit to an1018/PaddleOCR that referenced this issue Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants