Spaces:
Runtime error
Runtime error
AnkitKUpadhyay
commited on
Commit
•
9b5c19b
1
Parent(s):
561583f
Adding app file, requirements text, and model weights
Browse files- app.py +99 -0
- best.pt +3 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import required libraries
|
2 |
+
import PIL
|
3 |
+
import cv2
|
4 |
+
import streamlit as st
|
5 |
+
from ultralytics import YOLO
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
|
9 |
+
# Replace the relative path to your weight file
|
10 |
+
model_path = '/content/drive/MyDrive/wildfire_augmented_final/experiment_113/weights/best.pt'
|
11 |
+
|
12 |
+
# Setting page layout
|
13 |
+
st.set_page_config(
|
14 |
+
page_title="Object Detection using YOLOv8", # Setting page title
|
15 |
+
page_icon="🤖", # Setting page icon
|
16 |
+
layout="wide", # Setting layout to wide
|
17 |
+
initial_sidebar_state="expanded" # Expanding sidebar by default
|
18 |
+
)
|
19 |
+
|
20 |
+
# Creating sidebar
|
21 |
+
with st.sidebar:
|
22 |
+
st.header("Image/Video Config") # Adding header to sidebar
|
23 |
+
# Adding file uploader to sidebar for selecting images and videos
|
24 |
+
source_file = st.file_uploader(
|
25 |
+
"Choose an image or video...", type=("jpg", "jpeg", "png", 'bmp', 'webp', 'mp4'))
|
26 |
+
|
27 |
+
# Model Options
|
28 |
+
confidence = float(st.slider(
|
29 |
+
"Select Model Confidence", 25, 100, 40)) / 100
|
30 |
+
|
31 |
+
# Creating main page heading
|
32 |
+
st.title("Object Detection using YOLOv8")
|
33 |
+
|
34 |
+
# Creating two columns on the main page
|
35 |
+
col1, col2 = st.columns(2)
|
36 |
+
|
37 |
+
# Adding image to the first column if image is uploaded
|
38 |
+
with col1:
|
39 |
+
if source_file:
|
40 |
+
# Check if the file is an image
|
41 |
+
if source_file.type.split('/')[0] == 'image':
|
42 |
+
# Opening the uploaded image
|
43 |
+
uploaded_image = PIL.Image.open(source_file)
|
44 |
+
# Adding the uploaded image to the page with a caption
|
45 |
+
st.image(source_file,
|
46 |
+
caption="Uploaded Image",
|
47 |
+
use_column_width=True
|
48 |
+
)
|
49 |
+
else:
|
50 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
51 |
+
tfile.write(source_file.read())
|
52 |
+
vidcap = cv2.VideoCapture(tfile.name)
|
53 |
+
|
54 |
+
try:
|
55 |
+
model = YOLO(model_path)
|
56 |
+
except Exception as ex:
|
57 |
+
st.error(
|
58 |
+
f"Unable to load model. Check the specified path: {model_path}")
|
59 |
+
st.error(ex)
|
60 |
+
|
61 |
+
if st.sidebar.button('Detect Objects'):
|
62 |
+
if source_file.type.split('/')[0] == 'image':
|
63 |
+
res = model.predict(uploaded_image,
|
64 |
+
conf=confidence
|
65 |
+
)
|
66 |
+
boxes = res[0].boxes
|
67 |
+
res_plotted = res[0].plot()[:, :, ::-1]
|
68 |
+
with col2:
|
69 |
+
st.image(res_plotted,
|
70 |
+
caption='Detected Image',
|
71 |
+
use_column_width=True
|
72 |
+
)
|
73 |
+
try:
|
74 |
+
with st.expander("Detection Results"):
|
75 |
+
for box in boxes:
|
76 |
+
st.write(box.xywh)
|
77 |
+
except Exception as ex:
|
78 |
+
st.write("No image is uploaded yet!")
|
79 |
+
else:
|
80 |
+
# Open the video file
|
81 |
+
success, image = vidcap.read()
|
82 |
+
while success:
|
83 |
+
res = model.predict(image,
|
84 |
+
conf=confidence
|
85 |
+
)
|
86 |
+
boxes = res[0].boxes
|
87 |
+
res_plotted = res[0].plot()[:, :, ::-1]
|
88 |
+
with col2:
|
89 |
+
st.image(res_plotted,
|
90 |
+
caption='Detected Frame',
|
91 |
+
use_column_width=True
|
92 |
+
)
|
93 |
+
try:
|
94 |
+
with st.expander("Detection Results"):
|
95 |
+
for box in boxes:
|
96 |
+
st.write(box.xywh)
|
97 |
+
except Exception as ex:
|
98 |
+
st.write("No video is uploaded yet!")
|
99 |
+
success, image = vidcap.read()
|
best.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:05a5e990d1bcb3e4f2b6f38d48baffba4418baf65f3d426af0cecce43ecd4eab
|
3 |
+
size 6236761
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.29.0
|
2 |
+
torch==1.8.0
|
3 |
+
Pillow==7.1.0
|
4 |
+
opencv-python-headless==4.6.0
|
5 |
+
ultralytics==8.0.221
|