Spaces:
Sleeping
Sleeping
muhammadhamza-stack
commited on
Commit
·
c566fa5
1
Parent(s):
bc42890
refine the gradio app
Browse files- .gitattributes +2 -0
- .gitignore +1 -0
- app.py +103 -16
- requirements.txt +4 -3
- sample_data/Test21.png +3 -0
- sample_data/coin.jpeg +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
venv
|
app.py
CHANGED
|
@@ -1,33 +1,120 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Load the YOLO model
|
|
|
|
| 5 |
model = YOLO('best1.pt')
|
| 6 |
|
| 7 |
def predict(img, confidence_threshold):
|
| 8 |
# Perform inference
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
# Filter predictions based on the confidence threshold
|
| 12 |
# The results[0].boxes.data contains the detection results, including confidence scores
|
| 13 |
-
filtered_boxes = [box for box in results[0].boxes.data if box[4] >= confidence_threshold]
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return annotated_frame
|
| 19 |
|
| 20 |
-
# Create the Gradio interface
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Launch the Gradio interface
|
| 33 |
-
iface.
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
+
import os
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# --- DOCUMENTATION STRINGS (Coin Detector App) ---
|
| 7 |
+
|
| 8 |
+
GUIDELINE_SETUP = """
|
| 9 |
+
## 1. Quick Start Guide: Detection and Filtering
|
| 10 |
+
|
| 11 |
+
This application uses a trained YOLO model to automatically detect coins in an image and allows you to filter the results based on detection confidence.
|
| 12 |
+
|
| 13 |
+
1. **Upload Image:** Upload the image you want to analyze in the 'Input Image' box.
|
| 14 |
+
2. **Adjust Threshold:** Use the 'Confidence Threshold' slider to set the minimum certainty required for a coin to be displayed.
|
| 15 |
+
3. **Review:** The output image will show bounding boxes around all detections that meet or exceed the set threshold.
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
GUIDELINE_INPUT = """
|
| 19 |
+
## 2. Expected Inputs and Parameters
|
| 20 |
+
|
| 21 |
+
| Input Field | Purpose | Requirement |
|
| 22 |
+
| :--- | :--- | :--- |
|
| 23 |
+
| **Input Image** | The photograph containing the coins you wish to detect. | Must be an image file (e.g., JPG, PNG). |
|
| 24 |
+
| **Confidence Threshold** | Filters the model's predictions. Only detections with a confidence score equal to or higher than this value will be shown. | Slider range: 0.0 (least strict) to 1.0 (most strict). Default is 0.5. |
|
| 25 |
+
|
| 26 |
+
**Tip:** If you see too many false positives (non-coins being detected), raise the threshold. If the model misses coins you know are there, try lowering the threshold.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
GUIDELINE_OUTPUT = """
|
| 30 |
+
## 3. Expected Outputs (Annotated Image)
|
| 31 |
+
|
| 32 |
+
The output is a single image component displaying the **Annotated Frame**.
|
| 33 |
+
|
| 34 |
+
* **Content:** This image is the original input image with colored bounding boxes drawn around every coin detected by the model that passed the `Confidence Threshold` filter.
|
| 35 |
+
* **Bounding Boxes:** Each box confirms a coin detection and is usually accompanied by a label (e.g., 'coin') and the confidence score (e.g., 0.95).
|
| 36 |
+
"""
|
| 37 |
|
| 38 |
# Load the YOLO model
|
| 39 |
+
# NOTE: The model file 'best1.pt' must exist in the same directory or accessible path.
|
| 40 |
model = YOLO('best1.pt')
|
| 41 |
|
| 42 |
def predict(img, confidence_threshold):
|
| 43 |
# Perform inference
|
| 44 |
+
# Note: Using verbose=False to keep the interface clean during prediction
|
| 45 |
+
results = model(img, verbose=False)
|
| 46 |
|
| 47 |
# Filter predictions based on the confidence threshold
|
| 48 |
# The results[0].boxes.data contains the detection results, including confidence scores
|
|
|
|
| 49 |
|
| 50 |
+
# We filter the bounding boxes data array based on the confidence score (index 4)
|
| 51 |
+
# Then we must convert the filtered list back to a tensor format expected by the plotting function.
|
| 52 |
|
| 53 |
+
filtered_data = [box.cpu() for box in results[0].boxes.data if box[4] >= confidence_threshold]
|
| 54 |
+
|
| 55 |
+
if filtered_data:
|
| 56 |
+
filtered_tensor = torch.stack(filtered_data)
|
| 57 |
+
|
| 58 |
+
# Create a deep copy of the original results object to manipulate its boxes data
|
| 59 |
+
filtered_results = results[0].cpu()
|
| 60 |
+
filtered_results.boxes.data = filtered_tensor
|
| 61 |
+
|
| 62 |
+
# Plot the results using the filtered results object
|
| 63 |
+
annotated_frame = filtered_results.plot()
|
| 64 |
+
else:
|
| 65 |
+
# If no coins pass the filter, plot the original image without boxes
|
| 66 |
+
annotated_frame = results[0].plot()
|
| 67 |
+
|
| 68 |
return annotated_frame
|
| 69 |
|
| 70 |
+
# Create the Gradio interface using gr.Blocks to allow for documentation placement
|
| 71 |
+
with gr.Blocks(title="Coin Detector") as iface:
|
| 72 |
+
|
| 73 |
+
gr.Markdown("# Coin Detector")
|
| 74 |
+
gr.Markdown("Upload an image to detect coins. Adjust the confidence threshold to filter results.")
|
| 75 |
+
|
| 76 |
+
# 1. Guidelines Section
|
| 77 |
+
with gr.Accordion("User Guidelines and Documentation", open=False):
|
| 78 |
+
gr.Markdown(GUIDELINE_SETUP)
|
| 79 |
+
gr.Markdown("---")
|
| 80 |
+
gr.Markdown(GUIDELINE_INPUT)
|
| 81 |
+
gr.Markdown("---")
|
| 82 |
+
gr.Markdown(GUIDELINE_OUTPUT)
|
| 83 |
+
|
| 84 |
+
gr.Markdown("---")
|
| 85 |
+
|
| 86 |
+
# 2. Input/Output Layout
|
| 87 |
+
with gr.Row():
|
| 88 |
+
with gr.Column(scale=2):
|
| 89 |
+
gr.Markdown("## Step 1: Upload an Image ")
|
| 90 |
+
input_img = gr.Image(label="Input Image", type="filepath")
|
| 91 |
+
gr.Markdown("## Step 2: Adjest Confidence Threshold (Optional) ")
|
| 92 |
+
confidence_slider = gr.Slider(minimum=0, maximum=1, value=0.5, label="Confidence Threshold", step=0.01)
|
| 93 |
+
gr.Markdown("## Step 3: Click Detect Coins ")
|
| 94 |
+
submit_btn = gr.Button("Detect Coins", variant="primary")
|
| 95 |
+
|
| 96 |
+
with gr.Column(scale=1):
|
| 97 |
+
gr.Markdown("## Result ")
|
| 98 |
+
output_img = gr.Image(label="Output Image")
|
| 99 |
+
|
| 100 |
+
# 3. Example Data (if available, added here for completeness)
|
| 101 |
+
# Note: Since no examples were provided, this is commented out or left as placeholders.
|
| 102 |
+
gr.Markdown("## Examples ")
|
| 103 |
+
gr.Examples(
|
| 104 |
+
examples=[["./sample_data/coin.jpeg", 0.5], ["./sample_data/Test21.png", 0.4]],
|
| 105 |
+
inputs=[input_img, confidence_slider],
|
| 106 |
+
outputs=output_img,
|
| 107 |
+
fn=predict,
|
| 108 |
+
cache_examples=False
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# 4. Event Handler
|
| 112 |
+
submit_btn.click(
|
| 113 |
+
fn=predict,
|
| 114 |
+
inputs=[input_img, confidence_slider],
|
| 115 |
+
outputs=output_img
|
| 116 |
+
)
|
| 117 |
|
| 118 |
# Launch the Gradio interface
|
| 119 |
+
iface.queue()
|
| 120 |
+
iface.launch(share=True)
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
ultralytics
|
| 2 |
-
|
| 3 |
-
numpy
|
| 4 |
-
|
|
|
|
|
|
| 1 |
ultralytics
|
| 2 |
+
pillow
|
| 3 |
+
numpy<2
|
| 4 |
+
gradio==3.50.2
|
| 5 |
+
gradio-client==0.6.1
|
sample_data/Test21.png
ADDED
|
Git LFS Details
|
sample_data/coin.jpeg
ADDED
|
Git LFS Details
|