PrashanthB461 commited on
Commit
ca9201c
·
verified ·
1 Parent(s): 3bcf6dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -39,7 +39,6 @@ CONFIG = {
39
  },
40
  "PUBLIC_URL_BASE": "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/static/output/",
41
  "FRAME_SKIP": 15,
42
- "MAX_PROCESSING_TIME": 30, # Updated to 30 seconds
43
  "CONFIDENCE_THRESHOLD": 0.5
44
  }
45
 
@@ -210,7 +209,7 @@ def calculate_safety_score(violations):
210
  score -= penalties[v["violation"]]
211
  return max(score, 0)
212
 
213
- def process_video(video_data):
214
  try:
215
  video_path = os.path.join(CONFIG["OUTPUT_DIR"], f"temp_{int(time.time())}.mp4")
216
  with open(video_path, "wb") as f:
@@ -221,11 +220,11 @@ def process_video(video_data):
221
  if not video.isOpened():
222
  raise ValueError("Could not open video file")
223
 
224
- violations, snapshots = [], []
225
- frame_count = 0
226
- start_time = time.time()
227
  fps = video.get(cv2.CAP_PROP_FPS)
228
 
 
 
229
  snapshot_taken = {"no_helmet": False, "no_harness": False, "unsafe_posture": False}
230
 
231
  while True:
@@ -235,12 +234,10 @@ def process_video(video_data):
235
 
236
  if frame_count % CONFIG["FRAME_SKIP"] != 0:
237
  frame_count += 1
 
 
238
  continue
239
 
240
- if time.time() - start_time > CONFIG["MAX_PROCESSING_TIME"]:
241
- logger.info("Processing time limit of 30 seconds reached")
242
- break
243
-
244
  results = model(frame, device=device)
245
  seen_violations = set()
246
  for result in results:
@@ -279,6 +276,9 @@ def process_video(video_data):
279
 
280
  frame_count += 1
281
 
 
 
 
282
  video.release()
283
  os.remove(video_path)
284
 
@@ -316,16 +316,17 @@ def process_video(video_data):
316
  "message": f"Error processing video: {e}"
317
  }
318
 
319
- def gradio_interface(video_file):
320
  if not video_file:
321
  return "No file uploaded.", "", "No file uploaded.", "", ""
 
322
  try:
323
  with open(video_file, "rb") as f:
324
  video_data = f.read()
325
- result = process_video(video_data)
 
326
 
327
  if result.get("message"):
328
- # If message present (either no violations or error), show it plainly
329
  return result["message"], "", "", "", ""
330
 
331
  violation_table = "No violations detected."
@@ -370,7 +371,8 @@ interface = gr.Interface(
370
  gr.Textbox(label="Violation Details URL")
371
  ],
372
  title="Worksite Safety Violation Analyzer",
373
- description="Upload site videos to detect safety violations (No Helmet Violation, No Harness Violation, Unsafe Posture Violation). Non-violations are ignored."
 
374
  )
375
 
376
  if __name__ == "__main__":
 
39
  },
40
  "PUBLIC_URL_BASE": "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/static/output/",
41
  "FRAME_SKIP": 15,
 
42
  "CONFIDENCE_THRESHOLD": 0.5
43
  }
44
 
 
209
  score -= penalties[v["violation"]]
210
  return max(score, 0)
211
 
212
+ def process_video(video_data, progress_callback=None):
213
  try:
214
  video_path = os.path.join(CONFIG["OUTPUT_DIR"], f"temp_{int(time.time())}.mp4")
215
  with open(video_path, "wb") as f:
 
220
  if not video.isOpened():
221
  raise ValueError("Could not open video file")
222
 
223
+ total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
 
 
224
  fps = video.get(cv2.CAP_PROP_FPS)
225
 
226
+ violations, snapshots = [], []
227
+ frame_count = 0
228
  snapshot_taken = {"no_helmet": False, "no_harness": False, "unsafe_posture": False}
229
 
230
  while True:
 
234
 
235
  if frame_count % CONFIG["FRAME_SKIP"] != 0:
236
  frame_count += 1
237
+ if progress_callback:
238
+ progress_callback(min(frame_count / total_frames, 1.0))
239
  continue
240
 
 
 
 
 
241
  results = model(frame, device=device)
242
  seen_violations = set()
243
  for result in results:
 
276
 
277
  frame_count += 1
278
 
279
+ if progress_callback:
280
+ progress_callback(min(frame_count / total_frames, 1.0))
281
+
282
  video.release()
283
  os.remove(video_path)
284
 
 
316
  "message": f"Error processing video: {e}"
317
  }
318
 
319
+ def gradio_interface(video_file, progress=gr.Progress(track_tqdm=True)):
320
  if not video_file:
321
  return "No file uploaded.", "", "No file uploaded.", "", ""
322
+
323
  try:
324
  with open(video_file, "rb") as f:
325
  video_data = f.read()
326
+
327
+ result = process_video(video_data, progress_callback=progress)
328
 
329
  if result.get("message"):
 
330
  return result["message"], "", "", "", ""
331
 
332
  violation_table = "No violations detected."
 
371
  gr.Textbox(label="Violation Details URL")
372
  ],
373
  title="Worksite Safety Violation Analyzer",
374
+ description="Upload site videos to detect safety violations (No Helmet Violation, No Harness Violation, Unsafe Posture Violation). Non-violations are ignored.",
375
+ live=False
376
  )
377
 
378
  if __name__ == "__main__":