Spaces:
Running
Running
import cv2 | |
import mediapipe as mp | |
import os | |
import tempfile | |
import shutil | |
import requests | |
from bs4 import BeautifulSoup | |
import gradio as gr | |
# MediaPipe setup | |
mp_face_detection = mp.solutions.face_detection | |
mp_drawing = mp.solutions.drawing_utils | |
def download_public_gdrive_file(file_id, save_dir): | |
try: | |
url = f"https://drive.google.com/uc?id={file_id}&export=download" | |
response = requests.get(url, stream=True) | |
if response.status_code == 200: | |
ext = ".avi" if "avi" in response.headers.get('Content-Disposition', '') else ".mp4" | |
output_path = os.path.join(save_dir, f"{file_id}{ext}") | |
with open(output_path, 'wb') as f: | |
f.write(response.content) | |
return output_path | |
except Exception as e: | |
print(f"Error downloading {file_id}: {e}") | |
return None | |
def get_video_ids_from_folder(folder_url): | |
response = requests.get(folder_url) | |
soup = BeautifulSoup(response.text, "html.parser") | |
video_ids = [] | |
for link in soup.find_all("a"): | |
href = link.get("href", "") | |
if "/file/d/" in href: | |
video_id = href.split("/file/d/")[1].split("/")[0] | |
video_ids.append(video_id) | |
return list(set(video_ids)) | |
def detect_faces_from_video(video_path): | |
face_count = 0 | |
temp_dir = tempfile.mkdtemp() | |
frames_with_faces = [] | |
cap = cv2.VideoCapture(video_path) | |
frame_index = 0 | |
with mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.5) as detector: | |
while cap.isOpened(): | |
ret, frame = cap.read() | |
if not ret: | |
break | |
frame_index += 1 | |
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
results = detector.process(rgb_frame) | |
if results.detections: | |
face_count += len(results.detections) | |
for detection in results.detections: | |
mp_drawing.draw_detection(frame, detection) | |
frame_path = os.path.join(temp_dir, f"frame_{frame_index}.jpg") | |
cv2.imwrite(frame_path, frame) | |
frames_with_faces.append(frame_path) | |
cap.release() | |
return frames_with_faces, face_count | |
def run_on_drive_folder(folder_url): | |
folder_url = folder_url.strip() | |
video_ids = get_video_ids_from_folder(folder_url) | |
if not video_ids: | |
return "❌ No videos found or access denied.", [] | |
frames_all = [] | |
total_faces = 0 | |
for vid_id in video_ids[:5]: # Limit to 5 videos to manage processing time | |
video_path = download_public_gdrive_file(vid_id, "/tmp") | |
if video_path: | |
frames, count = detect_faces_from_video(video_path) | |
frames_all.extend(frames) | |
total_faces += count | |
return f"✅ Detected {total_faces} face(s) across {len(frames_all)} frame(s) from {len(video_ids)} video(s).", frames_all | |
demo = gr.Interface( | |
fn=run_on_drive_folder, | |
inputs=gr.Textbox(label="Google Drive Folder URL (Public)"), | |
outputs=[gr.Textbox(label="Summary"), gr.Gallery(label="Detected Frames")], | |
title="Intruder Detection from Google Drive Folder (MP4 + AVI)", | |
description="Paste the link to a *public* Google Drive folder containing .mp4 or .avi videos." | |
) | |
if __name__ == "__main__": | |
demo.launch() | |