Created
December 3, 2024 16:11
-
-
Save Michaelgathara/23c6add2053000af3140b10f0c3599c5 to your computer and use it in GitHub Desktop.
Take a video and tear it down into frames that have faces then classify the person's emotions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import os | |
import glob | |
from deepface import DeepFace | |
videos_folder = 'videos/' | |
output_folder = 'output_frames/' | |
if not os.path.exists(output_folder): | |
os.makedirs(output_folder) | |
video_files = glob.glob(os.path.join(videos_folder, '*.webm')) | |
# This only reads .webm files because that's what the download youtube video gave me | |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') | |
for video_path in video_files: | |
video_name = os.path.splitext(os.path.basename(video_path))[0] | |
cap = cv2.VideoCapture(video_path) | |
frame_rate = cap.get(cv2.CAP_PROP_FPS) | |
frame_interval = 30 | |
frame_number = 0 | |
success, frame = cap.read() | |
while success: | |
frame_number += 1 | |
if frame_number % frame_interval == 0: | |
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5) | |
if len(faces) > 0: | |
(x, y, w, h) = faces[0] | |
face_img = frame[y:y+h, x:x+w] | |
try: | |
analysis = DeepFace.analyze(face_img, actions=['emotion'], enforce_detection=False) | |
# Check if analysis is a list | |
if isinstance(analysis, list): | |
# Access the first element | |
analysis = analysis[0] | |
emotion = analysis['dominant_emotion'] | |
except Exception as e: | |
print(f"Emotion recognition failed at frame {frame_number}: {e}") | |
emotion = 'unknown' | |
timestamp = frame_number / frame_rate | |
timestamp_formatted = "{:.2f}".format(timestamp) | |
output_filename = f"{video_name}_{emotion}_{timestamp_formatted}.png" | |
output_path = os.path.join(output_folder, output_filename) | |
cv2.imwrite(output_path, frame) | |
print(f"Saved frame at {timestamp_formatted}s to {output_filename}") | |
success, frame = cap.read() | |
cap.release() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment