| from pathlib import Path | |
| from PIL import Image | |
| import pathlib | |
| import numpy as np | |
| import torch | |
| import streamlit as st | |
| import cv2 | |
| #If you have linux (or deploying for linux) use: | |
| pathlib.WindowsPath = pathlib.PosixPath | |
| # Load YOLOv5 model | |
| model = torch.hub.load('./yolov5', 'custom', path='./yolo/best.pt', source='local', force_reload=True) | |
| st.title("YOLO Object Detection Web App") | |
| # Upload image | |
| uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| # Convert the file to an OpenCV image | |
| image = Image.open(uploaded_file) | |
| st.image(image, caption="Uploaded Image", use_column_width=True) | |
| st.write("Processing...") | |
| # Convert the image to a format compatible with YOLO | |
| image_np = np.array(image) | |
| image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR) | |
| # Perform YOLO detection | |
| results = model(image_cv) | |
| # Render the results | |
| detected_image = np.squeeze(results.render()) | |
| # Display result | |
| st.image(detected_image, caption="Detected Image", use_column_width=True) |