-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32 - Age and Gender Detection with Python.py
More file actions
57 lines (40 loc) · 1.11 KB
/
32 - Age and Gender Detection with Python.py
File metadata and controls
57 lines (40 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import cv2
from deepface import DeepFace
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("❌ Cannot open webcam")
exit()
print("📷 Webcam started | Press ESC to exit")
while True:
ret, frame = cap.read()
if not ret:
break
try:
results = DeepFace.analyze(
frame,
actions=["age", "gender", "emotion"],
enforce_detection=False
)
if isinstance(results, list):
results = results[0]
age = results["age"]
gender = results["dominant_gender"]
emotion = results["dominant_emotion"]
label = f"{gender}, {age}, {emotion}"
cv2.putText(
frame,
label,
(20, 40),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(0, 255, 0),
2,
cv2.LINE_AA
)
except Exception:
pass
cv2.imshow("Live Age, Gender & Emotion Detection", frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()