0% found this document useful (0 votes)
9 views4 pages

Selected Generalize Function For Face Detection MTCNN

Uploaded by

Rudra Rathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Selected Generalize Function For Face Detection MTCNN

Uploaded by

Rudra Rathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Selected generalize function for Face Detection MTCNN

Code

from facenet_pytorch import MTCNN


import cv2

mtcnn = MTCNN(keep_all=True)
cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
boxes, _ = mtcnn.detect(frame)

if boxes is not None:


for box in boxes:
cv2.rectangle(frame, (int(box[0]), int(box[1])), (int(box[2]),
int(box[3])), (0, 255, 0), 2)

cv2.imshow('Face Detection', frame)


if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

Models which we have studied

1. Haar Cascade Classifier (OpenCV)


 Pros: Fast, simple, and requires no training.
 Cons: Less accurate compared to more modern methods.
 Usage: Built into OpenCV, suitable for real-time face detection.
2. YOLO (You Only Look Once)
 Pros: Very fast and accurate.
 Cons: Slightly more complex to implement.
 Usage: Pre-trained YOLO models can be used with OpenCV DNN module or Darknet.
3. MTCNN (Multi-task Cascaded Convolutional Networks)
 Pros: High accuracy and good at detecting small faces.
 Cons: Slower than SSD and YOLO.
 Usage: Implemented in various libraries such as facenet-pytorch.
4. Dlib’s HOG + Linear SVM
 Pros: Accurate and relatively fast.
 Cons: Slower than Haar cascades and SSD.
 Usage: Available through the Dlib library.
Other code

Haar cascade
import cv2 # Import the OpenCV library

# Load the Haar Cascade Classifier for face detection


# Ensure the XML file is in the same directory as your script or provide the
correct path
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Open the default camera (usually the webcam)


cap = cv2.VideoCapture(0)

# Continuously capture frames from the camera


while True:
# Capture frame-b y-frame
ret, frame = cap.read()

# Check if the frame is captured correctly


if not ret:
break

# Convert the captured frame to grayscale


gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Detect faces in the grayscale frame


faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1,
minNeighbors=5, minSize=(30, 30))

# Draw rectangles around the detected faces


for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 255), 2)

# Display the frame with detected faces


cv2.imshow('Face Detection', frame)

# Break the loop and close the window when 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release the camera and close all OpenCV windows


cap.release()
cv2.destroyAllWindows()

DLIB
import cv2
import dlib

detector = dlib.get_frontal_face_detector()
cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
for face in faces:
x, y, w, h = (face.left(), face.top(), face.width(), face.height())
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 255), 2)

cv2.imshow('Face Detection', frame)


if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

YOLO
import cv2
import numpy as np

# Load YOLO
net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
layer_names = net.getLayerNames()
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]

# Open a connection to the webcam


cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
height, width, _ = frame.shape # Get frame dimensions

# Prepare the image for the model


blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True,
crop=False)
net.setInput(blob)
outs = net.forward(output_layers)

# Initialize lists for detected objects


boxes = []
confidences = []
# Process each detection from YOLO output
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]

# Filter detections by threshold and class (person in COCO)


if confidence > 0.5 and class_id == 0: # class_id 0 corresponds
to 'person' in COCO dataset
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)

# Calculate bounding box coordinates


x = int(center_x - w / 2)
y = int(center_y - h / 2)

# Store bounding box coordinates and confidence


boxes.append([x, y, w, h])
confidences.append(float(confidence))

# Apply non-maxima suppression to remove redundant overlapping boxes


indexes = cv2.dnn.NMSBoxes(boxes, confidences, score_threshold=0.5,
nms_threshold=0.4)

# Draw filtered bounding boxes on the frame


for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

# Display the resulting frame with detected faces


cv2.imshow('YOLO Face Detection', frame)

# Exit loop if 'q' is pressed


if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release video capture and close all OpenCV windows


cap.release()
cv2.destroyAllWindows()

You might also like