Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,35 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
|
|
|
|
3 |
from PIL import Image
|
4 |
-
import os
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
|
9 |
-
# Load the YOLO model
|
10 |
-
model = YOLO(model_path)
|
11 |
-
|
12 |
-
# Define the prediction function
|
13 |
def predict(image):
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
# Streamlit UI
|
19 |
-
st.title("Object Detection with
|
20 |
st.markdown("Upload an image for detection.")
|
21 |
|
22 |
-
# Allow the user to upload an image
|
23 |
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
24 |
|
25 |
if uploaded_image is not None:
|
26 |
-
# Open the uploaded image using PIL
|
27 |
image = Image.open(uploaded_image)
|
28 |
-
|
29 |
-
# Display the uploaded image
|
30 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
31 |
|
32 |
-
# Run the model prediction
|
33 |
st.subheader("Prediction Results:")
|
34 |
result_image = predict(image)
|
35 |
-
|
36 |
-
# Display the result image with bounding boxes
|
37 |
st.image(result_image, caption="Detected Image", use_column_width=True)
|
|
|
1 |
import streamlit as st
|
2 |
+
import torch
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
from PIL import Image
|
|
|
6 |
|
7 |
+
# Load YOLOv5 model
|
8 |
+
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
9 |
|
|
|
|
|
|
|
|
|
10 |
def predict(image):
|
11 |
+
# Convert PIL Image to numpy array
|
12 |
+
img_array = np.array(image)
|
13 |
+
|
14 |
+
# Detect objects
|
15 |
+
results = model(img_array)
|
16 |
+
|
17 |
+
# Plot results
|
18 |
+
img_with_boxes = results.render()[0]
|
19 |
+
|
20 |
+
# Convert back to PIL Image
|
21 |
+
return Image.fromarray(img_with_boxes)
|
22 |
|
23 |
+
# Streamlit UI
|
24 |
+
st.title("Object Detection with YOLOv5")
|
25 |
st.markdown("Upload an image for detection.")
|
26 |
|
|
|
27 |
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
28 |
|
29 |
if uploaded_image is not None:
|
|
|
30 |
image = Image.open(uploaded_image)
|
|
|
|
|
31 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
32 |
|
|
|
33 |
st.subheader("Prediction Results:")
|
34 |
result_image = predict(image)
|
|
|
|
|
35 |
st.image(result_image, caption="Detected Image", use_column_width=True)
|