Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,27 @@ model = YOLO('HandSignDetector.pt') # Replace with the path to your best.pt mod
|
|
| 10 |
st.title("YOLOv5 Object Detection with Streamlit")
|
| 11 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if uploaded_file is not None:
|
| 14 |
# Read the uploaded image
|
| 15 |
image = Image.open(uploaded_file)
|
|
@@ -27,5 +48,4 @@ if uploaded_file is not None:
|
|
| 27 |
score = det.boxes.conf
|
| 28 |
|
| 29 |
# Draw box on the image
|
| 30 |
-
st.image(
|
| 31 |
-
channels="RGB", caption=f"Class {label} - Confidence: {score:.2f}", use_column_width=True)
|
|
|
|
| 10 |
st.title("YOLOv5 Object Detection with Streamlit")
|
| 11 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 12 |
|
| 13 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 14 |
+
|
| 15 |
+
def draw_bounding_box(image, box, class_label, probability):
|
| 16 |
+
# Convert to ImageDraw format
|
| 17 |
+
draw = ImageDraw.Draw(image)
|
| 18 |
+
|
| 19 |
+
# Draw bounding box
|
| 20 |
+
draw.rectangle(box, outline="red", width=3)
|
| 21 |
+
|
| 22 |
+
# Add class label and probability
|
| 23 |
+
label = f"{class_label}: {probability:.2f}"
|
| 24 |
+
font = ImageFont.load_default()
|
| 25 |
+
text_width, text_height = draw.textsize(label, font)
|
| 26 |
+
|
| 27 |
+
# Calculate position to center the text inside the bounding box
|
| 28 |
+
text_position = ((box[0] + box[2]) - text_width) / 2, box[3] + 5
|
| 29 |
+
|
| 30 |
+
# Draw text on the image
|
| 31 |
+
draw.text(text_position, label, font=font, fill="red")
|
| 32 |
+
return draw
|
| 33 |
+
|
| 34 |
if uploaded_file is not None:
|
| 35 |
# Read the uploaded image
|
| 36 |
image = Image.open(uploaded_file)
|
|
|
|
| 48 |
score = det.boxes.conf
|
| 49 |
|
| 50 |
# Draw box on the image
|
| 51 |
+
st.image(draw_bounding_box(image,box,cls,score))
|
|
|