poudel commited on
Commit
80b308a
1 Parent(s): b5dd549

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -33
app.py CHANGED
@@ -1,41 +1,38 @@
 
1
  import gradio as gr
2
  from ultralytics import YOLO
3
-
4
- # Load YOLOv11 models from Hugging Face Hub
5
-
6
- best_model = YOLO('https://huggingface.co/poudel/yolov8-cargo-package-counter/resolve/main/1/best.pt')
7
- last_model = YOLO('https://huggingface.co/poudel/yolov8-cargo-package-counter/resolve/main/1/last.pt')
8
-
9
-
10
- # Function to detect and count packages using the selected model
11
- def count_packages(image, model_choice):
12
- # Choose the model based on user input
13
- if model_choice == "Best Model":
14
- model = best_model
15
- else:
16
- model = last_model
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
- return package_count
25
-
26
- # Gradio interface with model selection dropdown
27
- inputs = [
28
- gr.Image(type="pil", label="Upload an Image"),
29
- gr.Radio(["Best Model", "Last Model"], label="Choose Model")
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 and select a model to count the number of packages detected.",
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