Spaces:
Running
Running
Upload 5 files
Browse files- .gitattributes +1 -0
- OIP (4).jpg +0 -0
- best.pt +3 -0
- input_video.mp4 +3 -0
- n.py +122 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
input_video.mp4 filter=lfs diff=lfs merge=lfs -text
|
OIP (4).jpg
ADDED
|
best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4945128a2f1a133d1429a3e04861177cb8f95ba9445d27becf6cf832fdd731e5
|
| 3 |
+
size 6254425
|
input_video.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e4ea71aa6ffc460c430debe677e9cecea6e2a1712d45ce26d48ace0a1aa76a83
|
| 3 |
+
size 4143534
|
n.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import PIL
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
import cv2
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Give the path of the best.pt (best weights)
|
| 8 |
+
model_dir="model"
|
| 9 |
+
model_file="best.pt"
|
| 10 |
+
model_path = os.path.join(model_dir, model_file)
|
| 11 |
+
|
| 12 |
+
# Setting page layout
|
| 13 |
+
st.set_page_config(
|
| 14 |
+
page_title="PPE(Private Protective Equipment)", # Setting page title
|
| 15 |
+
#page_icon="NK logo.jpeg", # Setting page icon
|
| 16 |
+
layout="wide", # Setting layout to wide
|
| 17 |
+
initial_sidebar_state="expanded", # Expanding sidebar by default
|
| 18 |
+
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
model = YOLO(model_path)
|
| 23 |
+
names=model.names
|
| 24 |
+
except Exception as ex:
|
| 25 |
+
st.error(
|
| 26 |
+
f"Unable to load model. Check the specified path: {model_path}")
|
| 27 |
+
st.error(ex)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# Creating sidebar
|
| 31 |
+
with st.sidebar:
|
| 32 |
+
st.header("Upload The Image") # Adding header to sidebar
|
| 33 |
+
# Adding file uploader to sidebar for selecting images
|
| 34 |
+
source = st.file_uploader(
|
| 35 |
+
"Upload an image or video...", type=("jpg", "jpeg", "png", 'bmp', 'webp','mp4'))
|
| 36 |
+
|
| 37 |
+
#st.sidebar("Upload the video") #adding header to sidebar
|
| 38 |
+
# Adding file uploader to sidebar for selecting videos
|
| 39 |
+
#source_video=st.file_uploader("Upload a video...", type=("mp4"))
|
| 40 |
+
|
| 41 |
+
# Model Options
|
| 42 |
+
confidence = float(st.slider(
|
| 43 |
+
"Select Model Confidence", 25, 100, 40)) / 100
|
| 44 |
+
|
| 45 |
+
# Creating main page heading
|
| 46 |
+
st.title("PPE(Private Protective Equipment) Detection")
|
| 47 |
+
st.caption('Updload a photo or video by selecting :blue[Browse files]')
|
| 48 |
+
st.caption('Then click the :blue[Detect Objects] button and check the result.')
|
| 49 |
+
# Creating two columns on the main page
|
| 50 |
+
col1, col2 = st.columns(2)
|
| 51 |
+
|
| 52 |
+
# Adding image to the first column if image is uploaded
|
| 53 |
+
with col1:
|
| 54 |
+
#checking if the source is not empty
|
| 55 |
+
if source is not None:
|
| 56 |
+
|
| 57 |
+
#getting the file extentions from the uploaded file using name.split()
|
| 58 |
+
file_extension = source.name.split(".")[-1]
|
| 59 |
+
|
| 60 |
+
#checking if the file extention is image or not
|
| 61 |
+
if file_extension in ["jpg", "jpeg", "png"]:
|
| 62 |
+
|
| 63 |
+
# Opening the uploaded image
|
| 64 |
+
uploaded_image = PIL.Image.open(source)
|
| 65 |
+
|
| 66 |
+
# Getting the image size
|
| 67 |
+
image_width, image_height = uploaded_image.size
|
| 68 |
+
|
| 69 |
+
# Adding the uploaded image to the page with a caption
|
| 70 |
+
st.image(uploaded_image,
|
| 71 |
+
caption="Uploaded Image",
|
| 72 |
+
width=image_width
|
| 73 |
+
)
|
| 74 |
+
#checking if the the button is clicked
|
| 75 |
+
if st.sidebar.button('Detect Objects'):
|
| 76 |
+
#prediction based on the image with conf=confidence from the slider
|
| 77 |
+
res = model.predict(uploaded_image,
|
| 78 |
+
conf=confidence,
|
| 79 |
+
line_width=2,
|
| 80 |
+
show_labels=False,
|
| 81 |
+
show_conf=False
|
| 82 |
+
)
|
| 83 |
+
#extracting information about the bounding box from res
|
| 84 |
+
boxes = res[0].boxes
|
| 85 |
+
#plotting the bounding box with confidence and labels
|
| 86 |
+
res_plotted = res[0].plot(labels=True, line_width=1)[:, :, ::-1]
|
| 87 |
+
with col2:
|
| 88 |
+
st.image(res_plotted,
|
| 89 |
+
caption='Detected Image',
|
| 90 |
+
width=image_width
|
| 91 |
+
)
|
| 92 |
+
try:
|
| 93 |
+
st.write(f'Number of detected: {len(boxes)}')
|
| 94 |
+
with st.expander("class detected:"):
|
| 95 |
+
for c in boxes.cls:
|
| 96 |
+
st.write(names[int(c)])
|
| 97 |
+
#print(names[int(c)])
|
| 98 |
+
except Exception as ex:
|
| 99 |
+
st.write("No image is uploaded yet!")
|
| 100 |
+
|
| 101 |
+
elif file_extension == "mp4":
|
| 102 |
+
|
| 103 |
+
video_bytes = source.read()
|
| 104 |
+
st.video(video_bytes)
|
| 105 |
+
# Save video locally
|
| 106 |
+
with open("input_video.mp4", "wb") as f:
|
| 107 |
+
f.write(video_bytes)
|
| 108 |
+
if st.sidebar.button("Detect Objects"):
|
| 109 |
+
with col2:
|
| 110 |
+
vid_cap = cv2.VideoCapture("input_video.mp4")
|
| 111 |
+
st_frame = st.empty()
|
| 112 |
+
while vid_cap.isOpened():
|
| 113 |
+
success, image = vid_cap.read()
|
| 114 |
+
if success:
|
| 115 |
+
image = cv2.resize(image, (720, int(720 * (9 / 16))))
|
| 116 |
+
res = model.predict(image, conf=confidence)
|
| 117 |
+
result_tensor = res[0].boxes
|
| 118 |
+
res_plotted = res[0].plot()
|
| 119 |
+
st_frame.image(res_plotted, caption="Detected Video", channels="BGR", use_column_width=True)
|
| 120 |
+
else:
|
| 121 |
+
vid_cap.release()
|
| 122 |
+
break
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics==8.0.196
|
| 2 |
+
jupyter
|
| 3 |
+
ipykernel
|