File size: 1,241 Bytes
82a6b74
 
 
 
8b11c4d
 
 
 
82a6b74
 
73ccacf
82a6b74
 
 
 
8b11c4d
 
 
 
 
 
 
73ccacf
 
 
8b11c4d
 
 
 
73ccacf
 
 
82a6b74
 
73ccacf
 
8b11c4d
73ccacf
8b11c4d
82a6b74
 
 
8b11c4d
 
82a6b74
 
 
73ccacf
 
8b11c4d
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
39
40
41
42
43
44
45
46
47
48
49
50
51
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)