Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import cv2
|
| 3 |
import gradio as gr
|
|
@@ -13,19 +14,18 @@ from io import BytesIO
|
|
| 13 |
import base64
|
| 14 |
import logging
|
| 15 |
from retrying import retry
|
| 16 |
-
import uuid
|
| 17 |
|
| 18 |
# ==========================
|
| 19 |
# Configuration
|
| 20 |
# ==========================
|
| 21 |
CONFIG = {
|
| 22 |
-
"MODEL_PATH": "
|
|
|
|
| 23 |
"OUTPUT_DIR": "static/output",
|
| 24 |
"VIOLATION_LABELS": {
|
| 25 |
0: "no_helmet",
|
| 26 |
1: "no_harness",
|
| 27 |
-
2: "unsafe_posture"
|
| 28 |
-
3: "unsafe_zone" # Ignored in processing
|
| 29 |
},
|
| 30 |
"DISPLAY_NAMES": { # Mapping for user-friendly violation names
|
| 31 |
"no_helmet": "Missing Helmet",
|
|
@@ -33,10 +33,10 @@ CONFIG = {
|
|
| 33 |
"unsafe_posture": "Unsafe Posture"
|
| 34 |
},
|
| 35 |
"SF_CREDENTIALS": {
|
| 36 |
-
"username": "
|
| 37 |
-
"password": "
|
| 38 |
-
"security_token": "
|
| 39 |
-
"domain": "login"
|
| 40 |
},
|
| 41 |
"PUBLIC_URL_BASE": "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/static/output/",
|
| 42 |
"FRAME_SKIP": 15, # Process every 15th frame
|
|
@@ -62,9 +62,14 @@ logger.info(f"Using device: {device}")
|
|
| 62 |
# ==========================
|
| 63 |
def load_model():
|
| 64 |
try:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
return model
|
| 69 |
except Exception as e:
|
| 70 |
logger.error(f"Failed to load model: {e}")
|
|
@@ -179,10 +184,10 @@ def push_report_to_salesforce(violations, score, pdf_path, pdf_file):
|
|
| 179 |
}
|
| 180 |
logger.info(f"Creating Salesforce record with data: {record_data}")
|
| 181 |
try:
|
| 182 |
-
record = sf.
|
| 183 |
-
logger.info(f"Created
|
| 184 |
except Exception as e:
|
| 185 |
-
logger.error(f"Failed to create
|
| 186 |
record = sf.Account.create({"Name": f"Safety_Report_{int(time.time())}"})
|
| 187 |
logger.warning(f"Fell back to Account record: {record['id']}")
|
| 188 |
record_id = record["id"]
|
|
@@ -191,10 +196,10 @@ def push_report_to_salesforce(violations, score, pdf_path, pdf_file):
|
|
| 191 |
uploaded_url = upload_pdf_to_salesforce(sf, pdf_file, record_id)
|
| 192 |
if uploaded_url:
|
| 193 |
try:
|
| 194 |
-
sf.
|
| 195 |
logger.info(f"Updated record {record_id} with PDF URL: {uploaded_url}")
|
| 196 |
except Exception as e:
|
| 197 |
-
logger.error(f"Failed to update
|
| 198 |
sf.Account.update(record_id, {"Description": uploaded_url})
|
| 199 |
logger.info(f"Updated Account record {record_id} with PDF URL")
|
| 200 |
pdf_url = uploaded_url
|
|
@@ -264,7 +269,7 @@ def process_video(video_data):
|
|
| 264 |
label = CONFIG["VIOLATION_LABELS"].get(cls, f"unknown_class_{cls}")
|
| 265 |
# Only process specified violations
|
| 266 |
if label not in ["no_helmet", "no_harness", "unsafe_posture"]:
|
| 267 |
-
logger.
|
| 268 |
continue
|
| 269 |
# Apply confidence threshold
|
| 270 |
if conf < CONFIG["CONFIDENCE_THRESHOLD"]:
|
|
@@ -389,4 +394,5 @@ interface = gr.Interface(
|
|
| 389 |
|
| 390 |
if __name__ == "__main__":
|
| 391 |
logger.info("Launching Safety Analyzer App...")
|
| 392 |
-
interface.launch()
|
|
|
|
|
|
| 1 |
+
```python
|
| 2 |
import os
|
| 3 |
import cv2
|
| 4 |
import gradio as gr
|
|
|
|
| 14 |
import base64
|
| 15 |
import logging
|
| 16 |
from retrying import retry
|
|
|
|
| 17 |
|
| 18 |
# ==========================
|
| 19 |
# Configuration
|
| 20 |
# ==========================
|
| 21 |
CONFIG = {
|
| 22 |
+
"MODEL_PATH": "yolov8_safety.pt", # Custom-trained model for specific violations
|
| 23 |
+
"FALLBACK_MODEL_PATH": "yolov8n.pt", # Fallback if custom model is missing
|
| 24 |
"OUTPUT_DIR": "static/output",
|
| 25 |
"VIOLATION_LABELS": {
|
| 26 |
0: "no_helmet",
|
| 27 |
1: "no_harness",
|
| 28 |
+
2: "unsafe_posture"
|
|
|
|
| 29 |
},
|
| 30 |
"DISPLAY_NAMES": { # Mapping for user-friendly violation names
|
| 31 |
"no_helmet": "Missing Helmet",
|
|
|
|
| 33 |
"unsafe_posture": "Unsafe Posture"
|
| 34 |
},
|
| 35 |
"SF_CREDENTIALS": {
|
| 36 |
+
"username": "your_username@safety.com",
|
| 37 |
+
"password": "your_password",
|
| 38 |
+
"security_token": "your_security_token",
|
| 39 |
+
"domain": "login" # Use "test" for sandbox
|
| 40 |
},
|
| 41 |
"PUBLIC_URL_BASE": "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/static/output/",
|
| 42 |
"FRAME_SKIP": 15, # Process every 15th frame
|
|
|
|
| 62 |
# ==========================
|
| 63 |
def load_model():
|
| 64 |
try:
|
| 65 |
+
model_path = CONFIG["MODEL_PATH"]
|
| 66 |
+
if not os.path.exists(model_path):
|
| 67 |
+
logger.warning(f"Custom model {model_path} not found. Falling back to {CONFIG['FALLBACK_MODEL_PATH']}")
|
| 68 |
+
model_path = CONFIG["FALLBACK_MODEL_PATH"]
|
| 69 |
+
model = YOLO(model_path).to(device)
|
| 70 |
+
logger.info(f"Model loaded: {model_path}")
|
| 71 |
+
if model_path == CONFIG["FALLBACK_MODEL_PATH"]:
|
| 72 |
+
logger.warning("Using fallback model. Detection accuracy may be poor. Train yolov8_safety.pt for best results.")
|
| 73 |
return model
|
| 74 |
except Exception as e:
|
| 75 |
logger.error(f"Failed to load model: {e}")
|
|
|
|
| 184 |
}
|
| 185 |
logger.info(f"Creating Salesforce record with data: {record_data}")
|
| 186 |
try:
|
| 187 |
+
record = sf.Safety_Violation_Report__c.create(record_data)
|
| 188 |
+
logger.info(f"Created Safety_Violation_Report__c record: {record['id']}")
|
| 189 |
except Exception as e:
|
| 190 |
+
logger.error(f"Failed to create Safety_Violation_Report__c: {e}")
|
| 191 |
record = sf.Account.create({"Name": f"Safety_Report_{int(time.time())}"})
|
| 192 |
logger.warning(f"Fell back to Account record: {record['id']}")
|
| 193 |
record_id = record["id"]
|
|
|
|
| 196 |
uploaded_url = upload_pdf_to_salesforce(sf, pdf_file, record_id)
|
| 197 |
if uploaded_url:
|
| 198 |
try:
|
| 199 |
+
sf.Safety_Violation_Report__c.update(record_id, {"PDF_Report_URL__c": uploaded_url})
|
| 200 |
logger.info(f"Updated record {record_id} with PDF URL: {uploaded_url}")
|
| 201 |
except Exception as e:
|
| 202 |
+
logger.error(f"Failed to update Safety_Violation_Report__c: {e}")
|
| 203 |
sf.Account.update(record_id, {"Description": uploaded_url})
|
| 204 |
logger.info(f"Updated Account record {record_id} with PDF URL")
|
| 205 |
pdf_url = uploaded_url
|
|
|
|
| 269 |
label = CONFIG["VIOLATION_LABELS"].get(cls, f"unknown_class_{cls}")
|
| 270 |
# Only process specified violations
|
| 271 |
if label not in ["no_helmet", "no_harness", "unsafe_posture"]:
|
| 272 |
+
logger.warning(f"Unexpected detection: {label} (cls: {cls}, conf: {conf}) - ignored")
|
| 273 |
continue
|
| 274 |
# Apply confidence threshold
|
| 275 |
if conf < CONFIG["CONFIDENCE_THRESHOLD"]:
|
|
|
|
| 394 |
|
| 395 |
if __name__ == "__main__":
|
| 396 |
logger.info("Launching Safety Analyzer App...")
|
| 397 |
+
interface.launch()
|
| 398 |
+
```
|