Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import os
|
2 |
import cv2
|
3 |
-
import face_recognition
|
4 |
import numpy as np
|
5 |
import time
|
6 |
import pickle
|
@@ -8,11 +7,13 @@ from googleapiclient.discovery import build
|
|
8 |
from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload
|
9 |
from io import BytesIO
|
10 |
|
|
|
11 |
# === CONFIGURATION ===
|
12 |
-
CCTVFEED_IDS = ['1KJRkSf2SKEZ1mXS9_si65IwMBtjs6p4n']
|
13 |
REG_FOLDER_ID = '1qkcR7nQTEtiMH9OFUv2bGxVn08E3dKjF'
|
14 |
-
INTRUDER_FOLDER_ID = '1PPAUWU-wMx7fek73p-hqPqYQypYtG8Ob'
|
15 |
TOKEN_PICKLE = 'token.pickle'
|
|
|
16 |
|
17 |
# === GOOGLE DRIVE AUTH ===
|
18 |
def init_gdrive():
|
@@ -37,43 +38,49 @@ def list_files(service, folder_id, mime_type='video/mp4'):
|
|
37 |
result = service.files().list(q=query, fields="files(id, name)").execute()
|
38 |
return result.get('files', [])
|
39 |
|
40 |
-
# === KNOWN
|
41 |
def load_known_faces(service):
|
42 |
known_faces = []
|
43 |
-
known_names = []
|
44 |
reg_files = list_files(service, REG_FOLDER_ID, mime_type='image/jpeg')
|
45 |
for f in reg_files:
|
46 |
fname = f"{f['id']}.jpg"
|
47 |
download_file(service, f['id'], fname)
|
48 |
-
img =
|
49 |
-
|
50 |
-
|
51 |
-
known_faces.append(
|
52 |
-
known_names.append(f['name'])
|
53 |
os.remove(fname)
|
54 |
-
return known_faces
|
55 |
|
56 |
-
# === INTRUDER
|
57 |
def upload_intruder(service, img_path):
|
58 |
file_metadata = {'name': os.path.basename(img_path), 'parents': [INTRUDER_FOLDER_ID]}
|
59 |
media = MediaFileUpload(img_path, mimetype='image/jpeg')
|
60 |
service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
61 |
|
62 |
-
# ===
|
63 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
video_capture = cv2.VideoCapture(video_path)
|
65 |
frame_count = 0
|
|
|
66 |
while True:
|
67 |
ret, frame = video_capture.read()
|
68 |
if not ret:
|
69 |
break
|
70 |
if frame_count % 30 == 0:
|
71 |
-
|
72 |
-
faces =
|
73 |
-
|
74 |
-
for
|
75 |
-
|
76 |
-
if not
|
77 |
ts = int(time.time())
|
78 |
fname = f"intruder_{ts}.jpg"
|
79 |
cv2.imwrite(fname, frame)
|
@@ -85,15 +92,15 @@ def process_video(video_path, known_faces, known_names, service):
|
|
85 |
# === MAIN ===
|
86 |
def main():
|
87 |
drive = init_gdrive()
|
88 |
-
known_faces
|
89 |
|
90 |
for folder_id in CCTVFEED_IDS:
|
91 |
videos = list_files(drive, folder_id)
|
92 |
for v in videos:
|
93 |
video_path = f"{v['id']}.mp4"
|
94 |
download_file(drive, v['id'], video_path)
|
95 |
-
process_video(video_path, known_faces,
|
96 |
os.remove(video_path)
|
97 |
|
98 |
-
if __name__ ==
|
99 |
main()
|
|
|
1 |
import os
|
2 |
import cv2
|
|
|
3 |
import numpy as np
|
4 |
import time
|
5 |
import pickle
|
|
|
7 |
from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload
|
8 |
from io import BytesIO
|
9 |
|
10 |
+
|
11 |
# === CONFIGURATION ===
|
12 |
+
CCTVFEED_IDS = ['1KJRkSf2SKEZ1mXS9_si65IwMBtjs6p4n'] # Replace with actual folder IDs
|
13 |
REG_FOLDER_ID = '1qkcR7nQTEtiMH9OFUv2bGxVn08E3dKjF'
|
14 |
+
INTRUDER_FOLDER_ID = '1PPAUWU-wMx7fek73p-hqPqYQypYtG8Ob'
|
15 |
TOKEN_PICKLE = 'token.pickle'
|
16 |
+
CASCADE_PATH = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
|
17 |
|
18 |
# === GOOGLE DRIVE AUTH ===
|
19 |
def init_gdrive():
|
|
|
38 |
result = service.files().list(q=query, fields="files(id, name)").execute()
|
39 |
return result.get('files', [])
|
40 |
|
41 |
+
# === LOAD KNOWN FACE IMAGES ===
|
42 |
def load_known_faces(service):
|
43 |
known_faces = []
|
|
|
44 |
reg_files = list_files(service, REG_FOLDER_ID, mime_type='image/jpeg')
|
45 |
for f in reg_files:
|
46 |
fname = f"{f['id']}.jpg"
|
47 |
download_file(service, f['id'], fname)
|
48 |
+
img = cv2.imread(fname, cv2.IMREAD_GRAYSCALE)
|
49 |
+
if img is not None:
|
50 |
+
img = cv2.resize(img, (100, 100))
|
51 |
+
known_faces.append(img)
|
|
|
52 |
os.remove(fname)
|
53 |
+
return known_faces
|
54 |
|
55 |
+
# === UPLOAD INTRUDER IMAGE ===
|
56 |
def upload_intruder(service, img_path):
|
57 |
file_metadata = {'name': os.path.basename(img_path), 'parents': [INTRUDER_FOLDER_ID]}
|
58 |
media = MediaFileUpload(img_path, mimetype='image/jpeg')
|
59 |
service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
60 |
|
61 |
+
# === BASIC FACE COMPARISON ===
|
62 |
+
def is_match(face_img, known_faces, threshold=2000):
|
63 |
+
face_img = cv2.resize(face_img, (100, 100))
|
64 |
+
errors = [np.mean((face_img - kf) ** 2) for kf in known_faces]
|
65 |
+
return any(err < threshold for err in errors)
|
66 |
+
|
67 |
+
# === VIDEO ANALYSIS ===
|
68 |
+
def process_video(video_path, known_faces, service):
|
69 |
+
face_cascade = cv2.CascadeClassifier(CASCADE_PATH)
|
70 |
video_capture = cv2.VideoCapture(video_path)
|
71 |
frame_count = 0
|
72 |
+
|
73 |
while True:
|
74 |
ret, frame = video_capture.read()
|
75 |
if not ret:
|
76 |
break
|
77 |
if frame_count % 30 == 0:
|
78 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
79 |
+
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
80 |
+
|
81 |
+
for (x, y, w, h) in faces:
|
82 |
+
face_img = gray[y:y+h, x:x+w]
|
83 |
+
if not is_match(face_img, known_faces):
|
84 |
ts = int(time.time())
|
85 |
fname = f"intruder_{ts}.jpg"
|
86 |
cv2.imwrite(fname, frame)
|
|
|
92 |
# === MAIN ===
|
93 |
def main():
|
94 |
drive = init_gdrive()
|
95 |
+
known_faces = load_known_faces(drive)
|
96 |
|
97 |
for folder_id in CCTVFEED_IDS:
|
98 |
videos = list_files(drive, folder_id)
|
99 |
for v in videos:
|
100 |
video_path = f"{v['id']}.mp4"
|
101 |
download_file(drive, v['id'], video_path)
|
102 |
+
process_video(video_path, known_faces, drive)
|
103 |
os.remove(video_path)
|
104 |
|
105 |
+
if __name__ == "__main__":
|
106 |
main()
|