Download app.py from subhodeepmoitra/Traffic-Light-Detection: direct link, hf CLI and curl.
- Browser
- Download file 1.5 kB
-
https://huggingface.co/spaces/subhodeepmoitra/Traffic-Light-Detection/resolve/main/app.py
- Command line
-
hf download hf://spaces/subhodeepmoitra/Traffic-Light-Detection/app.py
-
curl -L -o app.py https://huggingface.co/spaces/subhodeepmoitra/Traffic-Light-Detection/resolve/main/app.py
1.5 kB
| import gradio as gr | |
| from ultralytics import YOLO | |
| import numpy as np | |
| import cv2 | |
| import spaces | |
| model = None | |
| def load_model(): | |
| global model | |
| if model is None: | |
| model = YOLO( | |
| "https://huggingface.co/subhodeepmoitra/Traffic_signals_detection_YOLOv8m/resolve/main/best.onnx", | |
| task="detect" | |
| ) | |
| return model | |
| def detect(image): | |
| if image is None: | |
| return None | |
| model = load_model() | |
| image = np.array(image) | |
| results = model( | |
| image, | |
| verbose=False | |
| ) | |
| output = results[0].plot() | |
| output = cv2.cvtColor( | |
| output, | |
| cv2.COLOR_BGR2RGB | |
| ) | |
| return output | |
| with gr.Blocks() as demo: | |
| gr.Markdown( | |
| """ | |
| # YOLOv8m Traffic Detection | |
| Detect traffic signs, vehicles and pedestrians. | |
| """ | |
| ) | |
| with gr.Tab("Upload"): | |
| inp = gr.Image( | |
| type="numpy" | |
| ) | |
| out = gr.Image( | |
| type="numpy" | |
| ) | |
| btn = gr.Button("Detect") | |
| btn.click( | |
| detect, | |
| inp, | |
| out | |
| ) | |
| with gr.Tab("Webcam"): | |
| cam = gr.Image( | |
| sources=["webcam"], | |
| streaming=True, | |
| type="numpy" | |
| ) | |
| result = gr.Image( | |
| type="numpy" | |
| ) | |
| cam.stream( | |
| detect, | |
| cam, | |
| result | |
| ) | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860 | |
| ) |