StarLord69 commited on
Commit
8b11c4d
·
verified ·
1 Parent(s): 73ccacf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -5
app.py CHANGED
@@ -2,6 +2,10 @@ import gradio as gr
2
  from PIL import Image
3
  import numpy as np
4
  from ultralytics import YOLO
 
 
 
 
5
 
6
  def load_model():
7
  return YOLO('yolov8x.pt')
@@ -9,9 +13,20 @@ def load_model():
9
  model = load_model()
10
 
11
  def process_image(input_image):
 
 
 
 
 
 
 
12
  # Convert PIL Image to numpy array
13
  np_image = np.array(input_image)
14
 
 
 
 
 
15
  # Run YOLO model
16
  results = model(np_image)
17
 
@@ -19,17 +34,17 @@ def process_image(input_image):
19
  plotted_img = results[0].plot()
20
 
21
  # Convert numpy array back to PIL Image
22
- pil_image = Image.fromarray(plotted_img)
23
 
24
- return pil_image
25
 
26
  iface = gr.Interface(
27
  fn=process_image,
28
- inputs='image',
29
- outputs='image',
30
  title="Object Detection with YOLO",
31
  description="Upload an image and the model will detect objects."
32
  )
33
 
34
  if __name__ == "__main__":
35
- iface.launch(share=False)
 
2
  from PIL import Image
3
  import numpy as np
4
  from ultralytics import YOLO
5
+ from pillow_heif import register_heif_opener
6
+
7
+ # Register HEIF opener to handle .HEIC files
8
+ register_heif_opener()
9
 
10
  def load_model():
11
  return YOLO('yolov8x.pt')
 
13
  model = load_model()
14
 
15
  def process_image(input_image):
16
+ if input_image is None:
17
+ return None
18
+
19
+ # Convert input to PIL Image if it's not already
20
+ if not isinstance(input_image, Image.Image):
21
+ input_image = Image.open(input_image)
22
+
23
  # Convert PIL Image to numpy array
24
  np_image = np.array(input_image)
25
 
26
+ # Ensure the image is 3D
27
+ if len(np_image.shape) == 2:
28
+ np_image = np.stack((np_image,)*3, axis=-1)
29
+
30
  # Run YOLO model
31
  results = model(np_image)
32
 
 
34
  plotted_img = results[0].plot()
35
 
36
  # Convert numpy array back to PIL Image
37
+ output_image = Image.fromarray(plotted_img)
38
 
39
+ return output_image
40
 
41
  iface = gr.Interface(
42
  fn=process_image,
43
+ inputs=gr.Image(type="pil"),
44
+ outputs=gr.Image(type="pil"),
45
  title="Object Detection with YOLO",
46
  description="Upload an image and the model will detect objects."
47
  )
48
 
49
  if __name__ == "__main__":
50
+ iface.launch(share=False)