Skip to content
Advertisement

OpenCV(4.5.5) error: (-5:Bad argument) in function ‘putText’

def face_reco(self):
        def draw_boundary(img, classifier, scaleFactor, minNeighbors, color, text, classif):
            gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            features = classifier.detectMultiScale(
                gray_image, scaleFactor, minNeighbors)
            coord = []

            for (x, y, w, h) in features:
                cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 3)
                id, predict = classif.predict(gray_image[y:y+h, x:x+w])
                confidence = int((100*(1-predict/300)))

                conn = mysql.connector.connect(
                    host="localhost", username="root", password="jas@06111507", database="userdetails")
                my_cursor = conn.cursor()

                my_cursor.execute(
                    "Select employeename from employee where employeeid="+str(id))
                n = my_cursor.fetchone()
                n = "+".join(n)
                if confidence > 77:
                    cv2.putText(
                        img, f"NAME:{n}", (x, y, -30), cv2.FONT_HERSHEY_COMPLEX, 0.8, (255, 255, 255), 3)
                else:
                    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 3)
                    cv2.putText(img, "Face cannot be detected", (x, y, -5),
                                cv2.FONT_HERSHEY_COMPLEX, 0.8, (255, 255, 255), 3)
                coord = [x, y, w, h]
                    

Advertisement

Answer

In fact, this is a problem caused by the format of the image itself. There is an incompatibility issue with opencv. I don’t know if it’s a bug. This problem can be solved in two ways.

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

or

img = img.copy()
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement