File size: 1,281 Bytes
8597f7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0feee46
b5a502a
0feee46
8597f7b
a980497
8597f7b
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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()