Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,6 @@ import gradio as gr
|
|
3 |
from ultralytics import ASSETS, YOLO
|
4 |
|
5 |
model = YOLO("best.pt")
|
6 |
-
|
7 |
def predict_image(img, conf_threshold, iou_threshold):
|
8 |
results = model.predict(
|
9 |
source=img,
|
@@ -14,18 +13,25 @@ def predict_image(img, conf_threshold, iou_threshold):
|
|
14 |
imgsz=640,
|
15 |
)
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
|
|
20 |
|
21 |
-
#
|
22 |
-
for
|
23 |
-
im_array = r.plot()
|
24 |
-
im = Image.fromarray(im_array[..., ::-1])
|
25 |
-
|
26 |
-
caption = f"Detected fruits: {fruits_count}" # Modify this line according to the actual object you're detecting
|
27 |
-
return im, caption
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
iface = gr.Interface(
|
30 |
fn=predict_image,
|
31 |
inputs=[
|
|
|
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 |
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=[
|