Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Domains/AI-ML/MiniProjects/Emotion_classifier/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
**Contributor:** Ansh-1019

# 🎭 Real-Time Emotion Detection using DeepFace and OpenCV

This project detects human emotions in real-time using a webcam feed.
It utilizes the **DeepFace** library for emotion analysis and **OpenCV** for face detection and visualization.

---

## 🚀 Features

- Detects emotions such as **happy**, **sad**, **angry**, **surprise**, **neutral**, and more.
- Works in **real-time** using your webcam.
- Displays bounding boxes and emotion labels on detected faces.
- Gracefully handles frames without detectable faces.

---

## 🧠 Technologies Used

- **Python 3.8+**
- **OpenCV** – for capturing webcam feed and drawing on frames.
- **DeepFace** – for facial emotion analysis.
- **Haar Cascade Classifier** – for basic face detection.

---

## 📦 Installation

### 1️⃣ Clone or Download the Repository
```bash
git clone https://github.com/yourusername/emotion-detection.git
cd emotion-detection
```

---

## ▶️ How to Run

1. Save the provided code as **`emotion.py`** in your project folder.
2. Open **Command Prompt** or **Terminal** in that folder.
3. Run the script:

```bash
python emotion.py
```
4. Your webcam will open automatically.
5. A bounding box with your detected emotion will appear on screen.
6. Press **`q`** to quit the program.
45 changes: 45 additions & 0 deletions Domains/AI-ML/MiniProjects/Emotion_classifier/emotion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import cv2
from deepface import DeepFace


cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
if not ret:
break

try:

result = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False)


if isinstance(result, list):
emotion = result[0]['dominant_emotion']
else:
emotion = result['dominant_emotion']


face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(frame, 1.3, 5)

for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.putText(frame, emotion, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

except Exception as e:

print("No face detected or error:", e)

cv2.putText(frame, "No face detected", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)


cv2.imshow('Emotion Detection', frame)


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


cap.release()
cv2.destroyAllWindows()
Loading