Upload New File

parent 278d59e2
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# Load the video
cap = cv2.VideoCapture(0)
# Get the video dimensions
width = int(cap.get(3))
height = int(cap.get(4))
# Define the center of the frame
center_x = width // 2
center_y = height // 2
while True:
# Read the frame
_, frame = cap.read()
# Convert to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
# Loop over the face detections
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
# Get the center of the face
face_center_x = x + w // 2
face_center_y = y + h // 2
# Check the direction of the face
if face_center_x < center_x - 50:
print("Face is looking to the left.")
# Send an alert
#alert()
elif face_center_x > center_x + 50:
print("Face is looking to the right.")
# Send an alert
#alert()
else:
print("Face is looking straight ahead.")
# Show the frame
cv2.imshow("Face Detection", frame)
# Exit if the user presses 'q'
if cv2.waitKey(1) == ord('q'):
break
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment