Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import gradio as gr
|
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
from ultralytics import YOLO
|
| 7 |
-
from bytetrack import BYTETracker # Added ByteTrack
|
| 8 |
import time
|
| 9 |
from simple_salesforce import Salesforce
|
| 10 |
from reportlab.lib.pagesizes import letter
|
|
@@ -17,6 +16,20 @@ from retrying import retry
|
|
| 17 |
import uuid
|
| 18 |
from multiprocessing import Pool, cpu_count
|
| 19 |
from functools import partial
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# ==========================
|
| 22 |
# Optimized Configuration
|
|
@@ -54,21 +67,21 @@ CONFIG = {
|
|
| 54 |
},
|
| 55 |
"PUBLIC_URL_BASE": "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo2/resolve/main/static/output/",
|
| 56 |
"CONFIDENCE_THRESHOLDS": {
|
| 57 |
-
"no_helmet": 0.75,
|
| 58 |
"no_harness": 0.4,
|
| 59 |
"unsafe_posture": 0.4,
|
| 60 |
"unsafe_zone": 0.4,
|
| 61 |
"improper_tool_use": 0.4
|
| 62 |
},
|
| 63 |
"MIN_VIOLATION_FRAMES": 3,
|
| 64 |
-
"WORKER_TRACKING_DURATION": 5.0,
|
| 65 |
-
"MAX_PROCESSING_TIME": 60,
|
| 66 |
-
"FRAME_SKIP": 1,
|
| 67 |
-
"BATCH_SIZE": 32,
|
| 68 |
-
"PARALLEL_WORKERS": max(1, cpu_count() - 1),
|
| 69 |
-
"TRACK_BUFFER": 30,
|
| 70 |
-
"TRACK_THRESH": 0.4,
|
| 71 |
-
"MATCH_THRESH": 0.8
|
| 72 |
}
|
| 73 |
|
| 74 |
# Setup logging
|
|
@@ -99,6 +112,10 @@ def load_model():
|
|
| 99 |
|
| 100 |
model = load_model()
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# ==========================
|
| 103 |
# Optimized Helper Functions
|
| 104 |
# ==========================
|
|
@@ -454,22 +471,10 @@ def process_video(video_data):
|
|
| 454 |
logger.error(f"Error processing video: {e}", exc_info=True)
|
| 455 |
yield f"Error processing video: {e}", "", "", "", ""
|
| 456 |
|
|
|
|
| 457 |
# ==========================
|
| 458 |
# Gradio Interface
|
| 459 |
# ==========================
|
| 460 |
-
def gradio_interface(video_file):
|
| 461 |
-
if not video_file:
|
| 462 |
-
return "No file uploaded.", "", "No file uploaded.", "", ""
|
| 463 |
-
try:
|
| 464 |
-
with open(video_file, "rb") as f:
|
| 465 |
-
video_data = f.read()
|
| 466 |
-
|
| 467 |
-
for status, score, snapshots_text, record_id, details_url in process_video(video_data):
|
| 468 |
-
yield status, score, snapshots_text, record_id, details_url
|
| 469 |
-
except Exception as e:
|
| 470 |
-
logger.error(f"Error in Gradio interface: {e}", exc_info=True)
|
| 471 |
-
yield f"Error: {str(e)}", "", "Error in processing.", "", ""
|
| 472 |
-
|
| 473 |
interface = gr.Interface(
|
| 474 |
fn=gradio_interface,
|
| 475 |
inputs=gr.Video(label="Upload Site Video"),
|
|
|
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
from ultralytics import YOLO
|
|
|
|
| 7 |
import time
|
| 8 |
from simple_salesforce import Salesforce
|
| 9 |
from reportlab.lib.pagesizes import letter
|
|
|
|
| 16 |
import uuid
|
| 17 |
from multiprocessing import Pool, cpu_count
|
| 18 |
from functools import partial
|
| 19 |
+
import subprocess
|
| 20 |
+
import sys
|
| 21 |
+
|
| 22 |
+
# ==========================
|
| 23 |
+
# ByteTrack Installation & Import
|
| 24 |
+
# ==========================
|
| 25 |
+
try:
|
| 26 |
+
from bytetrack import BYTETracker
|
| 27 |
+
except ImportError:
|
| 28 |
+
logger = logging.getLogger(__name__)
|
| 29 |
+
logger.warning("ByteTrack not found, installing...")
|
| 30 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "cython", "lap"])
|
| 31 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "git+https://github.com/ifzhang/ByteTrack.git@main"])
|
| 32 |
+
from bytetrack import BYTETracker
|
| 33 |
|
| 34 |
# ==========================
|
| 35 |
# Optimized Configuration
|
|
|
|
| 67 |
},
|
| 68 |
"PUBLIC_URL_BASE": "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo2/resolve/main/static/output/",
|
| 69 |
"CONFIDENCE_THRESHOLDS": {
|
| 70 |
+
"no_helmet": 0.75,
|
| 71 |
"no_harness": 0.4,
|
| 72 |
"unsafe_posture": 0.4,
|
| 73 |
"unsafe_zone": 0.4,
|
| 74 |
"improper_tool_use": 0.4
|
| 75 |
},
|
| 76 |
"MIN_VIOLATION_FRAMES": 3,
|
| 77 |
+
"WORKER_TRACKING_DURATION": 5.0,
|
| 78 |
+
"MAX_PROCESSING_TIME": 60,
|
| 79 |
+
"FRAME_SKIP": 1,
|
| 80 |
+
"BATCH_SIZE": 32,
|
| 81 |
+
"PARALLEL_WORKERS": max(1, cpu_count() - 1),
|
| 82 |
+
"TRACK_BUFFER": 30,
|
| 83 |
+
"TRACK_THRESH": 0.4,
|
| 84 |
+
"MATCH_THRESH": 0.8
|
| 85 |
}
|
| 86 |
|
| 87 |
# Setup logging
|
|
|
|
| 112 |
|
| 113 |
model = load_model()
|
| 114 |
|
| 115 |
+
# [Rest of your existing functions remain exactly the same...]
|
| 116 |
+
# draw_detections(), calculate_safety_score(), generate_violation_pdf(),
|
| 117 |
+
# connect_to_salesforce(), upload_pdf_to_salesforce(), push_report_to_salesforce(),
|
| 118 |
+
# process_video(), and gradio_interface() functions should be kept exactly as they were
|
| 119 |
# ==========================
|
| 120 |
# Optimized Helper Functions
|
| 121 |
# ==========================
|
|
|
|
| 471 |
logger.error(f"Error processing video: {e}", exc_info=True)
|
| 472 |
yield f"Error processing video: {e}", "", "", "", ""
|
| 473 |
|
| 474 |
+
|
| 475 |
# ==========================
|
| 476 |
# Gradio Interface
|
| 477 |
# ==========================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
interface = gr.Interface(
|
| 479 |
fn=gradio_interface,
|
| 480 |
inputs=gr.Video(label="Upload Site Video"),
|