Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,54 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
from ultralytics import YOLO
|
3 |
from PIL import Image
|
4 |
import os
|
|
|
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 |
-
|
|
|
|
|
|
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 |
+
# Print Python and library versions for debugging
|
9 |
+
st.write(f"Python version: {sys.version}")
|
10 |
+
st.write(f"Torch version: {torch.__version__}")
|
11 |
+
st.write(f"Current working directory: {os.getcwd()}")
|
12 |
+
st.write(f"Files in current directory: {os.listdir('.')}")
|
13 |
+
|
14 |
+
# Check if the model file exists
|
15 |
+
model_path = "best.pt" # or the path to your actual model file
|
16 |
+
if not os.path.exists(model_path):
|
17 |
+
st.error(f"Model file {model_path} not found!")
|
18 |
+
else:
|
19 |
+
st.success(f"Model file {model_path} found!")
|
20 |
+
|
21 |
+
try:
|
22 |
+
# Load the trained YOLOv8 model
|
23 |
+
model = YOLO(model_path)
|
24 |
+
|
25 |
+
# Define the prediction function
|
26 |
+
def predict(image):
|
27 |
+
results = model(image) # Run YOLOv8 model on the uploaded image
|
28 |
+
results_img = results[0].plot() # Get image with bounding boxes
|
29 |
+
return Image.fromarray(results_img)
|
30 |
+
|
31 |
+
# Streamlit UI for Object Detection
|
32 |
+
st.title("Object Detection with YOLOv8")
|
33 |
+
st.markdown("Upload an image for detection.")
|
34 |
+
|
35 |
+
# Allow the user to upload an image
|
36 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
37 |
+
|
38 |
+
if uploaded_image is not None:
|
39 |
+
# Open the uploaded image using PIL
|
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()}")
|