How manish-9245/facial-emotion-recognition-using-opencv-and-deepface works
Purpose and execution
A single-process desktop computer-vision application for real-time webcam facial-emotion labeling. The executable entry point is emotion.py; it opens the default camera, processes frames continuously, and exits on user input. There is no server, persistence layer, or web interface.
Runtime and dependencies
The application runs under Python with OpenCV for camera I/O, image operations, face detection, drawing, and GUI display. DeepFace supplies pretrained emotion inference backed by TensorFlow/Keras. Dependency declarations are in requirements.txt.
Core runtime assets:
• emotion.py
• requirements.txt
• haarcascade_frontalface_default.xml
Camera acquisition and frame loop
emotion.py creates an OpenCV VideoCapture session for the default webcam and enters a continuous read/process/display loop. Each successful camera frame is the unit of work; processing is synchronous, so inference latency directly affects displayed frame rate.
The loop terminates when the user presses q, then releases the camera and closes OpenCV windows.
Face-detection boundary
Before emotion inference, frames are converted to grayscale and passed to an OpenCV Haar-cascade classifier. The classifier is loaded from the repository-local model asset:
• emotion.py
• haarcascade_frontalface_default.xml
This stage emits face bounding boxes. Haar detection determines which regions are analyzed; frames or regions without detections do not produce emotion labels.
Per-face emotion inference
For every detected bounding box, emotion.py crops the corresponding face region from the frame and invokes DeepFace emotion analysis/preprocessing. DeepFace owns the model-specific preparation and pretrained neural-network execution, returning emotion scores or a dominant emotion classification.
This is the principal external-model boundary: repository code manages frame/ROI orchestration, while DeepFace/TensorFlow manages learned-model inference.
Visualization and user interaction
The application overlays each face’s detection rectangle and predicted emotion text directly on the original camera frame, then renders it through an OpenCV display window. This creates a closed local pipeline:
webcam → OpenCV frame → Haar face boxes → face ROI → DeepFace emotion result → annotated frame → local display.
All UI is OpenCV window-based and ephemeral; no results are exported or retained.
Main entry points: emotion.py Camera frame · haarcascade_frontalface_default.xml Haar face detector · requirements.txt requirements.txt
Components 15
Desktop Runtime
Single synchronous Python process.
- emotion.py · Owns camera setup, loop, orchestration, and cleanup.
emotion.py - requirements.txt · Declares OpenCV, DeepFace, and model runtime dependencies.
requirements.txt - Default webcam
- OpenCV display window · Renders annotated frames and receives q to exit.
emotion.py - Camera and window cleanup · Releases the camera and closes windows.
emotion.py
Vision Pipeline
Per-frame local processing.
- VideoCapture loop · Reads frames synchronously until q is pressed.
emotion.py - Camera frame
emotion.py - Grayscale conversion
emotion.py - Face boxes · Only detected regions proceed to emotion analysis.
emotion.py - Face ROI crop
emotion.py - Emotion result
emotion.py - Frame annotation · Draws face rectangles and emotion text on the original frame.
emotion.py
Model Boundaries
Repository and external pretrained models.
- Haar face detector · Finds face bounding boxes in grayscale frames.
haarcascade_frontalface_default.xml - DeepFace analysis · Performs model-specific preprocessing and emotion analysis.
- TensorFlow/Keras model · Executes learned emotion inference behind DeepFace.
Connections 18
- requirements.txt → emotion.py: runtime dependencies
- emotion.py → VideoCapture loop: creates
- Default webcam → VideoCapture loop: camera stream
- VideoCapture loop → Camera frame: reads
- Camera frame → Grayscale conversion: converts
- Grayscale conversion → Haar face detector: detects faces
- Haar face detector → Face boxes: emits
- Camera frame → Face ROI crop: crops by box
- Face boxes → Face ROI crop: selects
- Face ROI crop → DeepFace analysis: analyzes
- DeepFace analysis → TensorFlow/Keras model: uses
- DeepFace analysis → Emotion result: returns
- Camera frame → Frame annotation: base frame
- Face boxes → Frame annotation: rectangles
- Emotion result → Frame annotation: label text
- Frame annotation → OpenCV display window: renders
- OpenCV display window → VideoCapture loop: continues until q
- OpenCV display window → Camera and window cleanup: q pressed