poudel commited on
Commit
1151f96
1 Parent(s): 89bb470

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ from PIL import Image
4
+
5
+ # Load YOLOv8 models from Hugging Face Hub
6
+ best_model = YOLO('https://huggingface.co/poudel/yolov8-cargo-package-counter/resolve/main/best.pt')
7
+ last_model = YOLO('https://huggingface.co/poudel/yolov8-cargo-package-counter/resolve/main/last.pt')
8
+
9
+ # Function to detect and count packages using the selected model
10
+ def count_packages(image, model_choice):
11
+ # Choose the model based on user input
12
+ if model_choice == "Best Model":
13
+ model = best_model
14
+ else:
15
+ model = last_model
16
+
17
+ # Run inference using the selected model
18
+ results = model(image)
19
+
20
+ # Get the number of detected boxes (packages)
21
+ package_count = len(results[0].boxes)
22
+
23
+ # Annotate the image with the detected boxes
24
+ annotated_image = results[0].plot() # Plot the detections on the image
25
+
26
+ return package_count, annotated_image
27
+
28
+ # Gradio interface with model selection dropdown
29
+ inputs = [
30
+ gr.Image(type="pil", label="Upload an Image"),
31
+ gr.Radio(["Best Model", "Last Model"], label="Choose Model")
32
+ ]
33
+
34
+ outputs = [gr.Textbox(label="Package Count"),
35
+ gr.Image(type="pil", label="Annotated Image")]
36
+
37
+ # Launch the Gradio app
38
+ gr.Interface(fn=count_packages,
39
+ inputs=inputs,
40
+ outputs=outputs,
41
+ title="Cargo Package Counting App",
42
+ description="Upload an image and select a model to count the number of packages detected.",
43
+ live=True).launch()