Spaces:
Runtime error
Runtime error
File size: 1,565 Bytes
c16ff00 8f9586d c16ff00 cdb86cf c16ff00 626674e c16ff00 344f81a c16ff00 344f81a c16ff00 626674e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import streamlit as st
from PIL import Image
from pathlib import Path
from ultralytics import YOLO
# Load YOLOv5 model
model = YOLO('HandSignDetector.pt') # Replace with the path to your best.pt model
# Set up Streamlit
st.title("YOLOv5 Object Detection with Streamlit")
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
from PIL import Image, ImageDraw, ImageFont
def draw_bounding_box(image, box, class_label, probability):
# Convert to ImageDraw format
draw = ImageDraw.Draw(image)
# Draw bounding box
draw.rectangle(box, outline="red", width=3)
# Add class label and probability
label = f"{class_label}: {probability:.2f}"
font = ImageFont.load_default()
text_width, text_height = draw.textsize(label, font)
# Calculate position to center the text inside the bounding box
text_position = ((box[0] + box[2]) - text_width) / 2, box[3] + 5
# Draw text on the image
draw.text(text_position, label, font=font, fill="red")
return draw
if uploaded_file is not None:
# Read the uploaded image
image = Image.open(uploaded_file)
# Inference
results = model(image)
# Display the image with bounding boxes
st.image(image, channels="RGB", caption="Object Detection Result", use_column_width=True)
# Display probability and class for each box
for det in results:
box = det.boxes.xyxy
cls = det.boxes.cls
score = det.boxes.conf
# Draw box on the image
st.image(draw_bounding_box(image,box,cls,score)) |