File size: 1,606 Bytes
5de7170
6cae9b2
5de7170
 
e218086
6cae9b2
5de7170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6cae9b2
e218086
5de7170
 
 
 
 
 
 
 
e218086
6cae9b2
e218086
 
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
import os
import gradio as gr
import cv2
import shutil
from services.under_construction.combined_detection import run_combined_detection

TEMP_DIR = "temp_frames"
VIDEO_PATH = "uploaded_video.mp4"

def extract_frames(video_path, output_dir, interval=30):
    if os.path.exists(output_dir):
        shutil.rmtree(output_dir)
    os.makedirs(output_dir, exist_ok=True)

    cap = cv2.VideoCapture(video_path)
    frame_count = 0
    saved = 0
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        if frame_count % interval == 0:
            frame_path = os.path.join(output_dir, f"frame_{saved:05d}.jpg")
            cv2.imwrite(frame_path, frame)
            saved += 1
        frame_count += 1
    cap.release()
    return output_dir

def process_video(video_file):
    # Save uploaded video
    with open(VIDEO_PATH, "wb") as f:
        f.write(video_file.read())

    # Extract frames
    frame_dir = extract_frames(VIDEO_PATH, TEMP_DIR, interval=30)

    # Run combined detection
    results, annotated = run_combined_detection(frame_dir)

    return results, annotated

demo = gr.Interface(
    fn=process_video,
    inputs=gr.File(label="Upload Drone Video (.mp4)", file_types=[".mp4"]),
    outputs=[
        gr.Textbox(label="Detection Results"),
        gr.Gallery(label="Detected Frames").style(grid=3)
    ],
    title="NHAI Combined Detection: Earthwork + Culvert + Bridge Pier",
    description="Upload a drone video. This app detects earthwork, culverts, and bridge piers using YOLOv8 models.",
)

if __name__ == "__main__":
    demo.launch()