【bug】paddleocr draw_ocr_box_txt ValueError: incorrect coordinate type

环境

python 3.10.15

pillow                10.4.0
paddleocr             2.8.1

错误详情

错误文本

Traceback (most recent call last):
  ....
    draw_left.polygon(box, fill=color)
  
ValueError: incorrect coordinate type

原因:是由于Pillow库的polygon()函数的xy参数(就是上面的box变量)有类型要求,一定要list(tuple)类型,如果是三角形,坐标例子[(50.0, 50), (150, 50), (100, 150)]

因此,这个错误是输入坐标格式导致的错误。

解决方法

paddleocr 识别得到的每个box坐标进行变换后再输入draw_ocr_box_txt 函数,

单个box坐标变换如下:

# [[819.0, 56.0], [894.0, 56.0], [894.0, 104.0], [819.0, 104.0]] -> [(819.0, 56.0), (894.0, 56.0), (894.0, 104.0), (819.0, 104.0)] 

box = [[819.0, 56.0], [894.0, 56.0], [894.0, 104.0], [819.0, 104.0]]
box = list(map(tuple, box))

【增】polygon函数测试例子

from PIL import Image, ImageDraw

# 创建一个新的白色背景图像
img_left = Image.new('RGB', (400, 300), 'white')

# 创建一个可以在img_left上绘图的对象
draw_left = ImageDraw.Draw(img_left)

# 定义多边形的顶点坐标,这里以一个三角形为例
# 顶点坐标需要是一个列表,其中包含(x, y)元组
t2 = [(50, 50), (150, 50), (100, 150)]

# 定义填充颜色,这里使用红色
color = 'red'

# 使用定义的顶点和颜色绘制多边形
draw_left.polygon(t2, fill=color)

# 显示图像
img_left.show()

# 如果需要,可以保存图像
img_left.save('polygon_image.png')

图片结果

在这里插入图片描述

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部