Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -51,22 +51,22 @@ CONFIG = {
|
|
| 51 |
},
|
| 52 |
"PUBLIC_URL_BASE": "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo2/resolve/main/static/output/",
|
| 53 |
"FRAME_SKIP": {
|
| 54 |
-
"no_helmet": 2,
|
| 55 |
"no_harness": 1,
|
| 56 |
"unsafe_posture": 1,
|
| 57 |
"unsafe_zone": 1,
|
| 58 |
"improper_tool_use": 1
|
| 59 |
},
|
| 60 |
"CONFIDENCE_THRESHOLDS": {
|
| 61 |
-
"no_helmet": 0.5,
|
| 62 |
-
"no_harness": 0.15,
|
| 63 |
"unsafe_posture": 0.15,
|
| 64 |
"unsafe_zone": 0.15,
|
| 65 |
"improper_tool_use": 0.15
|
| 66 |
},
|
| 67 |
"IOU_THRESHOLD": 0.4,
|
| 68 |
"MIN_VIOLATION_FRAMES": 3,
|
| 69 |
-
"HELMET_CONFIDENCE_THRESHOLD": 0.7
|
| 70 |
}
|
| 71 |
|
| 72 |
# Setup logging
|
|
@@ -493,38 +493,39 @@ def process_video(video_data):
|
|
| 493 |
|
| 494 |
if not violations:
|
| 495 |
logger.info("No persistent violations detected")
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
"snapshots": [],
|
| 499 |
-
"score": 100,
|
| 500 |
-
"salesforce_record_id": None,
|
| 501 |
-
"violation_details_url": "",
|
| 502 |
-
"message": "No violations detected in the video."
|
| 503 |
-
}
|
| 504 |
|
| 505 |
score = calculate_safety_score(violations)
|
| 506 |
pdf_path, pdf_url, pdf_file = generate_violation_pdf(violations, score)
|
| 507 |
report_id, final_pdf_url = push_report_to_salesforce(violations, score, pdf_path, pdf_file)
|
| 508 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
logger.info(f"Processing complete: {len(violations)} violations detected, score: {score}%")
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
"
|
| 513 |
-
|
| 514 |
-
"
|
| 515 |
-
"
|
| 516 |
-
|
| 517 |
-
}
|
| 518 |
except Exception as e:
|
| 519 |
logger.error(f"Error processing video: {e}", exc_info=True)
|
| 520 |
-
|
| 521 |
-
"violations": [],
|
| 522 |
-
"snapshots": [],
|
| 523 |
-
"score": 100,
|
| 524 |
-
"salesforce_record_id": None,
|
| 525 |
-
"violation_details_url": "",
|
| 526 |
-
"message": f"Error processing video: {e}"
|
| 527 |
-
}
|
| 528 |
|
| 529 |
# ==========================
|
| 530 |
# Gradio Interface
|
|
@@ -536,43 +537,9 @@ def gradio_interface(video_file):
|
|
| 536 |
with open(video_file, "rb") as f:
|
| 537 |
video_data = f.read()
|
| 538 |
|
| 539 |
-
# Use generator to yield
|
| 540 |
-
for status,
|
| 541 |
-
|
| 542 |
-
yield status, "", "", "", ""
|
| 543 |
-
continue
|
| 544 |
-
|
| 545 |
-
if status.get("message"):
|
| 546 |
-
yield status["message"], "", "", "", ""
|
| 547 |
-
return
|
| 548 |
-
|
| 549 |
-
violation_table = "No violations detected."
|
| 550 |
-
if status["violations"]:
|
| 551 |
-
header = "| Violation | Timestamp (s) | Confidence | Worker ID |\n"
|
| 552 |
-
separator = "|------------------------|---------------|------------|-----------|\n"
|
| 553 |
-
rows = []
|
| 554 |
-
violation_name_map = CONFIG["DISPLAY_NAMES"]
|
| 555 |
-
for v in status["violations"]:
|
| 556 |
-
display_name = violation_name_map.get(v.get("violation", "Unknown"), "Unknown")
|
| 557 |
-
row = f"| {display_name:<22} | {v.get('timestamp', 0.0):.2f} | {v.get('confidence', 0.0):.2f} | {v.get('worker_id', 'N/A')} |"
|
| 558 |
-
rows.append(row)
|
| 559 |
-
violation_table = header + separator + "\n".join(rows)
|
| 560 |
-
|
| 561 |
-
snapshots_text = "No snapshots captured."
|
| 562 |
-
if status["snapshots"]:
|
| 563 |
-
violation_name_map = CONFIG["DISPLAY_NAMES"]
|
| 564 |
-
snapshots_text = "\n".join(
|
| 565 |
-
f"- Snapshot for {violation_name_map.get(s.get('violation', 'Unknown'), 'Unknown')} at frame {s.get('frame', 0)}: })"
|
| 566 |
-
for s in status["snapshots"]
|
| 567 |
-
)
|
| 568 |
-
|
| 569 |
-
yield (
|
| 570 |
-
violation_table,
|
| 571 |
-
f"Safety Score: {status['score']}%",
|
| 572 |
-
snapshots_text,
|
| 573 |
-
f"Salesforce Record ID: {status['salesforce_record_id'] or 'N/A'}",
|
| 574 |
-
status["violation_details_url"] or "N/A"
|
| 575 |
-
)
|
| 576 |
except Exception as e:
|
| 577 |
logger.error(f"Error in Gradio interface: {e}", exc_info=True)
|
| 578 |
yield f"Error: {str(e)}", "", "Error in processing.", "", ""
|
|
@@ -580,7 +547,7 @@ def gradio_interface(video_file):
|
|
| 580 |
interface = gr.Interface(
|
| 581 |
fn=gradio_interface,
|
| 582 |
inputs=gr.Video(label="Upload Site Video"),
|
| 583 |
-
outputs=[
|
| 584 |
gr.Markdown(label="Detected Safety Violations"),
|
| 585 |
gr.Textbox(label="Compliance Score"),
|
| 586 |
gr.Markdown(label="Snapshots"),
|
|
|
|
| 51 |
},
|
| 52 |
"PUBLIC_URL_BASE": "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo2/resolve/main/static/output/",
|
| 53 |
"FRAME_SKIP": {
|
| 54 |
+
"no_helmet": 2,
|
| 55 |
"no_harness": 1,
|
| 56 |
"unsafe_posture": 1,
|
| 57 |
"unsafe_zone": 1,
|
| 58 |
"improper_tool_use": 1
|
| 59 |
},
|
| 60 |
"CONFIDENCE_THRESHOLDS": {
|
| 61 |
+
"no_helmet": 0.5,
|
| 62 |
+
"no_harness": 0.15,
|
| 63 |
"unsafe_posture": 0.15,
|
| 64 |
"unsafe_zone": 0.15,
|
| 65 |
"improper_tool_use": 0.15
|
| 66 |
},
|
| 67 |
"IOU_THRESHOLD": 0.4,
|
| 68 |
"MIN_VIOLATION_FRAMES": 3,
|
| 69 |
+
"HELMET_CONFIDENCE_THRESHOLD": 0.7
|
| 70 |
}
|
| 71 |
|
| 72 |
# Setup logging
|
|
|
|
| 493 |
|
| 494 |
if not violations:
|
| 495 |
logger.info("No persistent violations detected")
|
| 496 |
+
yield "No violations detected in the video.", "Safety Score: 100%", "No snapshots captured.", "N/A", "N/A"
|
| 497 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 498 |
|
| 499 |
score = calculate_safety_score(violations)
|
| 500 |
pdf_path, pdf_url, pdf_file = generate_violation_pdf(violations, score)
|
| 501 |
report_id, final_pdf_url = push_report_to_salesforce(violations, score, pdf_path, pdf_file)
|
| 502 |
|
| 503 |
+
violation_table = "| Violation | Timestamp (s) | Confidence | Worker ID |\n"
|
| 504 |
+
violation_table += "|------------------------|---------------|------------|-----------|\n"
|
| 505 |
+
for v in violations:
|
| 506 |
+
display_name = CONFIG["DISPLAY_NAMES"].get(v.get("violation", "Unknown"), "Unknown")
|
| 507 |
+
row = f"| {display_name:<22} | {v.get('timestamp', 0.0):.2f} | {v.get('confidence', 0.0):.2f} | {v.get('worker_id', 'N/A')} |\n"
|
| 508 |
+
violation_table += row
|
| 509 |
+
|
| 510 |
+
snapshots_text = "No snapshots captured."
|
| 511 |
+
if snapshots:
|
| 512 |
+
violation_name_map = CONFIG["DISPLAY_NAMES"]
|
| 513 |
+
snapshots_text = "\n".join(
|
| 514 |
+
f"- Snapshot for {violation_name_map.get(s.get('violation', 'Unknown'), 'Unknown')} at frame {s.get('frame', 0)}: })"
|
| 515 |
+
for s in snapshots
|
| 516 |
+
)
|
| 517 |
+
|
| 518 |
logger.info(f"Processing complete: {len(violations)} violations detected, score: {score}%")
|
| 519 |
+
yield (
|
| 520 |
+
violation_table,
|
| 521 |
+
f"Safety Score: {score}%",
|
| 522 |
+
snapshots_text,
|
| 523 |
+
f"Salesforce Record ID: {report_id or 'N/A'}",
|
| 524 |
+
final_pdf_url or "N/A"
|
| 525 |
+
)
|
|
|
|
| 526 |
except Exception as e:
|
| 527 |
logger.error(f"Error processing video: {e}", exc_info=True)
|
| 528 |
+
yield f"Error processing video: {e}", "", "", "", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
|
| 530 |
# ==========================
|
| 531 |
# Gradio Interface
|
|
|
|
| 537 |
with open(video_file, "rb") as f:
|
| 538 |
video_data = f.read()
|
| 539 |
|
| 540 |
+
# Use generator to yield updates
|
| 541 |
+
for status, score, snapshots_text, record_id, details_url in process_video(video_data):
|
| 542 |
+
yield status, score, snapshots_text, record_id, details_url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 543 |
except Exception as e:
|
| 544 |
logger.error(f"Error in Gradio interface: {e}", exc_info=True)
|
| 545 |
yield f"Error: {str(e)}", "", "Error in processing.", "", ""
|
|
|
|
| 547 |
interface = gr.Interface(
|
| 548 |
fn=gradio_interface,
|
| 549 |
inputs=gr.Video(label="Upload Site Video"),
|
| 550 |
+
outputs=[
|
| 551 |
gr.Markdown(label="Detected Safety Violations"),
|
| 552 |
gr.Textbox(label="Compliance Score"),
|
| 553 |
gr.Markdown(label="Snapshots"),
|