Download app.py from faiz3123/model123: direct link, hf CLI and curl.
- Browser
- Download file 2.92 kB
-
https://huggingface.co/spaces/faiz3123/model123/resolve/main/app.py
- Command line
-
hf download hf://spaces/faiz3123/model123/app.py
-
curl -L -o app.py https://huggingface.co/spaces/faiz3123/model123/resolve/main/app.py
2.92 kB
| from transformers import pipeline | |
| import cv2 | |
| import numpy as np | |
| from PIL import Image | |
| import gradio as gr | |
| # Load the Hugging Face pipeline | |
| pipe = pipeline("image-classification", model="dima806/facial_emotions_image_detection") | |
| # Load face detection model (Haar Cascade) | |
| face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") | |
| def preprocess_face(face_rgb): | |
| """ | |
| Preprocess the face for the pipeline. | |
| """ | |
| try: | |
| # Resize and normalize the face to match pipeline requirements | |
| face_resized = cv2.resize(face_rgb, (224, 224)) # Adjust to model input | |
| face_normalized = face_resized / 255.0 # Normalize pixel values (0-1) | |
| face_pil = Image.fromarray((face_normalized * 255).astype(np.uint8)) # Convert back to uint8 PIL Image | |
| return face_pil | |
| except Exception as e: | |
| return None | |
| def classify_mood_from_frame(frame): | |
| """ | |
| Detect faces in the frame, classify emotion for each face, and annotate the frame. | |
| """ | |
| try: | |
| # Convert frame to grayscale for face detection | |
| gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) | |
| for (x, y, w, h) in faces: | |
| # Extract and preprocess the face | |
| face_rgb = frame[y:y + h, x:x + w] # Extract RGB region | |
| face_pil = preprocess_face(face_rgb) | |
| if face_pil is None: | |
| continue # Skip if preprocessing failed | |
| # Predict emotion using the Hugging Face pipeline | |
| result = pipe(face_pil) | |
| emotion_label = result[0]['label'] | |
| emotion_score = result[0]['score'] | |
| # Draw rectangle and emotion label on the frame | |
| cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) | |
| cv2.putText(frame, f"{emotion_label} ({emotion_score:.2f})", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) | |
| # Convert frame to RGB for Gradio display | |
| return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
| except Exception as e: | |
| return None | |
| def webcam_emotion_classifier(frame): | |
| """ | |
| Main function to classify emotions from a webcam frame. | |
| """ | |
| if frame is None: | |
| return None # Return if no frame is provided | |
| # Classify the mood from the given frame | |
| annotated_frame = classify_mood_from_frame(frame) | |
| return annotated_frame | |
| # Gradio interface | |
| demo = gr.Interface( | |
| fn=webcam_emotion_classifier, | |
| inputs=gr.Video(source="webcam", streaming=True), # Webcam video input | |
| outputs=gr.Video(), # Display annotated video | |
| live=True, # Enable live streaming | |
| title="Real-Time Mood Classifier 🎮", | |
| description="This app uses your webcam to classify mood in real-time using facial emotion detection." | |
| ) | |
| # Launch the Gradio app | |
| if __name__ == "__main__": | |
| demo.launch() | |