File size: 1,375 Bytes
127262b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import streamlit as st
from PIL import Image
import io

# Load the YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'custom', path='fire.pt')  # Load custom model

# Streamlit interface
st.title("YOLOv5 Image Detection")
st.write("Upload an image to detect objects using YOLOv5")

# File uploader for image
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    # Convert the uploaded file to a PIL Image
    image = Image.open(uploaded_file)

    # Run the YOLOv5 model
    results = model(image)

    # Save the results image
    results_image = results.render()[0]  # Render returns a list, we take the first element

    # Convert the numpy array result to an image
    results_image = Image.fromarray(results_image)

    # Save to a buffer
    buf = io.BytesIO()
    results_image.save(buf, format='JPEG')
    byte_im = buf.getvalue()

    # Display the input and output images side by side
    st.image(image, caption='Uploaded Image', use_column_width=True)
    st.image(results_image, caption='Detected Image', use_column_width=True)

    # Provide a download button for the output image
    st.download_button(
        label="Download Output Image",
        data=byte_im,
        file_name="output.jpg",
        mime="image/jpeg"
    )