Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,92 +1,80 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
-
import requests
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
import tempfile
|
| 6 |
-
import time
|
| 7 |
import mediapipe as mp
|
|
|
|
| 8 |
|
| 9 |
# === CONFIGURATION ===
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
# ===
|
| 14 |
mp_face_detection = mp.solutions.face_detection
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
for
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
frame_display = st.empty()
|
| 48 |
-
stats_display = st.empty()
|
| 49 |
|
| 50 |
while cap.isOpened():
|
| 51 |
ret, frame = cap.read()
|
| 52 |
if not ret:
|
| 53 |
break
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
# Convert BGR to RGB
|
| 57 |
-
image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 58 |
-
results = face_detection.process(image_rgb)
|
| 59 |
-
|
| 60 |
-
# Draw faces
|
| 61 |
if results.detections:
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
x, y, w, h = int(bboxC.xmin * iw), int(bboxC.ymin * ih), int(bboxC.width * iw), int(bboxC.height * ih)
|
| 66 |
-
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 67 |
-
face_count += 1
|
| 68 |
-
|
| 69 |
-
# Resize for display
|
| 70 |
-
display_frame = cv2.resize(frame, (640, 360))
|
| 71 |
-
frame_display.image(display_frame, channels="BGR")
|
| 72 |
-
|
| 73 |
-
stats_display.markdown(f"**Frames Processed:** {frame_count} | **Faces Detected So Far:** {face_count}")
|
| 74 |
-
time.sleep(1 / 24.0) # Approx. 24 FPS
|
| 75 |
-
|
| 76 |
cap.release()
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
# ===
|
| 80 |
-
|
| 81 |
-
st.
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
import requests
|
| 5 |
import tempfile
|
|
|
|
| 6 |
import mediapipe as mp
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
# === CONFIGURATION ===
|
| 10 |
+
API_KEY = "AIzaSy...your_public_API_key..." # Replace with a valid public API key if needed
|
| 11 |
+
CCTVFEED_IDS = ['1KJRkSf2SKEZ1mXS9_si65IwMBtjs6p4n']
|
| 12 |
+
REG_FOLDER_ID = '1qkcR7nQTEtiMH9OFUv2bGxVn08E3dKjF'
|
| 13 |
+
INTRUDER_FOLDER_ID = '1PPAUWU-wMx7fek73p-hqPqYQypYtG8Ob'
|
| 14 |
|
| 15 |
+
# === SETUP ===
|
| 16 |
mp_face_detection = mp.solutions.face_detection
|
| 17 |
+
mp_drawing = mp.solutions.drawing_utils
|
| 18 |
+
face_detector = mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.5)
|
| 19 |
+
|
| 20 |
+
# === FUNCTIONS ===
|
| 21 |
+
def get_drive_video_links(folder_id):
|
| 22 |
+
api_url = f"https://www.googleapis.com/drive/v3/files?q='{folder_id}'+in+parents&key={API_KEY}&fields=files(id,name,mimeType)"
|
| 23 |
+
response = requests.get(api_url)
|
| 24 |
+
files = response.json().get("files", [])
|
| 25 |
+
|
| 26 |
+
st.write(f"🔍 Found {len(files)} files in folder `{folder_id}`")
|
| 27 |
+
for f in files:
|
| 28 |
+
st.write(f" - **{f['name']}** | `{f['mimeType']}`")
|
| 29 |
+
|
| 30 |
+
video_links = []
|
| 31 |
+
for f in files:
|
| 32 |
+
if f["mimeType"] in ["video/mp4", "video/x-msvideo", "video/avi"]:
|
| 33 |
+
link = f"https://drive.google.com/uc?id={f['id']}&export=download"
|
| 34 |
+
video_links.append((f["name"], link))
|
| 35 |
+
return video_links
|
| 36 |
+
|
| 37 |
+
def download_video(link):
|
| 38 |
+
response = requests.get(link, stream=True)
|
| 39 |
+
temp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 40 |
+
for chunk in response.iter_content(chunk_size=1024*1024):
|
| 41 |
+
temp.write(chunk)
|
| 42 |
+
temp.close()
|
| 43 |
+
return temp.name
|
| 44 |
+
|
| 45 |
+
def detect_faces_from_video(video_path):
|
| 46 |
+
cap = cv2.VideoCapture(video_path)
|
| 47 |
+
frames_with_faces = 0
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
while cap.isOpened():
|
| 50 |
ret, frame = cap.read()
|
| 51 |
if not ret:
|
| 52 |
break
|
| 53 |
+
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 54 |
+
results = face_detector.process(rgb)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
if results.detections:
|
| 56 |
+
frames_with_faces += 1
|
| 57 |
+
for det in results.detections:
|
| 58 |
+
mp_drawing.draw_detection(frame, det)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
cap.release()
|
| 60 |
+
return frames_with_faces
|
| 61 |
+
|
| 62 |
+
# === STREAMLIT UI ===
|
| 63 |
+
st.title("🔐 Intruder Detection from CCTV Feed")
|
| 64 |
+
st.write("Streaming videos from Google Drive, detecting faces using MediaPipe.")
|
| 65 |
+
|
| 66 |
+
with st.spinner("Fetching videos..."):
|
| 67 |
+
total_detections = 0
|
| 68 |
+
for folder_id in CCTVFEED_IDS:
|
| 69 |
+
videos = get_drive_video_links(folder_id)
|
| 70 |
+
for vname, link in videos:
|
| 71 |
+
st.subheader(f"🎥 {vname}")
|
| 72 |
+
video_file = download_video(link)
|
| 73 |
+
faces = detect_faces_from_video(video_file)
|
| 74 |
+
os.remove(video_file)
|
| 75 |
+
if faces > 0:
|
| 76 |
+
st.success(f"✅ Detected faces in {faces} frames.")
|
| 77 |
+
total_detections += 1
|
| 78 |
+
else:
|
| 79 |
+
st.warning("⚠️ No faces detected.")
|
| 80 |
+
st.info(f"🔎 Completed. Videos with detected faces: {total_detections}")
|