How to produce confidence score using Haar cascade

asked 2022-09-02 14:49:17 -0600

rexrapier gravatar image

updated 2022-09-03 21:58:49 -0600

ravijoshi gravatar image

I am new to ROS and python programming. I am doing face detection using haar cascade in python. It works fine, except for the confidence score, which shows a constant value of 6.9. Please help me.

The program code is shown below. I have tried tweaking the scale factor values, minimum neighbours, and flag from CASCADE_SCALE_IMAGE to HAAR_BIG_OBJECT. Yet the confidence value remains the same and doesn't change with distance. I have also tried doing it with "haarcascade_frontalface_default.xml" and "haarcascade_frontalface_alt.xml" but with no success.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cv2
import rospy

from sensor_msgs.msg import Image
# from std_msgs.msg import String

from cv_bridge import CvBridge, CvBridgeError

class face_detector:

    def __init__(self):
        self.image_sub = rospy.Subscriber("/xtion/rgb/image_raw", Image, self.callback)
        self.bridge = CvBridge()
        self.image_pub = rospy.Publisher("face_found", Image, queue_size=1)

    def callback(self, rgb_data):

            cv_image = self.bridge.imgmsg_to_cv2(rgb_data, desired_encoding="bgr8")

            # Trained xml file to detect face with its path
            # initialize the face recognizer
            face_cascade = cv2.CascadeClassifier("/home/rexrapier/tiago_public_ws/src/face_detector_py/scripts/haarcascade_frontalface_default.xml")

            # converting to grayscale
            image_gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)

            # detect all the faces in the image
            [faces, neighbours, weights] = face_cascade.detectMultiScale3(image_gray, scaleFactor=1.3, minNeighbors=5 ,flags=cv2.CASCADE_SCALE_IMAGE , minSize=(10,10), outputRejectLevels = True)

            # for every face, draw a blue rectangle
            for (x, y, width, height) in faces:
                cv2.rectangle(cv_image, (x, y), (x + width, y + height), color=(255, 0, 0), thickness=2)
                roi_gray = image_gray[y:y + height, x:x + width]
                roi_color = cv_image[y:y + height, x:x + width]
                face_coords = [x, y]

            for conf in weights:

        except CvBridgeError as e:

        #convert opencv format back to ros format and publish result
            faces_message = self.bridge.cv2_to_imgmsg(cv_image, "bgr8")
        except CvBridgeError as e:

    #to display image in a window
    #cv2.imshow("image", image)
    #if cv2.waitKey() == ord("q"):

def main():
    rospy.init_node('face_detector_node', anonymous = True)

    except KeyboardInterrupt:
        rospy.loginfo("Shutting down")


if __name__ == '__main__':
edit retag flag offensive close merge delete


The problem is unrelated to ROS and Python as well. I recommend going through OpenCV's Cascade Classifier tutorial. On the other hand, is it necessary to re-initialize the cascade classifier for every image? I think you can do it in the class constructor as self.face_cascade = cv2.CascadeClassifier("/home/rexrapier/tiago_public_ws/src/face_detector_py/scripts/haarcascade_frontalface_default.xml")

ravijoshi gravatar image ravijoshi  ( 2022-09-03 22:03:08 -0600 )edit

I already went through the cascade classifier tutorial but it doesn't help much with confidence score. and btw thanks for the suggestion, I've rearranged the code. but any idea with the confidence score please let me know...

rexrapier gravatar image rexrapier  ( 2022-09-04 18:34:20 -0600 )edit