-
Notifications
You must be signed in to change notification settings - Fork 0
/
realtime_video.py
220 lines (174 loc) · 10.2 KB
/
realtime_video.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import tkinter as tk
from PIL import Image, ImageTk
import cv2
import os
import json
from datetime import datetime, timedelta
class RealtimeVideoTab(tk.Frame):
# Initialize last_capture_time and capture_interval at the class level
last_capture_time = datetime.now()
capture_interval = 10
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
tk.Label(self, text="Live Video Capture", font=("Arial", 14), background="#FFFFFF").pack(pady=20)
# Create a canvas for displaying video
self.canvas = tk.Canvas(self, width=640, height=480)
self.canvas.pack()
# Create a label for the notification with an initial text of "Initializing..."
self.notification_label = tk.Label(self, text="Initializing...", font=("Arial", 13), bg="#048a01", fg="white", padx=17, pady=10)
self.notification_label.place(relx=0.5, rely=0.1, anchor='center')
self.notification_label.place_forget() # Hide the label immediately
# Initialize video streaming
self.start_video_stream()
def show_notification(self, message, border_color=None):
# Set the label text and position
self.notification_label.config(text=message)
self.notification_label.place(relx=0.5, rely=0.1, anchor='center')
# Change the border color of the canvas if specified
if border_color:
self.canvas.config(highlightbackground=border_color)
# Show the label only if it's hidden
if not self.notification_label.winfo_ismapped():
self.notification_label.lift() # Bring the label to the front
# Schedule the hide_notification method to be called after a delay (e.g., 2000 milliseconds or 2 seconds)
self.after(2000, lambda: self.hide_notification(border_color))
def hide_notification(self, border_color=None):
# Hide the label
self.notification_label.place_forget()
# Revert the border color of the canvas if specified
if border_color:
self.canvas.config(highlightbackground=border_color)
def face_box(self, face_net, frame):
frameWidth = frame.shape[1]
frameHeight = frame.shape[0]
blob = cv2.dnn.blobFromImage(frame, 1.0, (227, 227), [104, 117, 123], swapRB=False)
face_net.setInput(blob)
detection = face_net.forward()
bboxs = []
for i in range(detection.shape[2]):
confidence = detection[0, 0, i, 2]
if confidence > 0.7:
x1 = int(detection[0, 0, i, 3] * frameWidth)
y1 = int(detection[0, 0, i, 4] * frameHeight)
x2 = int(detection[0, 0, i, 5] * frameWidth)
y2 = int(detection[0, 0, i, 6] * frameHeight)
bboxs.append([x1, y1, x2, y2])
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2) # Set box color to red (BGR: 0, 0, 255)
return frame, bboxs
def start_video_stream(self):
# Create the output folder if it doesn't exist
output_folder = "captured_images"
os.makedirs(output_folder, exist_ok=True)
faceProto = "opencv_face_detector.pbtxt"
faceModel = "opencv_face_detector_uint8.pb"
ageProto = "age_deploy.prototxt"
ageModel = "age_net.caffemodel"
genderProto = "gender_deploy.prototxt"
genderModel = "gender_net.caffemodel"
faceNet = cv2.dnn.readNet(faceModel, faceProto)
ageNet = cv2.dnn.readNet(ageModel, ageProto)
genderNet = cv2.dnn.readNet(genderModel, genderProto)
MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746)
ageList = ['11-15', '16-20', '21-25', '26-30', '31-35', '36-40', '41-45', '46-50', '51-55']
genderList = ['Male', 'Female']
video = cv2.VideoCapture(0)
padding = 20
line_margin = 5
def update_frame(genderNet, ageNet):
nonlocal video, faceNet
ret, frame = video.read()
frame = cv2.flip(frame, 1) # Horizontal flip
# Detect faces
frame, bboxs = self.face_box(faceNet, frame)
# Check if faces are detected
if bboxs:
# Display the live video with labels
for bbox in bboxs:
face = frame[max(0, bbox[1] - padding):min(bbox[3] + padding, frame.shape[0] - 1),
max(0, bbox[0] - padding):min(bbox[2] + padding, frame.shape[1] - 1)]
blob = cv2.dnn.blobFromImage(face, 1.0, (227, 227), MODEL_MEAN_VALUES, swapRB=False)
genderNet.setInput(blob)
genderPred = genderNet.forward()
gender = genderList[genderPred[0].argmax()]
ageNet.setInput(blob)
agePred = ageNet.forward()
age = ageList[agePred[0].argmax()]
label_gender = "Gender: {}".format(gender)
label_age = "Age: {}".format(age)
text_size_gender = cv2.getTextSize(label_gender, cv2.FONT_HERSHEY_DUPLEX, 0.6, 1)[0]
text_size_age = cv2.getTextSize(label_age, cv2.FONT_HERSHEY_DUPLEX, 0.6, 1)[0]
cv2.putText(frame, label_gender, (bbox[0], bbox[3] + text_size_gender[1] + line_margin),
cv2.FONT_HERSHEY_DUPLEX, 0.6, (255, 255, 255), 1, cv2.LINE_AA)
cv2.putText(frame, label_age, (bbox[0], bbox[3] + text_size_gender[1] + text_size_age[1] + 2 * line_margin),
cv2.FONT_HERSHEY_DUPLEX, 0.6, (255, 255, 255), 1, cv2.LINE_AA)
# Convert the frame to RGB format for display in tkinter
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(rgb_frame)
img = ImageTk.PhotoImage(image=img)
# Update the canvas with the new frame
self.canvas.create_image(0, 0, anchor=tk.NW, image=img)
self.canvas.image = img # Keep a reference to prevent garbage collection
current_time = datetime.now()
time_difference = current_time - RealtimeVideoTab.last_capture_time
if time_difference.total_seconds() >= RealtimeVideoTab.capture_interval:
# Save the image with a timestamp in the filename
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
age_gender_timestamp = f"{age}_{gender}_{timestamp}"
image_number = len(os.listdir(output_folder)) + 1
image_filename = f"{age_gender_timestamp}_{image_number}.png"
image_path = os.path.join(output_folder, image_filename)
# Apply blur only inside the red facebox
for bbox in bboxs:
face = frame[max(0, bbox[1] - padding):min(bbox[3] + padding, frame.shape[0] - 1),
max(0, bbox[0] - padding):min(bbox[2] + padding, frame.shape[1] - 1)]
# Apply Gaussian blur to the face region
blurred_face = cv2.GaussianBlur(face, (99, 99), 30)
# Replace the face region with the blurred face in the saved image
frame[max(0, bbox[1] - padding):min(bbox[3] + padding, frame.shape[0] - 1),
max(0, bbox[0] - padding):min(bbox[2] + padding, frame.shape[1] - 1)] = blurred_face
# Draw the red facebox and labels on the saved image
cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 2)
cv2.putText(frame, label_gender, (bbox[0], bbox[3] + text_size_gender[1] + line_margin),
cv2.FONT_HERSHEY_DUPLEX, 0.6, (255, 255, 255), 1, cv2.LINE_AA)
cv2.putText(frame, label_age, (bbox[0], bbox[3] + text_size_gender[1] + text_size_age[1] + 2 * line_margin),
cv2.FONT_HERSHEY_DUPLEX, 0.6, (255, 255, 255), 1, cv2.LINE_AA)
# Save the image with blur applied
cv2.imwrite(image_path, frame)
print(f"Blurred Image captured and saved: {image_path}")
# Log the data to log.json
log_data = {
"Date": current_time.strftime('%Y-%m-%d'),
"Time": current_time.strftime('%H:%M:%S'),
"Gender": gender,
"Age": age,
"Image Captured Filename": image_filename
}
log_json_path = "log.json"
try:
with open(log_json_path, 'r') as log_file:
log_json = json.load(log_file)
except json.decoder.JSONDecodeError:
# Handle the case where the file is empty or improperly formatted
log_json = []
log_json.append(log_data)
with open(log_json_path, 'w') as log_file:
json.dump(log_json, log_file, indent=4)
RealtimeVideoTab.last_capture_time = current_time
# Show the notification label without fade-in and fade-out animation
self.show_notification("Image captured successfully")
else:
# No faces detected, update the canvas without processing and displaying faces
img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
img = ImageTk.PhotoImage(image=img)
# Update the canvas with the new frame
self.canvas.create_image(0, 0, anchor=tk.NW, image=img)
self.canvas.image = img # Keep a reference to prevent garbage collection
# Call the update_frame function after 10 milliseconds
self.after(10, lambda: update_frame(genderNet, ageNet))
# Start the update_frame function
update_frame(genderNet, ageNet)
if __name__ == "__main__":
root = tk.Tk()
app = RealtimeVideoTab(root, bg="white")
app.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
root.mainloop()