Spaces:
Sleeping
Sleeping
Download app.py from VigorInfotechLimited/TEST: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/spaces/VigorInfotechLimited/TEST/resolve/main/app.py
- Command line
-
hf download hf://spaces/VigorInfotechLimited/TEST/app.py
-
curl -L -o app.py https://huggingface.co/spaces/VigorInfotechLimited/TEST/resolve/main/app.py
1.28 kB
| import gradio as gr | |
| from ultralytics import YOLO | |
| import numpy as np | |
| import cv2 | |
| from PIL import Image | |
| # Load the pre-trained Ultralytics YOLOv8 model (can be 'yolov8n.pt' for nano model) | |
| model = YOLO('vigorvision.pt') # replace with your model path if custom | |
| def detect_objects(img: Image.Image): | |
| # Convert PIL image to OpenCV format (BGR) | |
| img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) | |
| # Run inference | |
| results = model(img_cv) | |
| # Results.render() updates results.imgs with annotated images (list of np arrays) | |
| annotated_img = results[0].plot() # get annotated image | |
| # Convert back to PIL Image (RGB) | |
| annotated_img_pil = Image.fromarray(cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)) | |
| return annotated_img_pil | |
| # Build Gradio interface | |
| iface = gr.Interface( | |
| fn=detect_objects, | |
| #inputs=gr.Image(source='upload', type='pil', tool='editor', label='Upload or Capture Image'), | |
| inputs = gr.Image(type='pil', label='Upload or Capture Image'), | |
| outputs=gr.Image(type='pil', label='Detected Output'), | |
| title='VigorVision Object Detection', | |
| description='Upload an image to detect objects using Ultralytics YOLOv8 model running directly inside this Hugging Face Space.' | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |