Spaces:
Sleeping
Sleeping
Download app.py from taher-dev/helmet-detection: direct link, hf CLI and curl.
- Browser
- Download file 2.91 kB
-
https://huggingface.co/spaces/taher-dev/helmet-detection/resolve/main/app.py
- Command line
-
hf download hf://spaces/taher-dev/helmet-detection/app.py
-
curl -L -o app.py https://huggingface.co/spaces/taher-dev/helmet-detection/resolve/main/app.py
2.91 kB
| # app.py (fixed) | |
| import os | |
| from huggingface_hub import hf_hub_download | |
| from ultralytics import YOLO | |
| import gradio as gr | |
| import cv2 | |
| import numpy as np | |
| # Ensure Ultralytics writes config to a writable temp dir inside Spaces | |
| os.environ.setdefault("YOLO_CONFIG_DIR", "/tmp/Ultralytics") | |
| # ---------------- CONFIG ---------------- | |
| MODEL_REPO = "taher-dev/helmet-detection" # your model repo (where best.pt is uploaded) | |
| MODEL_FILENAME = "best.pt" # filename inside the model repo | |
| LOCAL_MODEL = MODEL_FILENAME # local filename to use | |
| # ---------------------------------------- | |
| def download_model_if_missing(): | |
| if os.path.exists(LOCAL_MODEL): | |
| print(f"Model already exists at {LOCAL_MODEL}") | |
| return LOCAL_MODEL | |
| print(f"Downloading {MODEL_FILENAME} from huggingface.co/{MODEL_REPO} ...") | |
| hf_token = os.environ.get("HF_TOKEN", None) # Space secret if repo private | |
| try: | |
| local_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME, repo_type="model", token=hf_token) | |
| except Exception as e: | |
| print("hf_hub_download failed:", e) | |
| raise | |
| print("Downloaded model to:", local_path) | |
| return local_path | |
| # Download / load | |
| MODEL_PATH = download_model_if_missing() | |
| print("Loading YOLO model from:", MODEL_PATH) | |
| model = YOLO(MODEL_PATH) | |
| print("Model loaded. Model names:", model.names) | |
| # Inference function used by Gradio | |
| def detect(image, conf=0.25): | |
| """ | |
| image: numpy array (H,W,3) in RGB | |
| conf: confidence threshold (float) | |
| returns: annotated image (RGB numpy array) | |
| """ | |
| if image is None: | |
| return None | |
| results = model.predict(source=image, conf=conf, verbose=False) | |
| r = results[0] | |
| img = image.copy() | |
| if hasattr(r, "boxes") and r.boxes is not None: | |
| boxes = r.boxes.xyxy.cpu().numpy() | |
| scores = r.boxes.conf.cpu().numpy() | |
| classes = r.boxes.cls.cpu().numpy().astype(int) | |
| names = model.names | |
| for (x1, y1, x2, y2), s, c in zip(boxes, scores, classes): | |
| x1, y1, x2, y2 = map(int, [x1, y1, x2, y2]) | |
| cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2) | |
| label = f"{names.get(c, str(c))}: {s:.2f}" | |
| cv2.putText(img, label, (x1, y1 - 8), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) | |
| return img | |
| title = "Helmet Detection (YOLO + Gradio)" | |
| description = "Upload an image to detect helmets. Model: taher-dev/helmet-detection/best.pt" | |
| # NOTE: removed 'allow_flagging' argument to be compatible with the Spaces Gradio version | |
| demo = gr.Interface( | |
| fn=detect, | |
| inputs=[gr.Image(type="numpy", label="Upload Image"), gr.Slider(minimum=0.05, maximum=0.9, step=0.01, value=0.25, label="Confidence")], | |
| outputs=gr.Image(type="numpy", label="Detections"), | |
| title=title, | |
| description=description | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860) | |