CoolFace
Apppublic

Mohamedgodz/Real_Time_Object_detection

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
1likes
app.py69 linesDownload Raw Back to root
1import gradio as gr2import cv23from ultralytics import YOLO4import tempfile5 6# Load your 3 YOLO models7models = [8    YOLO("yolo_trained_model.pt"),9    YOLO("car_person_best.pt"),10    YOLO("license best.pt")11]12 13# Function to detect objects in an image14def detect_on_image(image):15    result_frame = image.copy()16    for model in models:17        results = model(result_frame)18        result_frame = results[0].plot()19    return result_frame20 21# Function to detect objects in a video22def detect_on_video(video):23    temp_out = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")24    cap = cv2.VideoCapture(video)25 26    fps = cap.get(cv2.CAP_PROP_FPS)27    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))28    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))29 30    out = cv2.VideoWriter(temp_out.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))31 32    while cap.isOpened():33        ret, frame = cap.read()34        if not ret:35            break36        result_frame = frame.copy()37        for model in models:38            results = model(result_frame)39            result_frame = results[0].plot()40        out.write(result_frame)41 42    cap.release()43    out.release()44    return temp_out.name45 46# Gradio interfaces47image_interface = gr.Interface(48    fn=detect_on_image,49    inputs=gr.Image(type="numpy", label="Upload an Image"),50    outputs=gr.Image(type="numpy", label="Annotated Image"),51    title="YOLO Image Detection"52)53 54video_interface = gr.Interface(55    fn=detect_on_video,56    inputs=gr.Video(label="Upload a Video"),57    outputs=gr.Video(label="Processed Video"),58    title="YOLO Video Detection"59)60 61# Combine both interfaces into tabs62demo = gr.TabbedInterface(63    interface_list=[image_interface, video_interface],64    tab_names=["Image Detection", "Video Detection"]65)66 67if __name__ == "__main__":68    demo.launch()69