otmanheddouch commited on
Commit
89fca02
·
verified ·
1 Parent(s): 4edcf13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -2,25 +2,34 @@ import gradio as gr
2
  from ultralytics import YOLO
3
  import cv2
4
  import numpy as np
5
- import os
 
6
  from PIL import Image
7
 
8
- def perform(image):
9
- """Perform the task of detection"""
10
  # Predict with the model
11
  results = model(image, save=True) # predict on an image
 
12
  # get the saved prediction
13
  prediction_image = Image.open(os.path.join(results[0].save_dir, os.listdir(results[0].save_dir)[0]))
 
 
 
 
 
 
14
  # initialize the output list to save the bbox
15
  outputs = []
 
16
  # Access the results
17
  for result in results[0]:
18
  xywh = result.boxes.xyxy[0].numpy().tolist() # top-left-x, top-left-y, bottom-right-x, bottom-right-y
19
- name = result.names[result.boxes.cls.item()] # classes
20
  confs = float(result.boxes.conf) # confidence score of each box
21
- outputs.append({"xywh":xywh ,"conf":confs, "name":name})
22
- # return the image and the bbox
23
- return prediction_image, outputs
 
24
 
25
  def load_model(model_name: str = "yolo11n.pt"):
26
  """Model loading"""
@@ -29,7 +38,6 @@ def load_model(model_name: str = "yolo11n.pt"):
29
  model = YOLO(model_name)
30
  except Exception as e:
31
  print(f"Error loading the model {e}")
32
- return None
33
  return model
34
 
35
  # loading the model
@@ -39,7 +47,7 @@ model = load_model()
39
  demo = gr.Interface(
40
  fn=perform,
41
  inputs=[gr.Image(type="pil")], # PIL Image format
42
- outputs=[gr.Image(type="pil"), gr.JSON()],
43
  title="Object Detection using YOLO",
44
  description="Upload an image and get image with bbox in it",
45
  examples=None # You can add example images here if needed
@@ -47,5 +55,4 @@ demo = gr.Interface(
47
 
48
  # Launch the app
49
  if __name__ == "__main__":
50
-
51
- demo.launch()
 
2
  from ultralytics import YOLO
3
  import cv2
4
  import numpy as np
5
+ import os
6
+ import tempfile
7
  from PIL import Image
8
 
9
+ def perform(image, task: str="detect"):
 
10
  # Predict with the model
11
  results = model(image, save=True) # predict on an image
12
+
13
  # get the saved prediction
14
  prediction_image = Image.open(os.path.join(results[0].save_dir, os.listdir(results[0].save_dir)[0]))
15
+
16
+ # Save the prediction image to a temporary file for public URL
17
+ temp_dir = tempfile.mkdtemp()
18
+ temp_path = os.path.join(temp_dir, "prediction.jpg")
19
+ prediction_image.save(temp_path)
20
+
21
  # initialize the output list to save the bbox
22
  outputs = []
23
+
24
  # Access the results
25
  for result in results[0]:
26
  xywh = result.boxes.xyxy[0].numpy().tolist() # top-left-x, top-left-y, bottom-right-x, bottom-right-y
27
+ name = result.names[result.boxes.cls.item()] # classes
28
  confs = float(result.boxes.conf) # confidence score of each box
29
+ outputs.append({"xywh": xywh, "conf": confs, "name": name})
30
+
31
+ # return the image path (for public URL) and the bbox
32
+ return temp_path, outputs
33
 
34
  def load_model(model_name: str = "yolo11n.pt"):
35
  """Model loading"""
 
38
  model = YOLO(model_name)
39
  except Exception as e:
40
  print(f"Error loading the model {e}")
 
41
  return model
42
 
43
  # loading the model
 
47
  demo = gr.Interface(
48
  fn=perform,
49
  inputs=[gr.Image(type="pil")], # PIL Image format
50
+ outputs=[gr.Image(type="filepath"), gr.JSON()], # Changed to filepath for public URL
51
  title="Object Detection using YOLO",
52
  description="Upload an image and get image with bbox in it",
53
  examples=None # You can add example images here if needed
 
55
 
56
  # Launch the app
57
  if __name__ == "__main__":
58
+ demo.launch() # Added share=True for public access