PrashanthB461 commited on
Commit
cc1585c
·
verified ·
1 Parent(s): 06a63ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -18
app.py CHANGED
@@ -34,8 +34,6 @@ CONFIG = {
34
  "domain": "login"
35
  },
36
  "PUBLIC_URL_BASE": "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/static/output/",
37
- "FRAME_SKIP": 5,
38
- "MAX_PROCESSING_TIME": 30
39
  }
40
 
41
  # Setup logging
@@ -74,8 +72,7 @@ def connect_to_salesforce():
74
  try:
75
  sf = Salesforce(**CONFIG["SF_CREDENTIALS"])
76
  logger.info("Connected to Salesforce")
77
- # Verify object accessibility
78
- sf.describe() # Fetch metadata to ensure connection
79
  logger.debug("Salesforce object metadata fetched successfully")
80
  return sf
81
  except Exception as e:
@@ -175,7 +172,6 @@ def push_report_to_salesforce(violations, score, pdf_path, pdf_file):
175
  record = sf.Safety_Video_Report__c.create(record_data)
176
  except Exception as e:
177
  logger.error(f"Failed to create Safety_Video_Report__c: {e}")
178
- # Fallback to a standard object for debugging
179
  record = sf.Account.create({"Name": f"Safety_Report_{int(time.time())}"})
180
  logger.warning(f"Fell back to creating Account record: {record}")
181
  record_id = record["id"]
@@ -188,7 +184,6 @@ def push_report_to_salesforce(violations, score, pdf_path, pdf_file):
188
  sf.Safety_Video_Report__c.update(record_id, {"PDF_Report_URL__c": uploaded_url})
189
  except Exception as e:
190
  logger.error(f"Failed to update Safety_Video_Report__c: {e}")
191
- # Fallback update
192
  sf.Account.update(record_id, {"Description": uploaded_url})
193
  pdf_url = uploaded_url
194
  logger.debug(f"Updated record {record_id} with PDF URL: {pdf_url}")
@@ -205,9 +200,9 @@ def calculate_safety_score(violations):
205
  penalties = {
206
  "no_helmet": 25,
207
  "no_harness": 30,
208
- "unsafe_posture": 20,
209
- "unsafe_zone": 25
210
  }
 
211
  score = 100 - sum(penalties.get(v["violation"], 0) for v in violations)
212
  return max(score, 0)
213
 
@@ -227,15 +222,11 @@ def process_video(video_data):
227
 
228
  violations, snapshots = [], []
229
  frame_count = 0
230
- start_time = time.time()
231
 
232
  while True:
233
  ret, frame = video.read()
234
  if not ret:
235
  break
236
- if frame_count % CONFIG["FRAME_SKIP"] != 0:
237
- frame_count += 1
238
- continue
239
 
240
  results = model(frame, device=device)
241
  seen_violations = set() # Track unique violations in this frame
@@ -243,6 +234,9 @@ def process_video(video_data):
243
  for box in result.boxes:
244
  cls, conf = int(box.cls), float(box.conf)
245
  label = CONFIG["VIOLATION_LABELS"].get(cls, f"class_{cls}")
 
 
 
246
  if label in seen_violations:
247
  continue # Skip if this violation type was already recorded in this frame
248
  seen_violations.add(label)
@@ -258,7 +252,6 @@ def process_video(video_data):
258
 
259
  snapshot_path = os.path.join(CONFIG["OUTPUT_DIR"], f"snapshot_{frame_count}_{label}.jpg")
260
  cv2.imwrite(snapshot_path, frame)
261
- # Encode snapshot as base64 for immediate display
262
  with open(snapshot_path, "rb") as img_file:
263
  img_base64 = base64.b64encode(img_file.read()).decode('utf-8')
264
  snapshots.append({
@@ -269,9 +262,6 @@ def process_video(video_data):
269
  })
270
 
271
  frame_count += 1
272
- if time.time() - start_time > CONFIG["MAX_PROCESSING_TIME"]:
273
- logger.warning("Processing time limit exceeded")
274
- break
275
 
276
  video.release()
277
  os.remove(video_path)
@@ -307,7 +297,6 @@ def gradio_interface(video_file):
307
  video_data = f.read()
308
  result = process_video(video_data)
309
 
310
- # Format violations as a Markdown table
311
  violation_table = "No violations detected."
312
  if result["violations"]:
313
  header = "| Violation | Timestamp | Confidence | Bounding Box | Violation Details |\n"
@@ -323,7 +312,6 @@ def gradio_interface(video_file):
323
  rows.append(row)
324
  violation_table = header + separator + "\n".join(rows)
325
 
326
- # Format snapshots as a bullet list with base64 images
327
  snapshots_text = "No snapshots captured."
328
  if result["snapshots"]:
329
  snapshots_text = "\n".join(
 
34
  "domain": "login"
35
  },
36
  "PUBLIC_URL_BASE": "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/static/output/",
 
 
37
  }
38
 
39
  # Setup logging
 
72
  try:
73
  sf = Salesforce(**CONFIG["SF_CREDENTIALS"])
74
  logger.info("Connected to Salesforce")
75
+ sf.describe()
 
76
  logger.debug("Salesforce object metadata fetched successfully")
77
  return sf
78
  except Exception as e:
 
172
  record = sf.Safety_Video_Report__c.create(record_data)
173
  except Exception as e:
174
  logger.error(f"Failed to create Safety_Video_Report__c: {e}")
 
175
  record = sf.Account.create({"Name": f"Safety_Report_{int(time.time())}"})
176
  logger.warning(f"Fell back to creating Account record: {record}")
177
  record_id = record["id"]
 
184
  sf.Safety_Video_Report__c.update(record_id, {"PDF_Report_URL__c": uploaded_url})
185
  except Exception as e:
186
  logger.error(f"Failed to update Safety_Video_Report__c: {e}")
 
187
  sf.Account.update(record_id, {"Description": uploaded_url})
188
  pdf_url = uploaded_url
189
  logger.debug(f"Updated record {record_id} with PDF URL: {pdf_url}")
 
200
  penalties = {
201
  "no_helmet": 25,
202
  "no_harness": 30,
203
+ "unsafe_posture": 20
 
204
  }
205
+ # Only penalize for detected violations (no_helmet, no_harness, unsafe_posture)
206
  score = 100 - sum(penalties.get(v["violation"], 0) for v in violations)
207
  return max(score, 0)
208
 
 
222
 
223
  violations, snapshots = [], []
224
  frame_count = 0
 
225
 
226
  while True:
227
  ret, frame = video.read()
228
  if not ret:
229
  break
 
 
 
230
 
231
  results = model(frame, device=device)
232
  seen_violations = set() # Track unique violations in this frame
 
234
  for box in result.boxes:
235
  cls, conf = int(box.cls), float(box.conf)
236
  label = CONFIG["VIOLATION_LABELS"].get(cls, f"class_{cls}")
237
+ # Only consider "no_helmet", "no_harness", "unsafe_posture" as violations
238
+ if label not in ["no_helmet", "no_harness", "unsafe_posture"]:
239
+ continue
240
  if label in seen_violations:
241
  continue # Skip if this violation type was already recorded in this frame
242
  seen_violations.add(label)
 
252
 
253
  snapshot_path = os.path.join(CONFIG["OUTPUT_DIR"], f"snapshot_{frame_count}_{label}.jpg")
254
  cv2.imwrite(snapshot_path, frame)
 
255
  with open(snapshot_path, "rb") as img_file:
256
  img_base64 = base64.b64encode(img_file.read()).decode('utf-8')
257
  snapshots.append({
 
262
  })
263
 
264
  frame_count += 1
 
 
 
265
 
266
  video.release()
267
  os.remove(video_path)
 
297
  video_data = f.read()
298
  result = process_video(video_data)
299
 
 
300
  violation_table = "No violations detected."
301
  if result["violations"]:
302
  header = "| Violation | Timestamp | Confidence | Bounding Box | Violation Details |\n"
 
312
  rows.append(row)
313
  violation_table = header + separator + "\n".join(rows)
314
 
 
315
  snapshots_text = "No snapshots captured."
316
  if result["snapshots"]:
317
  snapshots_text = "\n".join(