Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,38 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# Run inference using the selected model
|
| 19 |
-
results = model(image)
|
| 20 |
-
|
| 21 |
# Get the number of detected boxes (packages)
|
| 22 |
package_count = len(results[0].boxes)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
outputs = gr.Textbox(label="Package Count")
|
|
|
|
| 33 |
|
| 34 |
# Launch the Gradio app
|
| 35 |
-
gr.Interface(fn=count_packages,
|
| 36 |
-
inputs=inputs,
|
| 37 |
-
outputs=outputs,
|
| 38 |
title="Cargo Package Counting App",
|
| 39 |
-
description="Upload an image
|
| 40 |
-
live=True).launch()
|
| 41 |
|
|
|
|
| 1 |
+
|
| 2 |
import gradio as gr
|
| 3 |
from ultralytics import YOLO
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Load the YOLOv8 model
|
| 8 |
+
best_model = YOLO('https://huggingface.co/poudel/yolov8-cargo-package-counter/resolve/main/best.pt')
|
| 9 |
+
|
| 10 |
+
# Function to detect and count packages
|
| 11 |
+
def count_packages(image):
|
| 12 |
+
# Convert the PIL image to a NumPy array
|
| 13 |
+
image = np.array(image)
|
| 14 |
+
|
| 15 |
+
# Run inference using the loaded YOLO model
|
| 16 |
+
results = best_model(image)
|
| 17 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Get the number of detected boxes (packages)
|
| 19 |
package_count = len(results[0].boxes)
|
| 20 |
+
|
| 21 |
+
# Annotate the image with the detected boxes
|
| 22 |
+
annotated_image = results[0].plot()
|
| 23 |
+
|
| 24 |
+
# Return the package count and the annotated image
|
| 25 |
+
return package_count, annotated_image
|
| 26 |
+
|
| 27 |
+
# Gradio interface
|
| 28 |
+
inputs = gr.Image(type="pil", label="Upload an Image")
|
| 29 |
+
outputs = [gr.Textbox(label="Package Count"),
|
| 30 |
+
gr.Image(type="numpy", label="Annotated Image")]
|
| 31 |
|
| 32 |
# Launch the Gradio app
|
| 33 |
+
gr.Interface(fn=count_packages,
|
| 34 |
+
inputs=inputs,
|
| 35 |
+
outputs=outputs,
|
| 36 |
title="Cargo Package Counting App",
|
| 37 |
+
description="Upload an image to count the number of packages detected.").launch()
|
|
|
|
| 38 |
|