This blog will guide you through creating a face counting system using Haarcascade and YOLOv3. The system reads a video file, detects faces using both methods, and counts the faces only when the bounding boxes of both algorithms intersect.
The repository for this tutorial:
https://github.com/fiqgant/face_counting_haarcascade_yolov3
modelshaarcascade_frontalface_default.xml
First, ensure you have the necessary libraries installed:
pip install numpy opencv-python opencv-python-headless onnxruntime
Next, we will create a Python script to implement the face counting system.
We begin by importing the required libraries and loading the Haarcascade and YOLOv3 models.
import cv2
import numpy as np
import onnxruntime as ort
# Load the Haarcascade model
haarcascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml' )
# Load the YOLOv3 model
ort_session = ort.InferenceSession( 'yolov3.onnx' )
input_name = ort_session.get_inputs()[ 0 ].name
Read the video file and initialize variables for counting faces.
# Read the video file
cap = cv2.VideoCapture( 'video.mp4' )
# Initialize the face count
face_count = 0
Create functions to detect faces using Haarcascade and YOLOv3.
def detect_faces_haarcascade (frame):
gray = cv2.cvtColor(frame, cv2. COLOR_BGR2GRAY )
faces = haarcascade.detectMultiScale(gray, scaleFactor = 1.1 , minNeighbors = 5 )
return faces
def detect_faces_yolo (frame):
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0 , ( 416 , 416 ), swapRB = True , crop = False )
ort_inputs = {input_name: blob}
detections = ort_session.run( None , ort_inputs)
faces = []
for detection in detections[ 0 ]:
confidence = detection[ 4 ]
if confidence > 0.5 :
x, y, w, h = (detection[ 0 : 4 ] * np.array([frame.shape[ 1 ], frame.shape[ 0 ], frame.shape[ 1 ], frame.shape[ 0 ]])).astype( int )
faces.append((x, y, w, h))
return faces
Process each frame of the video and count the faces only if both Haarcascade and YOLOv3 detect and intersect the same face.
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Detect faces using both methods
faces_haarcascade = detect_faces_haarcascade(frame)
faces_yolo = detect_faces_yolo(frame)
# Count faces based on intersection of both methods
for (x1, y1, w1, h1) in faces_haarcascade:
for (x2, y2, w2, h2) in faces_yolo:
if (x1 < x2 + w2 and x1 + w1 > x2 and y1 < y2 + h2 and y1 + h1 > y2):
face_count += 1
cv2.rectangle(frame, (x1, y1), (x1 + w1, y1 + h1), ( 0 , 255 , 0 ), 2 )
break
cv2.imshow( 'Face Counting' , frame)
if cv2.waitKey( 1 ) & 0x FF == ord ( 'q' ):
break
cap.release()
cv2.destroyAllWindows()
print (f 'Total faces counted: {face_count} ' )
This tutorial demonstrated how to create a face counting system by combining Haarcascade and YOLOv3. By leveraging the strengths of both methods, we can achieve more accurate face detection and counting. Feel free to explore the GitHub repository for more details and additional functionalities.
Feel free to reach out with any questions or comments about this tutorial!