Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import gradio as gr
|
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
from simple_salesforce import Salesforce
|
|
|
|
| 7 |
|
| 8 |
# Salesforce connection details (replace these with your actual credentials)
|
| 9 |
sf = Salesforce(username='prashanth@safetyanaluzer.com', password='SaiPrash461', security_token='MOA3BXBfGyqnjBneog8a9IcGw')
|
|
@@ -39,7 +40,7 @@ except Exception as e:
|
|
| 39 |
device = torch.device("cpu")
|
| 40 |
|
| 41 |
# ==========================
|
| 42 |
-
# Load Model
|
| 43 |
# ==========================
|
| 44 |
if os.path.isfile(MODEL_PATH):
|
| 45 |
selected_model = MODEL_PATH
|
|
@@ -58,7 +59,7 @@ except Exception as e:
|
|
| 58 |
# ==========================
|
| 59 |
# Video Processing with Optimizations
|
| 60 |
# ==========================
|
| 61 |
-
def process_video(video_path, frame_skip=5):
|
| 62 |
try:
|
| 63 |
video = cv2.VideoCapture(video_path)
|
| 64 |
if not video.isOpened():
|
|
@@ -67,17 +68,19 @@ def process_video(video_path, frame_skip=5):
|
|
| 67 |
frame_count = 0
|
| 68 |
violations = []
|
| 69 |
processed_frame_count = 0
|
|
|
|
| 70 |
|
| 71 |
while True:
|
| 72 |
ret, frame = video.read()
|
| 73 |
if not ret:
|
| 74 |
break
|
| 75 |
|
| 76 |
-
# Skip frames to reduce processing time
|
| 77 |
if frame_count % frame_skip != 0:
|
| 78 |
frame_count += 1
|
| 79 |
continue
|
| 80 |
|
|
|
|
| 81 |
results = model(frame, device=device)
|
| 82 |
|
| 83 |
for result in results:
|
|
@@ -96,7 +99,15 @@ def process_video(video_path, frame_skip=5):
|
|
| 96 |
|
| 97 |
frame_count += 1
|
| 98 |
processed_frame_count += 1
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
break
|
| 101 |
|
| 102 |
video.release()
|
|
@@ -184,6 +195,7 @@ def upload_video_to_salesforce(video_path):
|
|
| 184 |
except Exception as e:
|
| 185 |
print(f"❌ Error uploading video to Salesforce: {e}")
|
| 186 |
return None
|
|
|
|
| 187 |
# ==========================
|
| 188 |
# Gradio Interface
|
| 189 |
# ==========================
|
|
|
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
from simple_salesforce import Salesforce
|
| 7 |
+
import time
|
| 8 |
|
| 9 |
# Salesforce connection details (replace these with your actual credentials)
|
| 10 |
sf = Salesforce(username='prashanth@safetyanaluzer.com', password='SaiPrash461', security_token='MOA3BXBfGyqnjBneog8a9IcGw')
|
|
|
|
| 40 |
device = torch.device("cpu")
|
| 41 |
|
| 42 |
# ==========================
|
| 43 |
+
# Load Model (Use YOLOv8n for Faster Inference)
|
| 44 |
# ==========================
|
| 45 |
if os.path.isfile(MODEL_PATH):
|
| 46 |
selected_model = MODEL_PATH
|
|
|
|
| 59 |
# ==========================
|
| 60 |
# Video Processing with Optimizations
|
| 61 |
# ==========================
|
| 62 |
+
def process_video(video_path, frame_skip=5, max_frames=100):
|
| 63 |
try:
|
| 64 |
video = cv2.VideoCapture(video_path)
|
| 65 |
if not video.isOpened():
|
|
|
|
| 68 |
frame_count = 0
|
| 69 |
violations = []
|
| 70 |
processed_frame_count = 0
|
| 71 |
+
start_time = time.time()
|
| 72 |
|
| 73 |
while True:
|
| 74 |
ret, frame = video.read()
|
| 75 |
if not ret:
|
| 76 |
break
|
| 77 |
|
| 78 |
+
# Skip frames to reduce processing time (process only every 'frame_skip' frame)
|
| 79 |
if frame_count % frame_skip != 0:
|
| 80 |
frame_count += 1
|
| 81 |
continue
|
| 82 |
|
| 83 |
+
# Model inference for detecting violations
|
| 84 |
results = model(frame, device=device)
|
| 85 |
|
| 86 |
for result in results:
|
|
|
|
| 99 |
|
| 100 |
frame_count += 1
|
| 101 |
processed_frame_count += 1
|
| 102 |
+
|
| 103 |
+
# Stop processing after a fixed number of frames to save time
|
| 104 |
+
if processed_frame_count >= max_frames:
|
| 105 |
+
break
|
| 106 |
+
|
| 107 |
+
# Check elapsed time, stop if we exceed 30 seconds
|
| 108 |
+
elapsed_time = time.time() - start_time
|
| 109 |
+
if elapsed_time > 30:
|
| 110 |
+
print("⏰ Exceeded 30 seconds of processing time.")
|
| 111 |
break
|
| 112 |
|
| 113 |
video.release()
|
|
|
|
| 195 |
except Exception as e:
|
| 196 |
print(f"❌ Error uploading video to Salesforce: {e}")
|
| 197 |
return None
|
| 198 |
+
|
| 199 |
# ==========================
|
| 200 |
# Gradio Interface
|
| 201 |
# ==========================
|