(-5:Bad argument) in function 'rectangle' - 无法解析'pt1'。序列中索引为0的项具有错误类型。

20
当我检测我的tflite文件时,出现了问题。
我写的命令是:
python detect.py --weights ./checkpoints/yolov4-tiny-tf.tflite --size 416 --model yolov4 --image D:\yolov4\training\tensorflow-yolov4-tflite-master\data\rice.jpg --framework tflite --tiny true

并且错误的信息:

cv2.rectangle(image, c1, c2, bbox_color, bbox_thick)
cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'rectangle'
> Overload resolution failed:
>  - Can't parse 'pt1'. Sequence item with index 0 has a wrong type
>  - Can't parse 'pt1'. Sequence item with index 0 has a wrong type
>  - Can't parse 'rec'. Expected sequence length 4, got 2
>  - Can't parse 'rec'. Expected sequence length 4, got 2

这里是相关代码(core/utils.py)。

fontScale = 0.5
    score = out_scores[0][i]
    class_ind = int(out_classes[0][i])
    bbox_color = colors[class_ind]
    bbox_thick = int(0.6 * (image_h + image_w) / 600)
    c1, c2 = (coor[1], coor[0]), (coor[3], coor[2])
    cv2.rectangle(image, c1, c2, bbox_color, bbox_thick)

    if show_label:
        bbox_mess = '%s: %.2f' % (classes[class_ind], score)
        t_size = cv2.getTextSize(bbox_mess, 0, fontScale, thickness=bbox_thick // 2)[0]
        c3 = (c1[0] + t_size[0], c1[1] - t_size[1] - 3)
        cv2.rectangle(image, c1, (np.float32(c3[0]), np.float32(c3[1])), bbox_color, -1) #filled

        cv2.putText(image, bbox_mess, (c1[0], np.float32(c1[1] - 2)), cv2.FONT_HERSHEY_SIMPLEX,
                    fontScale, (0, 0, 0), bbox_thick // 2, lineType=cv2.LINE_AA)
return image

我已经将其更改为。
fontScale = 0.5
    score = out_scores[0][i]
    class_ind = int(out_classes[0][i])
    bbox_color = colors[class_ind]
    bbox_thick = int(0.6 * (image_h + image_w) / 600)
    c1, c2 = (int(coor[1]), int(coor[0])), (int(coor[3]), int(coor[2]))
    print(c1, c2, bbox_color, bbox_thick)
    cv2.rectangle(image, (int(coor[1]), int(coor[0])), (int(coor[3]), int(coor[2])), bbox_color, -1)

    if show_label:
        bbox_mess = '%s: %.2f' % (classes[class_ind], score)
        t_size = cv2.getTextSize(bbox_mess, 0, fontScale, thickness=bbox_thick // 2)[0]
        c3 = (c1[0] + t_size[0], c1[1] - t_size[1] - 3)
        cv2.rectangle(image, (int(coor[1]), int(coor[0])), (int(c3[0]), int(c3[1])), (255, 0, 0), -1) #filled

        cv2.putText(image, bbox_mess, (int(c1[0]), int(c1[1] - 2)), cv2.FONT_HERSHEY_SIMPLEX,
                    fontScale, (0, 0, 0), bbox_thick // 2, lineType=cv2.LINE_AA)
return image

由于没有出现错误,它仍然没有显示图像。
    [{'name': 'input_1', 'index': 0, 'shape': array([  1, 416, 416,   3]), 'shape_signature': array([ -1, 416, 416,   3]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]
[{'name': 'Identity', 'index': 232, 'shape': array([   1, 2535,    4]), 'shape_signature': array([ 1, -1,  4]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'Identity_1', 'index': 211, 'shape': array([   1, 2535,    2]), 'shape_signature': array([ 1, -1,  2]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]

最后的消息

有人想到了解决方法吗? 感谢您的帮助!

这是我的文件:https://github.com/piggychu0w0/food-image-detection

3个回答

33
问题在于你将带有浮点数的元组传入函数参数作为点。以下是重现的错误:
import cv2
import numpy as np

img = np.zeros((600, 600), 'uint8')

c1 = 50.2, 12.4
c2 = 88.8, 40.8

cv2.rectangle(img, c1, c2, (255, 0, 0), -1)

输出:

Traceback (most recent call last):
  File "C:/Users/User/Desktop/temp.py", line 9, in <module>
    cv2.rectangle(img, c1, c2, (255, 0, 0), -1)
cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'rectangle'
> Overload resolution failed:
>  - Can't parse 'pt1'. Sequence item with index 0 has a wrong type
>  - Can't parse 'pt1'. Sequence item with index 0 has a wrong type
>  - Can't parse 'rec'. Expected sequence length 4, got 2
>  - Can't parse 'rec'. Expected sequence length 4, got 2

要修复它,只需在坐标周围使用 int() 包装器:

import cv2
import numpy as np

img = np.zeros((600, 600), 'uint8')

c1 = 50.2, 12.4
c2 = 88.8, 40.8

cv2.rectangle(img, (int(c1[0]), int(c1[1])), (int(c2[0]), int(c2[1])), (255, 0, 0), -1)

cv2.rectangle(image, c1, (np.float32(c3[0]), np.float32(c3[1])), bbox_color, -1) #填充cv2.error: OpenCV(4.5.2) :-1: 错误:(-5:参数错误) 函数'rectangle' - CYH
它有新的错误。 - CYH
@CYH 请尝试使用cv2.rectangle(img, (int(c1[0]), int(c1[1])), (int(c2[0]), int(c2[1])), (255, 0, 0), -1) - Ann Zen
仍然出现相同的错误。cv2.rectangle(image, c1, (np.float32(c3[0]), np.float32(c3[1])), bbox_color, -1) #填充 - CYH
我在帖子底部添加了消息。你有相关经验吗?@Ann Zen - CYH
显示剩余5条评论

2

尝试以下更改,对我很有效。

fontScale = 0.5
    score = out_scores[i]
    class_ind = int(out_classes[i])
    bbox_color = colors[class_ind]
    bbox_thick = int(0.6 * (image_h + image_w) / 600)
    c1, c2 = (coor[0], coor[1]), (coor[2], coor[3])
    print(c1, c2, bbox_color, bbox_thick)
    cv2.rectangle(image, (int(c1[0]), int(c1[1])), (int(c2[0]), int(c2[1])), bbox_color, bbox_thick)

    if show_label:
        bbox_mess = '%s: %.2f' % (classes[class_ind], score)
        t_size = cv2.getTextSize(bbox_mess, 0, fontScale, thickness=bbox_thick // 2)[0]
        c3 = (c1[0] + t_size[0], c1[1] - t_size[1] - 3)
        cv2.rectangle(image, (int(c1[0]), int(c1[1])), (int(c3[0]), int(c3[1])), (255, 0, 0), -1) #filled

        cv2.putText(image, bbox_mess, (int(c1[0]), int(c1[1] - 2)), cv2.FONT_HERSHEY_SIMPLEX,
                    fontScale, (0, 0, 0), bbox_thick // 2, lineType=cv2.LINE_AA)
return image

0
n_lines = len(bbox)
    for i in range(n_lines):
        # draw all lines
        point1 = tuple(bbox[i][0])
        point2 = tuple(bbox[(i+1) % n_lines][0])
        cv2.line(img, point1, point2, (255, 0, 0), thickness=2)

请问您能检查一下缩进吗? - Suraj Rao
1
你的回答可以通过添加更多关于代码的信息以及它如何帮助提问者来改进。 - Tyler2P
1
虽然这段代码可能解决了问题,但是包括解释它如何以及为什么解决了问题,将有助于提高您的帖子质量。请记住,您正在回答未来读者的问题,而不仅仅是现在提问的人。请[编辑]您的答案以添加解释,并指出适用的限制和假设。 - user17242583

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接