Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from ultralytics import YOLO | |
| import cv2 | |
| model = YOLO("best.pt") | |
| def detect_image(image): | |
| results = model(image, conf=0.5) | |
| return results[0].plot() | |
| def detect_video(video): | |
| cap = cv2.VideoCapture(video) | |
| width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
| height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
| fps = int(cap.get(cv2.CAP_PROP_FPS)) | |
| output_path = "output.mp4" | |
| out = cv2.VideoWriter( | |
| output_path, | |
| cv2.VideoWriter_fourcc(*"mp4v"), | |
| fps, | |
| (width, height) | |
| ) | |
| while cap.isOpened(): | |
| ret, frame = cap.read() | |
| if not ret: | |
| break | |
| results = model(frame, conf=0.5) | |
| annotated = results[0].plot() | |
| out.write(annotated) | |
| cap.release() | |
| out.release() | |
| return output_path | |
| # --------------------------- | |
| # CUSTOM CSS (your HTML style) | |
| # --------------------------- | |
| css = """ | |
| body{ | |
| background:linear-gradient(120deg,#f6f9fc,#e9f2ff); | |
| font-family:Segoe UI; | |
| } | |
| .main-container{ | |
| max-width:950px; | |
| margin:auto; | |
| background:white; | |
| padding:35px; | |
| border-radius:20px; | |
| box-shadow:0px 10px 30px rgba(0,0,0,0.15); | |
| } | |
| .header{ | |
| text-align:center; | |
| font-size:34px; | |
| font-weight:bold; | |
| color:#2c3e50; | |
| } | |
| .subheader{ | |
| text-align:center; | |
| font-size:18px; | |
| margin-bottom:20px; | |
| color:#555; | |
| } | |
| .gr-button{ | |
| background:#3498db !important; | |
| color:white !important; | |
| border-radius:10px !important; | |
| font-size:18px !important; | |
| padding:12px 30px !important; | |
| } | |
| .gr-button:hover{ | |
| background:#217dbb !important; | |
| } | |
| footer{ | |
| text-align:center; | |
| margin-top:20px; | |
| color:#777; | |
| font-size:14px; | |
| } | |
| """ | |
| # --------------------------- | |
| # UI | |
| # --------------------------- | |
| with gr.Blocks(css=css) as demo: | |
| with gr.Column(elem_classes="main-container"): | |
| gr.Markdown( | |
| """ | |
| <div class='header'> | |
| Kurshi's Project 🙋🏻♀️✨ | |
| </div> | |
| <div class='header'> | |
| Object Detection in Autonomous Vehicles 🚗 | |
| </div> | |
| <div class='subheader'> | |
| YOLO Object Detection | |
| </div> | |
| """ | |
| ) | |
| with gr.Tabs(): | |
| with gr.Tab("Upload Image"): | |
| input_img = gr.Image( | |
| type="numpy", | |
| label="Upload Image" | |
| ) | |
| detect_btn = gr.Button("Start Detection") | |
| output_img = gr.Image(label="Detection Result") | |
| detect_btn.click( | |
| detect_image, | |
| inputs=input_img, | |
| outputs=output_img | |
| ) | |
| with gr.Tab("Upload Video"): | |
| input_video = gr.Video(label="Upload Video") | |
| video_btn = gr.Button("Start Detection") | |
| output_video = gr.Video(label="Detection Result") | |
| video_btn.click( | |
| detect_video, | |
| inputs=input_video, | |
| outputs=output_video | |
| ) | |
| gr.Markdown( | |
| """ | |
| <footer> | |
| YOLO | Self Driving Car Dataset | STET College | |
| </footer> | |
| """ | |
| ) | |
| demo.launch(server_name="0.0.0.0", server_port=7860, share = True) | |