Spaces:
Runtime error
Runtime error
Download app.py from StarLord69/Object-Detection-Using-Yolo: direct link, hf CLI and curl.
- Browser
- Download file 1.24 kB
-
https://huggingface.co/spaces/StarLord69/Object-Detection-Using-Yolo/resolve/main/app.py
- Command line
-
hf download hf://spaces/StarLord69/Object-Detection-Using-Yolo/app.py
-
curl -L -o app.py https://huggingface.co/spaces/StarLord69/Object-Detection-Using-Yolo/resolve/main/app.py
1.24 kB
| import gradio as gr | |
| from PIL import Image | |
| import numpy as np | |
| from ultralytics import YOLO | |
| from pillow_heif import register_heif_opener | |
| # Register HEIF opener to handle .HEIC files | |
| register_heif_opener() | |
| def load_model(): | |
| return YOLO('yolov8x.pt') | |
| model = load_model() | |
| def process_image(input_image): | |
| if input_image is None: | |
| return None | |
| # Convert input to PIL Image if it's not already | |
| if not isinstance(input_image, Image.Image): | |
| input_image = Image.open(input_image) | |
| # Convert PIL Image to numpy array | |
| np_image = np.array(input_image) | |
| # Ensure the image is 3D | |
| if len(np_image.shape) == 2: | |
| np_image = np.stack((np_image,)*3, axis=-1) | |
| # Run YOLO model | |
| results = model(np_image) | |
| # Plot results on image | |
| plotted_img = results[0].plot() | |
| # Convert numpy array back to PIL Image | |
| output_image = Image.fromarray(plotted_img) | |
| return output_image | |
| iface = gr.Interface( | |
| fn=process_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs=gr.Image(type="pil"), | |
| title="Object Detection with YOLO", | |
| description="Upload an image and the model will detect objects." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch(share=False) | |