sajjjadayobi / FaceLib

Face Analysis: Detection, Age Gender Estimation & Recognition
MIT License
294 stars 52 forks source link

Coordinates for cv2.rectangle() need to be casted to int. #21

Closed datamove closed 2 years ago

datamove commented 2 years ago

Dear developers,

I'v got a problem running the test notebook with the below package (just using pip install -r requirements.txt):

opencv-python 4.5.5.62

, and was able to fix them with small patch to facelib/InsightFace/models/utils.py, casting coordinates to int explicitly:

diff --git a/facelib/InsightFace/models/utils.py b/facelib/InsightFace/models/utils.py
index ec929a0..764ae63 100644
--- a/facelib/InsightFace/models/utils.py
+++ b/facelib/InsightFace/models/utils.py
@@ -112,7 +112,7 @@ def special_draw(img, box, landmarsk, name, score=100):
     """draw a bounding box on image"""
     color = (148, 133, 0)
     tl = round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1  # line thickness
-    c1, c2 = (box[0], box[1]), (box[2], box[3])
+    c1, c2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3]))
     # draw bounding box
     cv2.rectangle(img, c1, c2, color, thickness=tl)
     # draw landmark
@@ -123,7 +123,7 @@ def special_draw(img, box, landmarsk, name, score=100):
     score = 0 if score < 0 else score
     bar = (box[3] + 2) - (box[1] - 2)
     score_final = bar - (score*bar/100)
-    cv2.rectangle(img, (box[2] + 1, box[1] - 2 + score_final), (box[2] + (tl+5), box[3] + 2), color, -1)
+    cv2.rectangle(img, (int(box[2] + 1), int(box[1] - 2 + score_final)), (int(box[2] + (tl+5)), int(box[3] + 2)), color, -1)
     # draw label
     tf = max(tl - 1, 1)  # font thickness
     t_size = cv2.getTextSize(name, 0, fontScale=tl / 3, thickness=tf)[0]

This has been tested on Windows 10 and Ubuntu 18.04. Please let me me know if PR is welcome for this fix.

Thanks!

sajjjadayobi commented 2 years ago

Hi, I'll appreciate your PR