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()