Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -203,7 +203,7 @@ def process_video(video_data):
|
|
| 203 |
|
| 204 |
video = cv2.VideoCapture(video_path)
|
| 205 |
if not video.isOpened():
|
| 206 |
-
raise
|
| 207 |
|
| 208 |
violations, snapshots = [], []
|
| 209 |
frame_count, processed_frames = 0, 0
|
|
@@ -272,74 +272,66 @@ def process_video(video_data):
|
|
| 272 |
# ==========================
|
| 273 |
# Gradio Interface
|
| 274 |
# ==========================
|
| 275 |
-
def gradio_interface(video_file
|
| 276 |
if not video_file:
|
| 277 |
-
return "No file uploaded.", "", "No file uploaded.", "", "
|
| 278 |
try:
|
| 279 |
with open(video_file, "rb") as f:
|
| 280 |
video_data = f.read()
|
| 281 |
result = process_video(video_data)
|
| 282 |
|
| 283 |
-
# Format violations as a Markdown table
|
| 284 |
violation_table = "No violations detected."
|
| 285 |
if result["violations"]:
|
| 286 |
-
header = "| Violation
|
| 287 |
-
separator = "
|
| 288 |
rows = []
|
| 289 |
for v in result["violations"]:
|
| 290 |
-
# Use original violation names (e.g., "no_helmet", "unsafe_posture")
|
| 291 |
violation_name = v["violation"]
|
| 292 |
-
#
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
| 300 |
rows.append(row)
|
| 301 |
violation_table = header + separator + "\n".join(rows)
|
| 302 |
|
| 303 |
-
# Format snapshots as a bullet list
|
| 304 |
snapshots_text = "No snapshots captured."
|
| 305 |
if result["snapshots"]:
|
| 306 |
snapshots_text = "\n".join(
|
| 307 |
-
f"- Snapshot for {s['violation']} at frame {s['frame']}: [{s['snapshot_url']}]({s['snapshot_url']})"
|
| 308 |
for s in result["snapshots"]
|
| 309 |
)
|
| 310 |
|
| 311 |
-
# Display the selected snapshot image
|
| 312 |
-
snapshot_image = selected_snapshot_url if selected_snapshot_url else None
|
| 313 |
-
|
| 314 |
return (
|
| 315 |
violation_table,
|
| 316 |
f"Safety Score: {result['score']}%",
|
| 317 |
snapshots_text,
|
| 318 |
f"Salesforce Record ID: {result['salesforce_record_id'] or 'N/A'}",
|
| 319 |
-
|
| 320 |
-
snapshot_image
|
| 321 |
)
|
| 322 |
except Exception as e:
|
| 323 |
logger.error(f"Error in Gradio interface: {e}")
|
| 324 |
-
return f"Error: {str(e)}", "", "Error in processing.", "", "
|
| 325 |
|
| 326 |
-
# Update Gradio interface to include snapshot URL input and image output
|
| 327 |
interface = gr.Interface(
|
| 328 |
fn=gradio_interface,
|
| 329 |
-
inputs=
|
| 330 |
-
gr.Video(label="Upload Site Video"),
|
| 331 |
-
gr.Textbox(label="Snapshot URL (Click a link in the table or snapshots)", placeholder="Paste snapshot URL here", interactive=True)
|
| 332 |
-
],
|
| 333 |
outputs=[
|
| 334 |
gr.Markdown(label="Detected Safety Violations"),
|
| 335 |
gr.Textbox(label="Compliance Score"),
|
| 336 |
gr.Markdown(label="Snapshots"),
|
| 337 |
gr.Textbox(label="Salesforce Record ID"),
|
| 338 |
-
gr.Textbox(label="URL")
|
| 339 |
-
gr.Image(label="Selected Snapshot")
|
| 340 |
],
|
| 341 |
title="Worksite Safety Violation Analyzer",
|
| 342 |
-
description="Upload short site videos to detect safety violations (e.g., no helmet, no harness, unsafe posture).
|
| 343 |
)
|
| 344 |
|
| 345 |
if __name__ == "__main__":
|
|
|
|
| 203 |
|
| 204 |
video = cv2.VideoCapture(video_path)
|
| 205 |
if not video.isOpened():
|
| 206 |
+
raise ValueError("Could not open video file")
|
| 207 |
|
| 208 |
violations, snapshots = [], []
|
| 209 |
frame_count, processed_frames = 0, 0
|
|
|
|
| 272 |
# ==========================
|
| 273 |
# Gradio Interface
|
| 274 |
# ==========================
|
| 275 |
+
def gradio_interface(video_file):
|
| 276 |
if not video_file:
|
| 277 |
+
return "No file uploaded.", "", "No file uploaded.", "", ""
|
| 278 |
try:
|
| 279 |
with open(video_file, "rb") as f:
|
| 280 |
video_data = f.read()
|
| 281 |
result = process_video(video_data)
|
| 282 |
|
| 283 |
+
# Format violations as a Markdown table
|
| 284 |
violation_table = "No violations detected."
|
| 285 |
if result["violations"]:
|
| 286 |
+
header = "| Violation | Timestamp | Confidence | Bounding Box | Violation Details |\n"
|
| 287 |
+
separator = "|---------------|-----------|------------|--------------------------|-------------------|\n"
|
| 288 |
rows = []
|
| 289 |
for v in result["violations"]:
|
|
|
|
| 290 |
violation_name = v["violation"]
|
| 291 |
+
# Replace specific violation names for display
|
| 292 |
+
if violation_name == "no_helmet":
|
| 293 |
+
violation_name = "no helmet"
|
| 294 |
+
elif violation_name == "no_harness":
|
| 295 |
+
violation_name = "no harness"
|
| 296 |
+
elif violation_name == "unsafe_posture":
|
| 297 |
+
violation_name = "unsafe posture"
|
| 298 |
+
elif violation_name == "unsafe_zone":
|
| 299 |
+
violation_name = "unsafe zone"
|
| 300 |
+
row = f"| {violation_name:<13} | {v['timestamp']:.2f}s | {v['confidence']:.2f} | {v['bounding_box']} | [Details]({result['violation_details_url']}) |"
|
| 301 |
rows.append(row)
|
| 302 |
violation_table = header + separator + "\n".join(rows)
|
| 303 |
|
| 304 |
+
# Format snapshots as a bullet list with clickable image links
|
| 305 |
snapshots_text = "No snapshots captured."
|
| 306 |
if result["snapshots"]:
|
| 307 |
snapshots_text = "\n".join(
|
| 308 |
+
f"- Snapshot for {s['violation'].replace('no_', '').replace('unsafe_', '')} at frame {s['frame']}: []({s['snapshot_url']})"
|
| 309 |
for s in result["snapshots"]
|
| 310 |
)
|
| 311 |
|
|
|
|
|
|
|
|
|
|
| 312 |
return (
|
| 313 |
violation_table,
|
| 314 |
f"Safety Score: {result['score']}%",
|
| 315 |
snapshots_text,
|
| 316 |
f"Salesforce Record ID: {result['salesforce_record_id'] or 'N/A'}",
|
| 317 |
+
result["violation_details_url"] or 'N/A'
|
|
|
|
| 318 |
)
|
| 319 |
except Exception as e:
|
| 320 |
logger.error(f"Error in Gradio interface: {e}")
|
| 321 |
+
return f"Error: {str(e)}", "", "Error in processing.", "", ""
|
| 322 |
|
|
|
|
| 323 |
interface = gr.Interface(
|
| 324 |
fn=gradio_interface,
|
| 325 |
+
inputs=gr.Video(label="Upload Site Video"),
|
|
|
|
|
|
|
|
|
|
| 326 |
outputs=[
|
| 327 |
gr.Markdown(label="Detected Safety Violations"),
|
| 328 |
gr.Textbox(label="Compliance Score"),
|
| 329 |
gr.Markdown(label="Snapshots"),
|
| 330 |
gr.Textbox(label="Salesforce Record ID"),
|
| 331 |
+
gr.Textbox(label="URL")
|
|
|
|
| 332 |
],
|
| 333 |
title="Worksite Safety Violation Analyzer",
|
| 334 |
+
description="Upload short site videos to detect safety violations (e.g., no helmet, no harness, unsafe posture)."
|
| 335 |
)
|
| 336 |
|
| 337 |
if __name__ == "__main__":
|