DEV Community

Samiul Baree Sifat
Samiul Baree Sifat

Posted on

cv2.rectangle() function error

I am writing a deep learning code that needs to detect very tiny faces. I found an implementation of tiny face detects paper in Github using TensorFlow. The code have to draw rectangles around faces in the image but the open cv cv2.rectangle() function giving TypeError. But I couldn't figure out exactly what is this error, searched the whole net found one or two issues talking about the argument being float is the problem.

This is my code to draw a rectangle in the given image:

    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))
Enter fullscreen mode Exit fullscreen mode

The error it's giving is:

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)
Enter fullscreen mode Exit fullscreen mode

I checked every argument data type which is integer obviously as the solutions I've found issued by others that one of the arguments being float is the problem.

What is the actual problem here? Thanks in advance!

Top comments (1)

Collapse
 
adwaithrajesh profile image
Adwaith Rajesh • Edited

the error says that a function , i dont know which, takes 4 arguments but you only gave 2
that is the error, not any data types error.