Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -235,7 +235,10 @@ def calculate_safety_score(violations):
|
|
| 235 |
"no_harness": 30,
|
| 236 |
"unsafe_posture": 20
|
| 237 |
}
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
| 239 |
return max(score, 0)
|
| 240 |
|
| 241 |
def process_video(video_data):
|
|
@@ -255,7 +258,7 @@ def process_video(video_data):
|
|
| 255 |
fps = video.get(cv2.CAP_PROP_FPS)
|
| 256 |
|
| 257 |
snapshot_taken = {label: False for label in CONFIG["VIOLATION_LABELS"].values()}
|
| 258 |
-
workers = [] # List to track workers
|
| 259 |
|
| 260 |
while True:
|
| 261 |
ret, frame = video.read()
|
|
@@ -276,6 +279,11 @@ def process_video(video_data):
|
|
| 276 |
for box in result.boxes:
|
| 277 |
cls, conf = int(box.cls), float(box.conf)
|
| 278 |
label = CONFIG["VIOLATION_LABELS"].get(cls, None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
if label not in CONFIG["VIOLATION_LABELS"].values() or conf < CONFIG["CONFIDENCE_THRESHOLD"]:
|
| 280 |
continue
|
| 281 |
|
|
@@ -288,7 +296,7 @@ def process_video(video_data):
|
|
| 288 |
"frame": frame_count
|
| 289 |
})
|
| 290 |
|
| 291 |
-
#
|
| 292 |
for detection in current_detections:
|
| 293 |
matched_worker = None
|
| 294 |
max_iou = 0
|
|
@@ -310,19 +318,7 @@ def process_video(video_data):
|
|
| 310 |
"timestamp": detection["timestamp"],
|
| 311 |
"worker_id": matched_worker["id"]
|
| 312 |
})
|
| 313 |
-
|
| 314 |
-
if not snapshot_taken[detection["violation"]]:
|
| 315 |
-
snapshot_path = os.path.join(CONFIG["OUTPUT_DIR"], f"snapshot_{frame_count}_{detection['violation']}.jpg")
|
| 316 |
-
cv2.imwrite(snapshot_path, frame)
|
| 317 |
-
with open(snapshot_path, "rb") as img_file:
|
| 318 |
-
img_base64 = base64.b64encode(img_file.read()).decode('utf-8')
|
| 319 |
-
snapshots.append({
|
| 320 |
-
"violation": detection["violation"],
|
| 321 |
-
"frame": frame_count,
|
| 322 |
-
"snapshot_url": f"{CONFIG['PUBLIC_URL_BASE']}{os.path.basename(snapshot_path)}",
|
| 323 |
-
"snapshot_base64": f"data:image/jpeg;base64,{img_base64}"
|
| 324 |
-
})
|
| 325 |
-
snapshot_taken[detection["violation"]] = True
|
| 326 |
matched_worker["bbox"] = detection["bounding_box"]
|
| 327 |
matched_worker["last_frame"] = frame_count
|
| 328 |
else:
|
|
@@ -342,19 +338,6 @@ def process_video(video_data):
|
|
| 342 |
"timestamp": detection["timestamp"],
|
| 343 |
"worker_id": worker_id
|
| 344 |
})
|
| 345 |
-
# Take snapshot if not already taken for this violation type
|
| 346 |
-
if not snapshot_taken[detection["violation"]]:
|
| 347 |
-
snapshot_path = os.path.join(CONFIG["OUTPUT_DIR"], f"snapshot_{frame_count}_{detection['violation']}.jpg")
|
| 348 |
-
cv2.imwrite(snapshot_path, frame)
|
| 349 |
-
with open(snapshot_path, "rb") as img_file:
|
| 350 |
-
img_base64 = base64.b64encode(img_file.read()).decode('utf-8')
|
| 351 |
-
snapshots.append({
|
| 352 |
-
"violation": detection["violation"],
|
| 353 |
-
"frame": frame_count,
|
| 354 |
-
"snapshot_url": f"{CONFIG['PUBLIC_URL_BASE']}{os.path.basename(snapshot_path)}",
|
| 355 |
-
"snapshot_base64": f"data:image/jpeg;base64,{img_base64}"
|
| 356 |
-
})
|
| 357 |
-
snapshot_taken[detection["violation"]] = True
|
| 358 |
|
| 359 |
frame_count += 1
|
| 360 |
|
|
@@ -444,7 +427,7 @@ def gradio_interface(video_file):
|
|
| 444 |
interface = gr.Interface(
|
| 445 |
fn=gradio_interface,
|
| 446 |
inputs=gr.Video(label="Upload Site Video"),
|
| 447 |
-
outputs=[
|
| 448 |
gr.Markdown(label="Detected Safety Violations"),
|
| 449 |
gr.Textbox(label="Compliance Score"),
|
| 450 |
gr.Markdown(label="Snapshots"),
|
|
@@ -458,4 +441,4 @@ interface = gr.Interface(
|
|
| 458 |
|
| 459 |
if __name__ == "__main__":
|
| 460 |
logger.info("Launching Safety Analyzer App...")
|
| 461 |
-
interface.launch()
|
|
|
|
| 235 |
"no_harness": 30,
|
| 236 |
"unsafe_posture": 20
|
| 237 |
}
|
| 238 |
+
total_penalty = sum(penalties.get(v["violation"], 0) for v in violations)
|
| 239 |
+
logger.info(f"Total Penalty: {total_penalty}")
|
| 240 |
+
score = 100 - total_penalty
|
| 241 |
+
logger.info(f"Calculated Score: {score}")
|
| 242 |
return max(score, 0)
|
| 243 |
|
| 244 |
def process_video(video_data):
|
|
|
|
| 258 |
fps = video.get(cv2.CAP_PROP_FPS)
|
| 259 |
|
| 260 |
snapshot_taken = {label: False for label in CONFIG["VIOLATION_LABELS"].values()}
|
| 261 |
+
workers = [] # List to track workers
|
| 262 |
|
| 263 |
while True:
|
| 264 |
ret, frame = video.read()
|
|
|
|
| 279 |
for box in result.boxes:
|
| 280 |
cls, conf = int(box.cls), float(box.conf)
|
| 281 |
label = CONFIG["VIOLATION_LABELS"].get(cls, None)
|
| 282 |
+
|
| 283 |
+
# Log detected violations
|
| 284 |
+
if label:
|
| 285 |
+
logger.info(f"Violation Detected: {label} with confidence: {conf}")
|
| 286 |
+
|
| 287 |
if label not in CONFIG["VIOLATION_LABELS"].values() or conf < CONFIG["CONFIDENCE_THRESHOLD"]:
|
| 288 |
continue
|
| 289 |
|
|
|
|
| 296 |
"frame": frame_count
|
| 297 |
})
|
| 298 |
|
| 299 |
+
# Process detections and workers
|
| 300 |
for detection in current_detections:
|
| 301 |
matched_worker = None
|
| 302 |
max_iou = 0
|
|
|
|
| 318 |
"timestamp": detection["timestamp"],
|
| 319 |
"worker_id": matched_worker["id"]
|
| 320 |
})
|
| 321 |
+
snapshot_taken[detection["violation"]] = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
matched_worker["bbox"] = detection["bounding_box"]
|
| 323 |
matched_worker["last_frame"] = frame_count
|
| 324 |
else:
|
|
|
|
| 338 |
"timestamp": detection["timestamp"],
|
| 339 |
"worker_id": worker_id
|
| 340 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
frame_count += 1
|
| 343 |
|
|
|
|
| 427 |
interface = gr.Interface(
|
| 428 |
fn=gradio_interface,
|
| 429 |
inputs=gr.Video(label="Upload Site Video"),
|
| 430 |
+
outputs=[
|
| 431 |
gr.Markdown(label="Detected Safety Violations"),
|
| 432 |
gr.Textbox(label="Compliance Score"),
|
| 433 |
gr.Markdown(label="Snapshots"),
|
|
|
|
| 441 |
|
| 442 |
if __name__ == "__main__":
|
| 443 |
logger.info("Launching Safety Analyzer App...")
|
| 444 |
+
interface.launch()
|