Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,33 @@ def preprocess_frame(frame):
|
|
12 |
normalized_frame = resized_frame / 255.0
|
13 |
return np.expand_dims(normalized_frame, axis=0) # Add batch dimension
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
@spaces.GPU(duration=120)
|
16 |
def predict_drowsiness(video_path):
|
17 |
# Open the video file
|
@@ -23,6 +50,9 @@ def predict_drowsiness(video_path):
|
|
23 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
24 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
25 |
|
|
|
|
|
|
|
26 |
# Create a temporary file for the output video
|
27 |
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_output:
|
28 |
temp_output_path = temp_output.name
|
@@ -30,24 +60,28 @@ def predict_drowsiness(video_path):
|
|
30 |
# Output video settings
|
31 |
out = cv2.VideoWriter(temp_output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))
|
32 |
|
|
|
33 |
while cap.isOpened():
|
34 |
ret, frame = cap.read()
|
35 |
if not ret:
|
36 |
break
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
48 |
|
49 |
-
# Write the frame
|
50 |
out.write(frame)
|
|
|
51 |
|
52 |
# Release resources
|
53 |
cap.release()
|
|
|
12 |
normalized_frame = resized_frame / 255.0
|
13 |
return np.expand_dims(normalized_frame, axis=0) # Add batch dimension
|
14 |
|
15 |
+
# Function to draw pretty label on the frame
|
16 |
+
def draw_label(frame, label, position=(50, 50), font_scale=1, thickness=2):
|
17 |
+
# Define label properties
|
18 |
+
if label == 'Drowsy':
|
19 |
+
color = (0, 0, 255) # Red for Drowsy
|
20 |
+
bg_color = (0, 0, 100) # Darker background for Drowsy
|
21 |
+
else:
|
22 |
+
color = (0, 255, 0) # Green for Alert
|
23 |
+
bg_color = (0, 100, 0) # Darker background for Alert
|
24 |
+
|
25 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
26 |
+
text_size = cv2.getTextSize(label, font, font_scale, thickness)[0]
|
27 |
+
|
28 |
+
# Define rectangle background dimensions
|
29 |
+
text_x, text_y = position
|
30 |
+
rect_start = (text_x, text_y - text_size[1] - 10) # Adjust y to account for text height
|
31 |
+
rect_end = (text_x + text_size[0] + 10, text_y + 10)
|
32 |
+
|
33 |
+
# Draw rectangle background
|
34 |
+
cv2.rectangle(frame, rect_start, rect_end, bg_color, -1)
|
35 |
+
|
36 |
+
# Add border around text
|
37 |
+
cv2.putText(frame, label, position, font, font_scale, (255, 255, 255), thickness + 2, lineType=cv2.LINE_AA)
|
38 |
+
|
39 |
+
# Add the main colored text
|
40 |
+
cv2.putText(frame, label, position, font, font_scale, color, thickness, lineType=cv2.LINE_AA)
|
41 |
+
|
42 |
@spaces.GPU(duration=120)
|
43 |
def predict_drowsiness(video_path):
|
44 |
# Open the video file
|
|
|
50 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
51 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
52 |
|
53 |
+
# Calculate frame skipping interval based on 0.5 seconds
|
54 |
+
skip_interval = int(fps * 0.5) # Skip frames to achieve 1 frame every 0.5 seconds
|
55 |
+
|
56 |
# Create a temporary file for the output video
|
57 |
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_output:
|
58 |
temp_output_path = temp_output.name
|
|
|
60 |
# Output video settings
|
61 |
out = cv2.VideoWriter(temp_output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))
|
62 |
|
63 |
+
frame_count = 0
|
64 |
while cap.isOpened():
|
65 |
ret, frame = cap.read()
|
66 |
if not ret:
|
67 |
break
|
68 |
|
69 |
+
# Only process frames at the specified interval
|
70 |
+
if frame_count % skip_interval == 0:
|
71 |
+
# Preprocess frame
|
72 |
+
preprocessed_frame = preprocess_frame(frame)
|
73 |
+
|
74 |
+
# Use the model to predict drowsiness
|
75 |
+
prediction = model.predict(preprocessed_frame)
|
76 |
+
drowsiness = np.argmax(prediction)
|
77 |
+
|
78 |
+
# Add label to frame with improved visibility
|
79 |
+
label = 'Drowsy' if drowsiness == 0 else 'Alert'
|
80 |
+
draw_label(frame, label, position=(50, 50)) # Use the draw_label function
|
81 |
|
82 |
+
# Write the frame (whether labeled or not) to the output video
|
83 |
out.write(frame)
|
84 |
+
frame_count += 1
|
85 |
|
86 |
# Release resources
|
87 |
cap.release()
|