File size: 3,331 Bytes
37b0940
ae58e6e
 
 
 
 
 
fcf4004
37b0940
ae58e6e
 
 
37b0940
ae58e6e
 
 
 
 
 
 
 
 
 
 
 
 
37b0940
ae58e6e
 
 
 
 
 
 
 
 
 
f9318be
ae58e6e
 
 
 
f9318be
ae58e6e
 
37b0940
ae58e6e
 
 
 
 
 
 
 
5de105b
ae58e6e
 
 
 
1cfb844
ae58e6e
 
 
f9318be
ca27d94
ae58e6e
37b0940
ae58e6e
 
 
 
 
 
 
 
 
 
 
 
 
 
1cfb844
ae58e6e
1cfb844
ae58e6e
 
 
 
 
 
 
37b0940
ae58e6e
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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()