【发布时间】:2021-08-30 06:32:30
【问题描述】:
我正在编写一个需要检测非常微小的面孔的深度学习代码。我使用 TensorFlow 在Github 中找到了tiny face detects paper 的实现。代码必须在图像中的面部周围绘制矩形,但打开的 cv cv2.rectangle() 函数会给出 TypeError。但是我无法弄清楚这个错误到底是什么,搜索了整个网络发现一两个问题谈论参数是浮动是问题。
这是我在给定图像中绘制矩形的代码:
def overlay_bounding_boxes(raw_img, refined_bboxes, lw):
"""Overlay bounding boxes of face on images.
Args:
raw_img:
A target image.
refined_bboxes:
Bounding boxes of detected faces.
lw:
The line width of bounding boxes. If zero specified,
this is determined based on the confidence of each detection.
Returns:
None.
"""
# Overlay bounding boxes on an image with the color based on the confidence.
for r in refined_bboxes:
_score = expit(r[4])
cm_idx = int(np.ceil(_score * 255))
rect_color = [int(np.ceil(x * 255)) for x in util.cm_data[cm_idx]] # parula
_lw = lw
if lw == 0: # line width of each bounding box is adaptively determined.
bw, bh = r[2] - r[0] + 1, r[3] - r[0] + 1
_lw = 1 if min(bw, bh) <= 20 else max(2, min(3, min(bh / 20, bw / 20)))
_lw = int(np.ceil(_lw * _score))
_r = [int(x) for x in r[:4]]
cv2.rectangle(raw_img, (_r[0], _r[1]), _r[2], _r[3]), rect_color, int(_lw))
它给出的错误是:
Traceback (most recent call last):
File "D:/PYTHON PROJECTS/Digital Attendance System (knn)/Tiny_Faces_in_Tensorflow-master/tiny_face_eval.py", line 242, in <module>
main()
File "D:/PYTHON PROJECTS/Digital Attendance System (knn)/Tiny_Faces_in_Tensorflow-master/tiny_face_eval.py", line 235, in main
evaluate(
File "D:/PYTHON PROJECTS/Digital Attendance System (knn)/Tiny_Faces_in_Tensorflow-master/tiny_face_eval.py", line 200, in evaluate
overlay_bounding_boxes(raw_img, refined_bboxes, lw)
File "D:/PYTHON PROJECTS/Digital Attendance System (knn)/Tiny_Faces_in_Tensorflow-master/tiny_face_eval.py", line 62, in overlay_bounding_boxes
cv2.rectangle(raw_img, (_r[0], _r[1]), (_r[2],_r[3]), rect_color, _lw)
TypeError: function takes exactly 4 arguments (2 given)
我检查了每个明显是整数的参数数据类型,因为我发现其他人发布的解决方案表明其中一个参数是浮点数是问题。
这里的实际问题是什么?提前致谢!
【问题讨论】:
-
Incorrect error message for non-integer points in rectangle draw function: TypeError: function takes exactly 4 arguments (2 given) 很可能,
_lw不是lw != 0的整数,参见。你的if声明。至少,对于每种情况,这是唯一没有被强制转换为int的论点。 -
@HansHirse 我通过
int(value)手动转换函数中的每个值。它仍然给我错误。 -
这没有反映在您的代码中。如果您已更新,请相应地edit your question。
_lw = int(np.ceil(_lw * _score))仅在if lw == 0:内部执行。对于任何其他lw,_lw现在不能保证是整数,因为_lw = lw在上述if语句之前。 -
我更新了the code。谢谢@HansHirse
-
您缺少括号。你有
cv2.rectangle(raw_img, (_r[0], _r[1]), _r[2], _r[3]), rect_color, int(_lw)),它应该是cv2.rectangle(raw_img, (_r[0], _r[1]), (_r[2], _r[3]), rect_color, int(_lw))。每个角点应该是一个 (x,y) 元组。还要确保 x,y 坐标是整数。
标签: python opencv deep-learning