File size: 1,569 Bytes
282aec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from yolo_agent import video_detection_tool
import os
import time

def detect_objects(video):
    """Handles video upload and runs YOLO detection, displaying detections in real-time."""
    result = video_detection_tool.invoke(video, conf=0.8)  # Explicitly setting confidence threshold
    detected_images = "detections"  # Folder where detected images are stored

    
    image_paths = []
    if os.path.exists(detected_images):
        for _ in range(20):  # Limit the loop to avoid infinite execution
            new_images = sorted(
                [os.path.join(detected_images, img) for img in os.listdir(detected_images) if img.endswith(".jpg")],
                key=os.path.getmtime  # Sort images by modification time for real-time order
            )
            if new_images != image_paths:
                image_paths = new_images
                yield result, image_paths
            time.sleep(1)  # Update images in real-time
    
    return result, []

# Gradio Interface
demo = gr.Blocks()
with demo:
    gr.Markdown("# πŸŽ₯ YOLO Object Detection with LangChain - Real-time Display")
    video_input = gr.File(label="πŸ“€ Upload a Video", type="filepath")
    output_text = gr.Textbox(label="πŸ“„ Detection Results")
    output_gallery = gr.Gallery(label="πŸ“Έ Detected Objects", show_label=True, interactive=False, columns=4)
    detect_button = gr.Button("πŸš€ Run Detection")
    detect_button.click(fn=detect_objects, inputs=video_input, outputs=[output_text, output_gallery])  # Removed `live=True`

demo.launch(share=True)