Spaces:
Running
Running
muhammadhamza-stack
commited on
Commit
·
c70e012
1
Parent(s):
c566fa5
update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
|
|
|
| 3 |
import os
|
| 4 |
-
import torch
|
| 5 |
|
| 6 |
# --- DOCUMENTATION STRINGS (Coin Detector App) ---
|
| 7 |
|
|
@@ -12,7 +135,8 @@ This application uses a trained YOLO model to automatically detect coins in an i
|
|
| 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. **
|
|
|
|
| 16 |
"""
|
| 17 |
|
| 18 |
GUIDELINE_INPUT = """
|
|
@@ -36,26 +160,22 @@ The output is a single image component displaying the **Annotated Frame**.
|
|
| 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 |
-
#
|
| 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 |
-
#
|
| 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
|
| 59 |
filtered_results = results[0].cpu()
|
| 60 |
filtered_results.boxes.data = filtered_tensor
|
| 61 |
|
|
@@ -85,17 +205,14 @@ with gr.Blocks(title="Coin Detector") as iface:
|
|
| 85 |
|
| 86 |
# 2. Input/Output Layout
|
| 87 |
with gr.Row():
|
| 88 |
-
with gr.Column(scale=
|
| 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=
|
| 97 |
-
gr.
|
| 98 |
-
|
| 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.
|
|
@@ -108,7 +225,7 @@ with gr.Blocks(title="Coin Detector") as iface:
|
|
| 108 |
cache_examples=False
|
| 109 |
)
|
| 110 |
|
| 111 |
-
#
|
| 112 |
submit_btn.click(
|
| 113 |
fn=predict,
|
| 114 |
inputs=[input_img, confidence_slider],
|
|
|
|
| 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)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
import gradio as gr
|
| 125 |
from ultralytics import YOLO
|
| 126 |
+
import torch
|
| 127 |
import os
|
|
|
|
| 128 |
|
| 129 |
# --- DOCUMENTATION STRINGS (Coin Detector App) ---
|
| 130 |
|
|
|
|
| 135 |
|
| 136 |
1. **Upload Image:** Upload the image you want to analyze in the 'Input Image' box.
|
| 137 |
2. **Adjust Threshold:** Use the 'Confidence Threshold' slider to set the minimum certainty required for a coin to be displayed.
|
| 138 |
+
3. **Run:** Click the **"Detect Coins"** button.
|
| 139 |
+
4. **Review:** The output image will show bounding boxes around all detections that meet or exceed the set threshold.
|
| 140 |
"""
|
| 141 |
|
| 142 |
GUIDELINE_INPUT = """
|
|
|
|
| 160 |
"""
|
| 161 |
|
| 162 |
# Load the YOLO model
|
|
|
|
| 163 |
model = YOLO('best1.pt')
|
| 164 |
|
| 165 |
def predict(img, confidence_threshold):
|
| 166 |
# Perform inference
|
| 167 |
+
# Using verbose=False to suppress unnecessary console output during inference
|
| 168 |
results = model(img, verbose=False)
|
| 169 |
|
|
|
|
|
|
|
|
|
|
| 170 |
# We filter the bounding boxes data array based on the confidence score (index 4)
|
| 171 |
+
# Filter predictions based on the confidence threshold
|
|
|
|
| 172 |
filtered_data = [box.cpu() for box in results[0].boxes.data if box[4] >= confidence_threshold]
|
| 173 |
|
| 174 |
if filtered_data:
|
| 175 |
+
# Stack the filtered tensors back into a single tensor
|
| 176 |
filtered_tensor = torch.stack(filtered_data)
|
| 177 |
|
| 178 |
+
# Create a results object to plot only the filtered boxes
|
| 179 |
filtered_results = results[0].cpu()
|
| 180 |
filtered_results.boxes.data = filtered_tensor
|
| 181 |
|
|
|
|
| 205 |
|
| 206 |
# 2. Input/Output Layout
|
| 207 |
with gr.Row():
|
| 208 |
+
with gr.Column(scale=1):
|
|
|
|
| 209 |
input_img = gr.Image(label="Input Image", type="filepath")
|
|
|
|
| 210 |
confidence_slider = gr.Slider(minimum=0, maximum=1, value=0.5, label="Confidence Threshold", step=0.01)
|
|
|
|
| 211 |
submit_btn = gr.Button("Detect Coins", variant="primary")
|
| 212 |
|
| 213 |
+
with gr.Column(scale=2):
|
| 214 |
+
output_img = gr.Image(label="Detected Coins (Annotated Frame)")
|
| 215 |
+
|
| 216 |
|
| 217 |
# 3. Example Data (if available, added here for completeness)
|
| 218 |
# Note: Since no examples were provided, this is commented out or left as placeholders.
|
|
|
|
| 225 |
cache_examples=False
|
| 226 |
)
|
| 227 |
|
| 228 |
+
# 3. Event Handler
|
| 229 |
submit_btn.click(
|
| 230 |
fn=predict,
|
| 231 |
inputs=[input_img, confidence_slider],
|