PaddleOCR icon indicating copy to clipboard operation
PaddleOCR copied to clipboard

ValueError when use det only / 单独执行检测时报错 ValueError

Open KumaTea opened this issue 11 months ago • 5 comments

问题

使用 PaddleOCR 单独执行检测时,报错 ValueError

import numpy as np
from paddleocr import PaddleOCR


ppocr = PaddleOCR(lang='ch', use_angle_cls=False,)

image: np.ndarray = ...
results = ppocr.ocr(image, det=True, rec=False, cls=False)
print(results)

报错如下:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\ProgramData\conda\envs\gsds\Lib\site-packages\paddleocr\paddleocr.py", line 674, in ocr
    if not dt_boxes:
           ^^^^^^^^
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

出错位置

paddleocr.py#L681

其中,dt_boxes 数据示例

dt_boxes = np.array([[
    [ 515.,    4.],
    [1127.,    4.],
    [1127.,   41.],
    [ 515.,   41.]
]], dtype=np.float32)

print(f'{dt_boxes.all()=}')
print(f'{dt_boxes.any()=}')
dt_boxes.all()=True
dt_boxes.any()=True

临时解决方案

~if not dt_boxes: 改为 if not dt_boxes.all():~

更正:dt_boxes 为 None 时会报错

if not dt_boxes: 改为 if dt_boxes is None or not dt_boxes.all():


  • 系统环境/System Environment:Windows 11 x64 23H2 / Python 3.12.2
  • 版本号/Version:Paddle:v2.6.0 PaddleOCR:v2.7.0.3 问题相关组件/Related components:paddleocr.PaddleOCR.ocr
  • 运行指令/Command Code:ocr.ocr(image, det=True, rec=False, cls=False)
  • 完整报错/Complete Error Message:ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

KumaTea avatar Mar 18 '24 08:03 KumaTea

dygraph 分支好像也是有类似的问题

paddleocr --image_dir ./doc/imgs/11.jpg --use_angle_cls False --use_gpu false --det True --rec False
[2024/03/19 10:23:16] ppocr INFO: **********./doc/imgs/11.jpg**********
[2024/03/19 10:23:16] ppocr INFO: [[27.0, 458.0], [137.0, 458.0], [137.0, 479.0], [27.0, 479.0]]
Traceback (most recent call last):
  File "/Users/wangxin/miniconda3/envs/ppocr-dev/bin/paddleocr", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/wangxin/miniconda3/envs/ppocr-dev/lib/python3.11/site-packages/paddleocr/paddleocr.py", line 825, in main
    val += str(box[0]) + ',' + str(box[1]) + ','
               ~~~^^^
TypeError: 'float' object is not subscriptable
(ppocr-dev) 

GreatV avatar Mar 19 '24 02:03 GreatV

@KumaTea 你可以提一个PR,修复这个问题。

GreatV avatar Mar 19 '24 02:03 GreatV

该问题数月前已在 commit 42d2809 修复,只是没有同步到主分支,故关闭

KumaTea avatar Mar 23 '24 10:03 KumaTea

Reopen as the fix still not merged in the latest version (v2.7.3)...

KumaTea avatar Apr 01 '24 20:04 KumaTea

你好感谢提出问题,我们会记录并进行修复

UserWangZz avatar Apr 30 '24 02:04 UserWangZz

已经在PR #11287 中修复,且merged

SWHL avatar Jun 10 '24 07:06 SWHL

不仅仅是这一个判断的问题,det only里面bboxes整个是有问题的。真不知道为啥要写一个这么复杂的函数

alixunxing avatar Jun 24 '24 13:06 alixunxing

已经把ppocr拆出来重写了

alixunxing avatar Jun 24 '24 13:06 alixunxing