Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -44,7 +44,7 @@ def analyze_video(video_path, confidence=0.5):
|
|
| 44 |
return None, "Error: Could not open video.", [], None, None, None, None, "Error: Could not open video.", None
|
| 45 |
|
| 46 |
# Early frame check and limit
|
| 47 |
-
if int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) >
|
| 48 |
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
| 49 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 50 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
|
@@ -70,8 +70,8 @@ def analyze_video(video_path, confidence=0.5):
|
|
| 70 |
log.write(f"Resolution: {new_width}x{new_height}, FPS: {fps}, Confidence threshold: {confidence}\n\n")
|
| 71 |
|
| 72 |
frame_count = 0
|
| 73 |
-
sample_rate =
|
| 74 |
-
while cap.isOpened() and frame_count <
|
| 75 |
ret, frame = cap.read()
|
| 76 |
if not ret:
|
| 77 |
break
|
|
@@ -120,8 +120,8 @@ def analyze_video(video_path, confidence=0.5):
|
|
| 120 |
color = (255, 255, 0) # Yellow for traffic lights
|
| 121 |
else:
|
| 122 |
color = (255, 255, 255) # White for others
|
| 123 |
-
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 3) #
|
| 124 |
-
cv2.putText(frame, f"{label} {conf:.2f}", (x1, y1 - 20), cv2.FONT_HERSHEY_SIMPLEX, 1.0, color, 2) #
|
| 125 |
|
| 126 |
if label == "person":
|
| 127 |
for other_box in boxes:
|
|
@@ -219,7 +219,7 @@ h3 {color: #34495E; font-size: 18px; padding: 5px;}
|
|
| 219 |
|
| 220 |
with gr.Blocks(css=css) as iface:
|
| 221 |
gr.Markdown("# Road Safety AI Video Analysis")
|
| 222 |
-
gr.Markdown("Upload a traffic video for fast HD analysis (<
|
| 223 |
|
| 224 |
with gr.Row():
|
| 225 |
video_input = gr.Video(label="Upload Video", height=360)
|
|
@@ -233,7 +233,7 @@ with gr.Blocks(css=css) as iface:
|
|
| 233 |
|
| 234 |
with gr.Row():
|
| 235 |
with gr.Column(scale=1):
|
| 236 |
-
video_output = gr.Video(label="Annotated Video Preview (HD)", height=360)
|
| 237 |
summary_output = gr.Textbox(label="Analysis Summary", lines=15)
|
| 238 |
with gr.Column(scale=1):
|
| 239 |
screenshot_output = gr.Gallery(label="Top 15 HD Incident Screenshots", columns=3, rows=5, height=400)
|
|
|
|
| 44 |
return None, "Error: Could not open video.", [], None, None, None, None, "Error: Could not open video.", None
|
| 45 |
|
| 46 |
# Early frame check and limit
|
| 47 |
+
if int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) > 300: # Limit to 5 seconds at 60 FPS
|
| 48 |
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
| 49 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 50 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
|
|
|
| 70 |
log.write(f"Resolution: {new_width}x{new_height}, FPS: {fps}, Confidence threshold: {confidence}\n\n")
|
| 71 |
|
| 72 |
frame_count = 0
|
| 73 |
+
sample_rate = 4 # Increased to process every 4th frame for faster output
|
| 74 |
+
while cap.isOpened() and frame_count < 300: # Limit to 5 seconds
|
| 75 |
ret, frame = cap.read()
|
| 76 |
if not ret:
|
| 77 |
break
|
|
|
|
| 120 |
color = (255, 255, 0) # Yellow for traffic lights
|
| 121 |
else:
|
| 122 |
color = (255, 255, 255) # White for others
|
| 123 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 3) # 3px border
|
| 124 |
+
cv2.putText(frame, f"{label} {conf:.2f}", (x1, y1 - 20), cv2.FONT_HERSHEY_SIMPLEX, 1.0, color, 2) # 1.0 font size
|
| 125 |
|
| 126 |
if label == "person":
|
| 127 |
for other_box in boxes:
|
|
|
|
| 219 |
|
| 220 |
with gr.Blocks(css=css) as iface:
|
| 221 |
gr.Markdown("# Road Safety AI Video Analysis")
|
| 222 |
+
gr.Markdown("Upload a traffic video for fast HD analysis (<30 sec) with charts and downloadable PDF/ZIP.")
|
| 223 |
|
| 224 |
with gr.Row():
|
| 225 |
video_input = gr.Video(label="Upload Video", height=360)
|
|
|
|
| 233 |
|
| 234 |
with gr.Row():
|
| 235 |
with gr.Column(scale=1):
|
| 236 |
+
video_output = gr.Video(label="Annotated Video Preview (HD)", height=360)
|
| 237 |
summary_output = gr.Textbox(label="Analysis Summary", lines=15)
|
| 238 |
with gr.Column(scale=1):
|
| 239 |
screenshot_output = gr.Gallery(label="Top 15 HD Incident Screenshots", columns=3, rows=5, height=400)
|