Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -113,6 +113,7 @@ def generate_violation_pdf(violations, score):
|
|
| 113 |
|
| 114 |
c.showPage()
|
| 115 |
c.save()
|
|
|
|
| 116 |
pdf_file.seek(0)
|
| 117 |
|
| 118 |
with open(pdf_path, "wb") as f:
|
|
@@ -225,13 +226,20 @@ def process_video(video_data):
|
|
| 225 |
violations, snapshots = [], []
|
| 226 |
frame_count = 0
|
| 227 |
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 228 |
-
start_time = time.time()
|
| 229 |
fps = video.get(cv2.CAP_PROP_FPS)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
max_frames = int(fps * CONFIG["MAX_PROCESSING_TIME"]) # Max frames for 60 seconds
|
| 231 |
|
| 232 |
while True:
|
| 233 |
ret, frame = video.read()
|
| 234 |
-
if not ret
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
break
|
| 236 |
|
| 237 |
results = model(frame, device=device)
|
|
@@ -252,6 +260,7 @@ def process_video(video_data):
|
|
| 252 |
"timestamp": frame_count / max(fps, 1)
|
| 253 |
}
|
| 254 |
violations.append(violation)
|
|
|
|
| 255 |
|
| 256 |
snapshot_path = os.path.join(CONFIG["OUTPUT_DIR"], f"snapshot_{frame_count}_{label}.jpg")
|
| 257 |
cv2.imwrite(snapshot_path, frame)
|
|
@@ -265,13 +274,14 @@ def process_video(video_data):
|
|
| 265 |
})
|
| 266 |
|
| 267 |
frame_count += 1
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
|
| 272 |
video.release()
|
| 273 |
os.remove(video_path)
|
| 274 |
-
score = calculate_safety_score(violations,
|
|
|
|
| 275 |
pdf_path, pdf_url, pdf_file = generate_violation_pdf(violations, score)
|
| 276 |
report_id, final_pdf_url = push_report_to_salesforce(violations, score, pdf_path, pdf_file)
|
| 277 |
|
|
@@ -280,16 +290,20 @@ def process_video(video_data):
|
|
| 280 |
"snapshots": snapshots,
|
| 281 |
"score": score,
|
| 282 |
"salesforce_record_id": report_id,
|
| 283 |
-
"violation_details_url": final_pdf_url
|
|
|
|
|
|
|
| 284 |
}
|
| 285 |
except Exception as e:
|
| 286 |
-
logger.error(f"Error processing video: {e}")
|
| 287 |
return {
|
| 288 |
"violations": [],
|
| 289 |
"snapshots": [],
|
| 290 |
"score": 0,
|
| 291 |
"salesforce_record_id": None,
|
| 292 |
-
"violation_details_url": ""
|
|
|
|
|
|
|
| 293 |
}
|
| 294 |
|
| 295 |
# ==========================
|
|
@@ -297,7 +311,7 @@ def process_video(video_data):
|
|
| 297 |
# ==========================
|
| 298 |
def gradio_interface(video_file):
|
| 299 |
if not video_file:
|
| 300 |
-
return "No file uploaded.", "", "No file uploaded.", "", ""
|
| 301 |
try:
|
| 302 |
with open(video_file, "rb") as f:
|
| 303 |
video_data = f.read()
|
|
@@ -332,24 +346,26 @@ def gradio_interface(video_file):
|
|
| 332 |
f"Safety Score: {result['score']}%",
|
| 333 |
snapshots_text,
|
| 334 |
f"Salesforce Record ID: {result['salesforce_record_id'] or 'N/A'}",
|
| 335 |
-
result["violation_details_url"] or "N/A"
|
|
|
|
| 336 |
)
|
| 337 |
except Exception as e:
|
| 338 |
logger.error(f"Error in Gradio interface: {e}", exc_info=True)
|
| 339 |
-
return f"Error: {str(e)}", "", "Error in processing.", "", ""
|
| 340 |
|
| 341 |
interface = gr.Interface(
|
| 342 |
fn=gradio_interface,
|
| 343 |
-
inputs=gr.Video(label="Upload Site Video"),
|
| 344 |
outputs=[
|
| 345 |
gr.Markdown(label="Detected Safety Violations"),
|
| 346 |
gr.Textbox(label="Compliance Score"),
|
| 347 |
gr.Markdown(label="Snapshots"),
|
| 348 |
gr.Textbox(label="Salesforce Record ID"),
|
| 349 |
-
gr.Textbox(label="Violation Details URL")
|
|
|
|
| 350 |
],
|
| 351 |
title="Worksite Safety Violation Analyzer",
|
| 352 |
-
description="Upload a video (up to 1 minute) to detect safety violations (missing helmet, missing harness, unsafe posture)."
|
| 353 |
)
|
| 354 |
|
| 355 |
if __name__ == "__main__":
|
|
|
|
| 113 |
|
| 114 |
c.showPage()
|
| 115 |
c.save()
|
| 116 |
+
_cookie = None
|
| 117 |
pdf_file.seek(0)
|
| 118 |
|
| 119 |
with open(pdf_path, "wb") as f:
|
|
|
|
| 226 |
violations, snapshots = [], []
|
| 227 |
frame_count = 0
|
| 228 |
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
|
|
|
| 229 |
fps = video.get(cv2.CAP_PROP_FPS)
|
| 230 |
+
video_duration = total_frames / max(fps, 1)
|
| 231 |
+
logger.info(f"Video duration: {video_duration:.2f} seconds, Total frames: {total_frames}, FPS: {fps:.2f}")
|
| 232 |
+
|
| 233 |
+
start_time = time.time()
|
| 234 |
max_frames = int(fps * CONFIG["MAX_PROCESSING_TIME"]) # Max frames for 60 seconds
|
| 235 |
|
| 236 |
while True:
|
| 237 |
ret, frame = video.read()
|
| 238 |
+
if not ret:
|
| 239 |
+
logger.info(f"End of video reached at frame {frame_count}")
|
| 240 |
+
break
|
| 241 |
+
if frame_count >= max_frames:
|
| 242 |
+
logger.info(f"Reached maximum processing limit at frame {frame_count}")
|
| 243 |
break
|
| 244 |
|
| 245 |
results = model(frame, device=device)
|
|
|
|
| 260 |
"timestamp": frame_count / max(fps, 1)
|
| 261 |
}
|
| 262 |
violations.append(violation)
|
| 263 |
+
logger.debug(f"Violation detected: {label} at frame {frame_count}, timestamp {violation['timestamp']:.2f}s")
|
| 264 |
|
| 265 |
snapshot_path = os.path.join(CONFIG["OUTPUT_DIR"], f"snapshot_{frame_count}_{label}.jpg")
|
| 266 |
cv2.imwrite(snapshot_path, frame)
|
|
|
|
| 274 |
})
|
| 275 |
|
| 276 |
frame_count += 1
|
| 277 |
+
|
| 278 |
+
processing_time = time.time() - start_time
|
| 279 |
+
logger.info(f"Processed {frame_count} frames in {processing_time:.2f} seconds")
|
| 280 |
|
| 281 |
video.release()
|
| 282 |
os.remove(video_path)
|
| 283 |
+
score = calculate_safety_score(violations, frame_count)
|
| 284 |
+
logger.info(f"Safety score calculated: {score}% with {len(violations)} violations")
|
| 285 |
pdf_path, pdf_url, pdf_file = generate_violation_pdf(violations, score)
|
| 286 |
report_id, final_pdf_url = push_report_to_salesforce(violations, score, pdf_path, pdf_file)
|
| 287 |
|
|
|
|
| 290 |
"snapshots": snapshots,
|
| 291 |
"score": score,
|
| 292 |
"salesforce_record_id": report_id,
|
| 293 |
+
"violation_details_url": final_pdf_url,
|
| 294 |
+
"processed_frames": frame_count,
|
| 295 |
+
"video_duration": video_duration
|
| 296 |
}
|
| 297 |
except Exception as e:
|
| 298 |
+
logger.error(f"Error processing video: {e}", exc_info=True)
|
| 299 |
return {
|
| 300 |
"violations": [],
|
| 301 |
"snapshots": [],
|
| 302 |
"score": 0,
|
| 303 |
"salesforce_record_id": None,
|
| 304 |
+
"violation_details_url": "",
|
| 305 |
+
"processed_frames": 0,
|
| 306 |
+
"video_duration": 0
|
| 307 |
}
|
| 308 |
|
| 309 |
# ==========================
|
|
|
|
| 311 |
# ==========================
|
| 312 |
def gradio_interface(video_file):
|
| 313 |
if not video_file:
|
| 314 |
+
return "No file uploaded.", "", "No file uploaded.", "", "", ""
|
| 315 |
try:
|
| 316 |
with open(video_file, "rb") as f:
|
| 317 |
video_data = f.read()
|
|
|
|
| 346 |
f"Safety Score: {result['score']}%",
|
| 347 |
snapshots_text,
|
| 348 |
f"Salesforce Record ID: {result['salesforce_record_id'] or 'N/A'}",
|
| 349 |
+
result["violation_details_url"] or "N/A",
|
| 350 |
+
f"Processed {result['processed_frames']} frames (Video duration: {result['video_duration']:.2f} seconds)"
|
| 351 |
)
|
| 352 |
except Exception as e:
|
| 353 |
logger.error(f"Error in Gradio interface: {e}", exc_info=True)
|
| 354 |
+
return f"Error: {str(e)}", "", "Error in processing.", "", "", ""
|
| 355 |
|
| 356 |
interface = gr.Interface(
|
| 357 |
fn=gradio_interface,
|
| 358 |
+
inputs=gr.Video(label="Upload Site Video (Up to 1 Minute)"),
|
| 359 |
outputs=[
|
| 360 |
gr.Markdown(label="Detected Safety Violations"),
|
| 361 |
gr.Textbox(label="Compliance Score"),
|
| 362 |
gr.Markdown(label="Snapshots"),
|
| 363 |
gr.Textbox(label="Salesforce Record ID"),
|
| 364 |
+
gr.Textbox(label="Violation Details URL"),
|
| 365 |
+
gr.Textbox(label="Processing Summary")
|
| 366 |
],
|
| 367 |
title="Worksite Safety Violation Analyzer",
|
| 368 |
+
description="Upload a video (up to 1 minute) to detect safety violations (missing helmet, missing harness, unsafe posture). Violations are only recorded when safety gear is missing or posture is unsafe."
|
| 369 |
)
|
| 370 |
|
| 371 |
if __name__ == "__main__":
|