解决yolo检测时报错:TypeError: function takes exactly 1 argument (3 given)

前段时间迁移yolo框架做一些检测的应用,因为自己的图像的灰度图,大小是512X512,训练的时候都正常,但在批量测试时报错。先把报错情况贴出来:

报错说PIL库中的函数只接收到一个参数,应该给三个,自己在这里记录下解决方法,出错的地方在yolo.py中,在yolo中在测试时需要对检测到的区域进行画出标记框和类别数字,因为作者测试的coco等图库都是RGB图像,会有三个参数输入给rectangle函数,不会发生报错,而在测试图像为灰度图时,就会出错。在解决错误是参考了参考文献[1]中的提示,很感谢!

对于这个错误原因,个人认为是这个函数在图像上绘制矩形框时,要求输入的图像的颜色空间维度要和矩形框颜色维度一致,比如:我们输入的灰度图(不做维度扩充),只能使用灰度进行画矩形框;输入的是RGB三色图,可以使用RGB颜色画矩形框。

解决方法:

1.对灰度图进行转换,

使用 image = image.convert('RGB')

比如部分yolo.py函数如下:

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            # 对灰度图像进行RGB转换,输入的原图为(0)一维 转为(0,0,0)三维
            image = image.convert('RGB')
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle(
                    [left + i, top + i, right - i, bottom - i],
                    outline=self.colors[c])
                    #outline=(255))
            draw.rectangle(
                [tuple(text_origin), tuple(text_origin + label_size)],
                fill=self.colors[c])
                #outline=(255))
            #draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            draw.text(text_origin, label, fill=(0), font=font)
            del draw

第二种解决办法

我们不对灰度图进行转换以增加维度,而是绘图时只用也是一维的标记框标记缺陷区域,修改如下:

        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            #image = image.convert('RGB') 
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle(
                    [left + i, top + i, right - i, bottom - i],
                    #outline=self.colors[c])
                    outline=(255)) #将标记框颜色固定成255,即白色
            draw.rectangle(
                [tuple(text_origin), tuple(text_origin + label_size)],
                #fill=self.colors[c])
                outline=(255)) #将颜色固定成255,即白色
            #draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            draw.text(text_origin, label, fill=(0), font=font)
            del draw

但是这样的话,检测结果上标记框很丑。 

 参考资料

[1] https://stackoverflow.com/questions/39080087/pillow-strange-behavior-using-draw-rectangle

 

  • 23
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值