-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebcam_model.py
More file actions
127 lines (94 loc) · 3.94 KB
/
Copy pathWebcam_model.py
File metadata and controls
127 lines (94 loc) · 3.94 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
import numpy as np
import cv2
import cvlib as cv
# Load model
model = load_model('gender_detection.h5')
# Open webcam
webcam = cv2.VideoCapture(0)
classes = ['man', 'woman']
# Function to check if the current frame is in low light condition
def is_low_light(frame, threshold=50):
# Convert to grayscale
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Calculate mean brightness
mean_brightness = np.mean(gray_frame)
# Check against the threshold
return mean_brightness < threshold
# Initialize counters for accuracy calculation
total_faces = 0
correct_predictions = 0
# Loop through frames
while webcam.isOpened():
# Read frame from webcam
status, frame = webcam.read()
if not status:
print("Error: Could not read frame.")
break
# Check for low light condition
if is_low_light(frame):
cv2.putText(frame, "Low Light Condition", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
else:
cv2.putText(frame, "Adequate Light", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Apply face detection
faces, confidence = cv.detect_face(frame)
# Initialize counters for this frame
man_count = 0
woman_count = 0
# Loop through detected faces
for idx, f in enumerate(faces):
# Get corner points of face rectangle
(startX, startY) = f[0], f[1]
(endX, endY) = f[2], f[3]
# Draw rectangle over face
cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 255, 0), 2)
# Crop the detected face region
face_crop = np.copy(frame[startY:endY, startX:endX])
if face_crop.shape[0] < 10 or face_crop.shape[1] < 10:
continue
# Preprocessing for gender detection model
face_crop = cv2.resize(face_crop, (96, 96))
face_crop = face_crop.astype("float") / 255.0
face_crop = img_to_array(face_crop)
face_crop = np.expand_dims(face_crop, axis=0)
# Apply gender detection on face
conf = model.predict(face_crop)[0] # model.predict returns a 2D matrix, ex: [[9.9993384e-01 7.4850512e-05]]
# Get label with max accuracy
idx = np.argmax(conf)
label = classes[idx]
# Increment the appropriate counter
if label == 'man':
man_count += 1
else:
woman_count += 1
label = "{}: {:.2f}%".format(label, conf[idx] * 100)
Y = startY - 10 if startY - 10 > 10 else startY + 10
# Write label and confidence above face rectangle
cv2.putText(frame, label, (startX, Y), cv2.FONT_HERSHEY_SIMPLEX,
0.7, (0, 255, 0), 2)
# Update accuracy calculation
total_faces += 1
correct_predictions += conf[idx] > 0.5 # Assuming 0.5 as the threshold for correctness
# Calculate accuracy
accuracy = (correct_predictions / total_faces) * 100 if total_faces > 0 else 0
# Display the number of men and women detected
cv2.putText(frame, f"Men: {man_count}, Women: {woman_count}", (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
# Display the accuracy
cv2.putText(frame, f"Accuracy: {accuracy:.2f}%", (10, 90),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 0), 2)
# Check for alert condition (only one woman detected, regardless of men count)
if woman_count == 1:
cv2.putText(frame, "ALERT: Woman Alone", (10, 120),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
# Display output
cv2.imshow("Gender Detection", frame)
# Press "Q" to stop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
webcam.release()
cv2.destroyAllWindows()