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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -1,25 +1,26 @@
1
  import gradio as gr
2
  from PIL import Image
3
- import cv2
4
  import numpy as np
5
  from ultralytics import YOLO
6
- import io
7
 
8
  def load_model():
9
- model = YOLO('yolov8x.pt')
10
- return model
11
 
12
  model = load_model()
13
 
14
  def process_image(input_image):
15
- open_cv_image = np.array(input_image)
16
- open_cv_image = open_cv_image[:, :, ::-1] # Convert RGB to BGR
17
- results = model(open_cv_image)
 
 
 
18
  # Plot results on image
19
  plotted_img = results[0].plot()
20
- # Convert BGR to RGB
21
- rgb_image = cv2.cvtColor(plotted_img, cv2.COLOR_BGR2RGB)
22
- pil_image = Image.fromarray(rgb_image)
 
23
  return pil_image
24
 
25
  iface = gr.Interface(
@@ -29,4 +30,6 @@ iface = gr.Interface(
29
  title="Object Detection with YOLO",
30
  description="Upload an image and the model will detect objects."
31
  )
32
- iface.launch(share=False)
 
 
 
1
  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')
 
8
 
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
+
18
  # Plot results on 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(
 
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)