luxmorocco commited on
Commit
74a4d84
·
verified ·
1 Parent(s): 23c21cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -18
app.py CHANGED
@@ -3,6 +3,8 @@ import gradio as gr
3
  from ultralytics import ASSETS, YOLO
4
 
5
  model = YOLO("best.pt")
 
 
6
  def predict_image(img, conf_threshold, iou_threshold):
7
  results = model.predict(
8
  source=img,
@@ -13,25 +15,13 @@ def predict_image(img, conf_threshold, iou_threshold):
13
  imgsz=640,
14
  )
15
 
16
- # Access the predictions and class names
17
- preds = results.pred[0]
18
- class_ids = preds[:, -1].int().tolist() # Get class IDs for all detections
19
- class_names = [results.names[i] for i in class_ids]
20
 
21
- # Count the number of fruits detected
22
- fruit_count = sum(1 for name in class_names if 'fruit' in name.lower()) # Adjust 'fruit' to the actual class name if needed
23
 
24
- # Convert the result to an image and draw the count on it
25
- im_array = results.render()[0]
26
- im = Image.fromarray(im_array)
27
- draw = ImageDraw.Draw(im)
28
- text = f"Fruits detected: {fruit_count}"
29
- text_size = draw.textsize(text)
30
- draw.rectangle([(0, im.height - text_size[1]), (text_size[0], im.height)], fill="white")
31
- draw.text((0, im.height - text_size[1]), text, fill="black")
32
 
33
- return im
34
-
35
  iface = gr.Interface(
36
  fn=predict_image,
37
  inputs=[
@@ -39,10 +29,11 @@ iface = gr.Interface(
39
  gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
40
  gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold")
41
  ],
42
- outputs=[gr.Image(type="pil", label="Result"), gr.Textbox(label="Caption")],
43
  title="My Yield | 🌱",
44
  description="Estimate the amount of plants per year",
 
45
  )
46
 
47
  if __name__ == '__main__':
48
- iface.launch()
 
3
  from ultralytics import ASSETS, YOLO
4
 
5
  model = YOLO("best.pt")
6
+
7
+
8
  def predict_image(img, conf_threshold, iou_threshold):
9
  results = model.predict(
10
  source=img,
 
15
  imgsz=640,
16
  )
17
 
18
+ for r in results:
19
+ im_array = r.plot()
20
+ im = Image.fromarray(im_array[..., ::-1])
 
21
 
22
+ return im
 
23
 
 
 
 
 
 
 
 
 
24
 
 
 
25
  iface = gr.Interface(
26
  fn=predict_image,
27
  inputs=[
 
29
  gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
30
  gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold")
31
  ],
32
+ outputs=gr.Image(type="pil", label="Result"),
33
  title="My Yield | 🌱",
34
  description="Estimate the amount of plants per year",
35
+
36
  )
37
 
38
  if __name__ == '__main__':
39
+ iface.launch()