Spaces:
Running
Running
File size: 1,830 Bytes
79f7e52 ac3b71f d9291e1 cc2ee2f ac3b71f cfe324c d9291e1 cfe324c ac3b71f d9291e1 cfe324c cde2fd0 cfe324c d9291e1 cfe324c d9291e1 cfe324c d9291e1 26f6a32 d9291e1 cfe324c d9291e1 26f6a32 d9291e1 26f6a32 |
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 52 |
import streamlit as st
from PIL import Image
import torch
import os
# Load the trained YOLOv5 model
model = torch.hub.load("ultralytics/yolov5", "custom", path="best.pt") # Load custom model
# Define the prediction function
def predict(image):
# Perform inference on the uploaded image
results = model(image) # Runs YOLOv5 model on the uploaded image
results_img = results.render()[0] # Get image with bounding boxes drawn
return Image.fromarray(results_img)
# Get example images from the images folder
def get_example_images():
examples = []
image_folder = "images"
for filename in os.listdir(image_folder):
if filename.lower().endswith(('.png', '.jpg', '.jpeg')):
examples.append(os.path.join(image_folder, filename))
return examples
# Streamlit UI for Seatbelt Detection with YOLOv5
st.title("Seatbelt Detection with YOLO")
st.markdown("Upload an image to detect Seat-Belt-Detection.")
# Allow the user to upload an image
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_image is not None:
# Open the uploaded image using PIL
image = Image.open(uploaded_image)
# Display the uploaded image
st.image(image, caption="Uploaded Image", use_container_width=True)
# Run the model prediction
st.subheader("Prediction Results:")
result_image = predict(image)
# Display the result image with bounding boxes
st.image(result_image, caption="Detected Image", use_container_width=True)
# Optionally, show example images from the folder
if st.checkbox('Show example images'):
example_images = get_example_images()
for example_image in example_images:
img = Image.open(example_image)
st.image(img, caption=os.path.basename(example_image), use_container_width=True)
|