【问题标题】:cv2.rectangle() TypeError: function takes exactly 4 arguments (2 given)cv2.rectangle() TypeError: 函数只需要 4 个参数(给定 2 个)
【发布时间】: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


【解决方案1】:

我得到了解决方案。 我检查了一下,问题出在坐标上,即使它是由int(x) 投射的。我找到了另一种使用np.round().astype() 方法将它们转换为整数的方法。

所以,我将 for 循环行更改为 _r = [np.round(x).astype("int") for x in r[:4]]

这似乎解决了我的问题。另外,我在另一个StackOverflow answer找到了解决方案

【讨论】:

    猜你喜欢
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    相关资源
    最近更新 更多