Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,40 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from ultralytics import YOLO
|
| 3 |
import os
|
|
|
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Load the model
|
| 7 |
model = YOLO('models/best.pt')
|
|
@@ -9,29 +42,60 @@ model = YOLO('models/best.pt')
|
|
| 9 |
# Streamlit UI
|
| 10 |
st.title("Object Detection with Ultralytics")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
#
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
if uploaded_file is not None:
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
for r in result : r.save(filename= img_save_path + uploaded_file.name)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from ultralytics import YOLO
|
| 3 |
import os
|
| 4 |
+
from os import listdir, remove
|
| 5 |
from PIL import Image
|
| 6 |
+
import cv2
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
import requests
|
| 10 |
+
|
| 11 |
+
def download_file(url, destination_folder):
|
| 12 |
+
"""
|
| 13 |
+
Download a file from a URL and save it to a local destination.
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
url (str): The URL of the file to download.
|
| 17 |
+
destination_folder (str): The local folder where the file should be saved.
|
| 18 |
+
"""
|
| 19 |
+
# Create the destination folder if it doesn't exist
|
| 20 |
+
if not os.path.exists(destination_folder):
|
| 21 |
+
os.makedirs(destination_folder)
|
| 22 |
+
|
| 23 |
+
# Extract filename from URL
|
| 24 |
+
filename = url.split('/')[-1]
|
| 25 |
+
destination_path = os.path.join(destination_folder, filename)
|
| 26 |
+
|
| 27 |
+
# Download the file
|
| 28 |
+
with open(destination_path, 'wb') as f:
|
| 29 |
+
response = requests.get(url)
|
| 30 |
+
f.write(response.content)
|
| 31 |
+
|
| 32 |
+
return destination_path
|
| 33 |
+
|
| 34 |
+
url = 'https://yolo-v8-models.s3.ap-south-1.amazonaws.com/best.pt'
|
| 35 |
+
destination_folder = 'models'
|
| 36 |
+
|
| 37 |
+
downloaded_file = download_file(url, destination_folder)
|
| 38 |
|
| 39 |
# Load the model
|
| 40 |
model = YOLO('models/best.pt')
|
|
|
|
| 42 |
# Streamlit UI
|
| 43 |
st.title("Object Detection with Ultralytics")
|
| 44 |
|
| 45 |
+
# Upload image or video
|
| 46 |
+
uploaded_file = st.file_uploader("Choose an image or video", type=["jpg", "jpeg", "png","mp4"])
|
| 47 |
+
|
| 48 |
+
# Demo section
|
| 49 |
+
st.header("Demo")
|
| 50 |
+
col1, col2 = st.columns(2)
|
| 51 |
+
|
| 52 |
+
# Display image in first column
|
| 53 |
+
with col1:
|
| 54 |
+
st.image("demoimg.jpeg", caption="Annotated Image", use_column_width=True)
|
| 55 |
+
|
| 56 |
+
# Display video in second column with adjusted width
|
| 57 |
+
with col2:
|
| 58 |
+
st.write(f'<div style="width: {300};height :500">', unsafe_allow_html=True)
|
| 59 |
+
st.video("demovideo.mp4")
|
| 60 |
+
st.write('</div>', unsafe_allow_html=True)
|
| 61 |
|
| 62 |
if uploaded_file is not None:
|
| 63 |
+
# Check if the uploaded file is a video
|
| 64 |
+
if uploaded_file.type.startswith("video/"):
|
| 65 |
+
# Progress bar to show the progress of object detection
|
| 66 |
+
progress_bar = st.progress(0)
|
| 67 |
+
|
| 68 |
+
st.header(uploaded_file.name)
|
| 69 |
|
| 70 |
+
# Perform object detection
|
| 71 |
+
with st.spinner('Performing object detection...'):
|
| 72 |
+
for percent_complete in range(100):
|
| 73 |
+
result = model.predict(source=uploaded_file, conf=0.2, save=True ,stream=True)
|
| 74 |
+
for i in result : i.save("video.mp4")
|
| 75 |
+
progress_bar.progress(percent_complete + 1)
|
| 76 |
+
|
| 77 |
+
st.success(f"Video saved successfully ")
|
| 78 |
+
# Perform object detection
|
| 79 |
|
|
|
|
| 80 |
|
| 81 |
+
else:
|
| 82 |
+
|
| 83 |
+
# Read the uploaded image
|
| 84 |
+
image = Image.open(uploaded_file)
|
| 85 |
+
|
| 86 |
+
img_name = "converted_image.jpg"
|
| 87 |
+
image.save(img_name)
|
| 88 |
+
|
| 89 |
+
# Perform object detection
|
| 90 |
+
result = model.predict(source=img_name, conf=0.2, save=True)
|
| 91 |
+
|
| 92 |
+
# Save the output image
|
| 93 |
+
img_save_path = "output/"
|
| 94 |
+
os.makedirs(img_save_path, exist_ok=True)
|
| 95 |
+
for r in result:
|
| 96 |
+
r.save(filename=os.path.join(img_save_path, uploaded_file.name))
|
| 97 |
+
|
| 98 |
+
st.success("Detected Object")
|
| 99 |
+
# Display the output image
|
| 100 |
+
st.image(os.path.join(img_save_path, uploaded_file.name), caption="Detected Objects", use_column_width=True)
|
| 101 |
+
|