Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,37 @@
|
|
1 |
import streamlit as st
|
2 |
-
import torch
|
3 |
from ultralytics import YOLO
|
4 |
from PIL import Image
|
5 |
import os
|
6 |
-
import sys
|
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 |
-
st.
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
image = Image.open(uploaded_image)
|
41 |
-
|
42 |
-
# Display the uploaded image
|
43 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
44 |
-
|
45 |
-
# Run the model prediction
|
46 |
-
st.subheader("Prediction Results:")
|
47 |
-
result_image = predict(image)
|
48 |
-
|
49 |
-
# Display the result image with bounding boxes
|
50 |
-
st.image(result_image, caption="Detected Image", use_column_width=True)
|
51 |
-
|
52 |
-
except Exception as e:
|
53 |
-
st.error(f"An error occurred: {e}")
|
54 |
-
st.error(f"Traceback: {sys.exc_info()}")
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from ultralytics import YOLO
|
3 |
from PIL import Image
|
4 |
import os
|
|
|
5 |
|
6 |
+
# Directly load the model using full path if needed
|
7 |
+
model_path = os.path.join(os.getcwd(), 'best.pt')
|
8 |
+
|
9 |
+
# Load the YOLO model
|
10 |
+
model = YOLO(model_path)
|
11 |
+
|
12 |
+
# Define the prediction function
|
13 |
+
def predict(image):
|
14 |
+
results = model(image) # Run YOLOv8 model on the uploaded image
|
15 |
+
results_img = results[0].plot() # Get image with bounding boxes
|
16 |
+
return Image.fromarray(results_img)
|
17 |
+
|
18 |
+
# Streamlit UI for Object Detection
|
19 |
+
st.title("Object Detection with YOLOv8")
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|