File size: 1,609 Bytes
64ea6aa
 
 
da3a1b7
 
61a8794
da3a1b7
d91b0f6
da3a1b7
 
 
 
 
 
 
 
 
 
 
 
d91b0f6
 
da3a1b7
 
 
 
d91b0f6
da3a1b7
 
d91b0f6
 
 
 
61a8794
da3a1b7
 
d91b0f6
da3a1b7
d91b0f6
 
61a8794
da3a1b7
 
d91b0f6
da3a1b7
 
d91b0f6
64ea6aa
d91b0f6
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
import streamlit as st
import torch
import cv2
from PIL import Image
import numpy as np

# YOLOv5 Model Loading (best.pt)
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')  # Replace 'best.pt' with your model's path

# Streamlit UI
st.title('YOLOv5 Object Detection')

# Input Options
upload_option = st.radio("Choose an input option:", ("Upload Image", "Real-Time Webcam"))

# Image Upload Handling
if upload_option == "Upload Image":
    uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        results = model(image)  # Perform inference
        st.image(results.render()[0], caption='Detected Objects', use_column_width=True)  # Display results

# Real-Time Webcam Detection
if upload_option == "Real-Time Webcam":
    run = st.checkbox('Run Webcam')
    FRAME_WINDOW = st.image([])  # Display window for webcam frames

    if run:
        cap = cv2.VideoCapture(0)  # Open webcam (0 for default)

        if not cap.isOpened():
            st.write("Error: Unable to open webcam")
            st.stop()

        while run:
            ret, frame = cap.read()  # Capture frame
            if not ret:
                st.write("Error: Unable to capture frame")
                break

            # Convert to RGB and detect objects
            frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            results = model(frame_rgb)

            annotated_frame = results.render()[0]
            FRAME_WINDOW.image(annotated_frame)

        cap.release()  # Release webcam