Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,10 +9,8 @@ import pickle
|
|
9 |
import io
|
10 |
from googleapiclient.discovery import build
|
11 |
from googleapiclient.http import MediaFileUpload
|
12 |
-
from google_auth_oauthlib.flow import InstalledAppFlow
|
13 |
from google.auth.transport.requests import Request
|
14 |
from PIL import Image
|
15 |
-
|
16 |
from simple_salesforce import Salesforce
|
17 |
from datetime import datetime
|
18 |
import uuid
|
@@ -22,11 +20,10 @@ sf = Salesforce(
|
|
22 |
username='suriraja822@agentforce.com',
|
23 |
password='Sati@1010',
|
24 |
security_token='B9nS0HEyBE7YmWllqXCyiOJpY',
|
25 |
-
domain='login'
|
26 |
)
|
27 |
|
28 |
def log_intruder_to_salesforce(frame_id):
|
29 |
-
"""Logs a detection event to Salesforce."""
|
30 |
try:
|
31 |
sf.Hotel_Security_Alert__c.create({
|
32 |
'Alert_Type__c': 'Intruder',
|
@@ -41,10 +38,6 @@ def log_intruder_to_salesforce(frame_id):
|
|
41 |
except Exception as e:
|
42 |
st.warning(f"⚠️ Failed to log to Salesforce: {e}")
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
# === CONFIGURATION ===
|
49 |
API_KEY = "AIzaSyDojJrpauA0XZtCCDUuo9xeQHZQamYKsC4"
|
50 |
CCTVFEED_IDS = ['1KJRkSf2SKEZ1mXS9_si65IwMBtjs6p4n']
|
@@ -54,23 +47,18 @@ FRAME_SKIP = st.sidebar.slider("⏩ Frame Skip", 1, 15, 5)
|
|
54 |
|
55 |
# === SETUP ===
|
56 |
mp_face_detection = mp.solutions.face_detection
|
57 |
-
mp_drawing = mp.solutions.drawing_utils
|
58 |
face_detector = mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.5)
|
59 |
|
60 |
def mse(img1, img2):
|
61 |
try:
|
62 |
if img1.shape != img2.shape:
|
63 |
img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))
|
64 |
-
|
65 |
-
return err
|
66 |
except Exception:
|
67 |
return float("inf")
|
68 |
|
69 |
def is_duplicate(img, img_list, threshold=200):
|
70 |
-
for existing in img_list
|
71 |
-
if mse(img, existing) < threshold:
|
72 |
-
return True
|
73 |
-
return False
|
74 |
|
75 |
def get_drive_file_links(folder_id, filter_video=False):
|
76 |
url = f"https://www.googleapis.com/drive/v3/files?q='{folder_id}'+in+parents&key={API_KEY}&fields=files(id,name,mimeType)"
|
@@ -145,9 +133,8 @@ def detect_faces_from_video(video_path, registered_faces):
|
|
145 |
if not any(mse(f, reg) < 200 for reg in registered_faces) and not is_duplicate(f, unique):
|
146 |
unique.append(f)
|
147 |
filename = f"intruder_{frame_id}.jpg"
|
148 |
-
upload_to_drive(f, filename)
|
149 |
-
log_intruder_to_salesforce(frame_id)
|
150 |
-
|
151 |
detected.extend(faces)
|
152 |
cap.release()
|
153 |
return detected, unique
|
@@ -188,4 +175,4 @@ with st.spinner("Processing videos..."):
|
|
188 |
os.remove(vfile)
|
189 |
total_uploaded += len(unique_faces)
|
190 |
|
191 |
-
st.info(
|
|
|
9 |
import io
|
10 |
from googleapiclient.discovery import build
|
11 |
from googleapiclient.http import MediaFileUpload
|
|
|
12 |
from google.auth.transport.requests import Request
|
13 |
from PIL import Image
|
|
|
14 |
from simple_salesforce import Salesforce
|
15 |
from datetime import datetime
|
16 |
import uuid
|
|
|
20 |
username='suriraja822@agentforce.com',
|
21 |
password='Sati@1010',
|
22 |
security_token='B9nS0HEyBE7YmWllqXCyiOJpY',
|
23 |
+
domain='login'
|
24 |
)
|
25 |
|
26 |
def log_intruder_to_salesforce(frame_id):
|
|
|
27 |
try:
|
28 |
sf.Hotel_Security_Alert__c.create({
|
29 |
'Alert_Type__c': 'Intruder',
|
|
|
38 |
except Exception as e:
|
39 |
st.warning(f"⚠️ Failed to log to Salesforce: {e}")
|
40 |
|
|
|
|
|
|
|
|
|
41 |
# === CONFIGURATION ===
|
42 |
API_KEY = "AIzaSyDojJrpauA0XZtCCDUuo9xeQHZQamYKsC4"
|
43 |
CCTVFEED_IDS = ['1KJRkSf2SKEZ1mXS9_si65IwMBtjs6p4n']
|
|
|
47 |
|
48 |
# === SETUP ===
|
49 |
mp_face_detection = mp.solutions.face_detection
|
|
|
50 |
face_detector = mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.5)
|
51 |
|
52 |
def mse(img1, img2):
|
53 |
try:
|
54 |
if img1.shape != img2.shape:
|
55 |
img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))
|
56 |
+
return np.mean((img1.astype("float") - img2.astype("float")) ** 2)
|
|
|
57 |
except Exception:
|
58 |
return float("inf")
|
59 |
|
60 |
def is_duplicate(img, img_list, threshold=200):
|
61 |
+
return any(mse(img, existing) < threshold for existing in img_list)
|
|
|
|
|
|
|
62 |
|
63 |
def get_drive_file_links(folder_id, filter_video=False):
|
64 |
url = f"https://www.googleapis.com/drive/v3/files?q='{folder_id}'+in+parents&key={API_KEY}&fields=files(id,name,mimeType)"
|
|
|
133 |
if not any(mse(f, reg) < 200 for reg in registered_faces) and not is_duplicate(f, unique):
|
134 |
unique.append(f)
|
135 |
filename = f"intruder_{frame_id}.jpg"
|
136 |
+
upload_to_drive(f, filename)
|
137 |
+
log_intruder_to_salesforce(frame_id)
|
|
|
138 |
detected.extend(faces)
|
139 |
cap.release()
|
140 |
return detected, unique
|
|
|
175 |
os.remove(vfile)
|
176 |
total_uploaded += len(unique_faces)
|
177 |
|
178 |
+
st.info("🎯 Completed processing all videos.")
|