Spaces:
Runtime error
Runtime error
Initial commit
Browse files- apd_utils.py +34 -0
- app.py +110 -0
- ckpt_best_yolonas.pth +3 -0
- default_img.png +0 -0
- default_img_res.png +0 -0
- packages.txt +7 -0
- requirements.txt +168 -0
apd_utils.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gdown
|
| 3 |
+
import subprocess
|
| 4 |
+
|
| 5 |
+
def write_video(source_vid):
|
| 6 |
+
os.makedirs('temp', exist_ok=True)
|
| 7 |
+
temp_uploaded_path = f'temp/{source_vid.name}'
|
| 8 |
+
|
| 9 |
+
with open(temp_uploaded_path, mode='wb') as temp:
|
| 10 |
+
temp.write(source_vid.read())
|
| 11 |
+
|
| 12 |
+
return temp_uploaded_path
|
| 13 |
+
|
| 14 |
+
def convert_video(in_path, out_path):
|
| 15 |
+
command = [
|
| 16 |
+
'ffmpeg',
|
| 17 |
+
'-i', in_path,
|
| 18 |
+
'-vcodec', 'libx264',
|
| 19 |
+
'-y',
|
| 20 |
+
out_path
|
| 21 |
+
]
|
| 22 |
+
subprocess.run(command)
|
| 23 |
+
|
| 24 |
+
def download_model(url):
|
| 25 |
+
os.makedirs('models', exist_ok=True)
|
| 26 |
+
gdown.download(url, 'models/ckpt_best_1.pth', fuzzy=True)
|
| 27 |
+
|
| 28 |
+
def delete_temp():
|
| 29 |
+
files = os.listdir('./temp')
|
| 30 |
+
|
| 31 |
+
for file_name in files:
|
| 32 |
+
file_path = os.path.join(folder_path, file_name)
|
| 33 |
+
if os.path.isfile(file_path):
|
| 34 |
+
os.remove(file_path)
|
app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from super_gradients.training import models
|
| 2 |
+
from apd_utils import write_video, convert_video
|
| 3 |
+
import torch, PIL, os
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
CLASSES = ['Dust Mask', 'Eye Wear', 'Glove', 'Protective Boots', 'Protective Helmet', 'Safety Vest', 'Shield']
|
| 7 |
+
SOURCES = ['Images', 'Videos']
|
| 8 |
+
|
| 9 |
+
# Setting page layout
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="PPE Object Detection using YOLO-NAS",
|
| 12 |
+
page_icon="👷",
|
| 13 |
+
layout="wide",
|
| 14 |
+
initial_sidebar_state="expanded"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Main page heading
|
| 18 |
+
st.title("PPE Object Detection using YOLO-NAS")
|
| 19 |
+
|
| 20 |
+
# Sidebar
|
| 21 |
+
st.sidebar.header("YOLO-NAS Model Config")
|
| 22 |
+
|
| 23 |
+
# Model Options
|
| 24 |
+
confidence = float(st.sidebar.slider(
|
| 25 |
+
"Select Model Confidence", 0, 100, 40)) / 100
|
| 26 |
+
|
| 27 |
+
st.sidebar.header("Image/Video Config")
|
| 28 |
+
source_radio = st.sidebar.radio("Select Source", SOURCES)
|
| 29 |
+
|
| 30 |
+
source_img = None
|
| 31 |
+
source_vid = None
|
| 32 |
+
|
| 33 |
+
#with st.spinner('Downloading model..'):
|
| 34 |
+
#model_url = 'https://drive.google.com/file/d/1XOq3OkpQ3OgibjHmYOCMsQPBtqjdf2i3/view?usp=sharing'
|
| 35 |
+
#download_model(model_url)
|
| 36 |
+
|
| 37 |
+
model = models.get('yolo_nas_m',
|
| 38 |
+
num_classes=len(CLASSES),
|
| 39 |
+
checkpoint_path="./ckpt_best_yolonas.pth")
|
| 40 |
+
|
| 41 |
+
device = 'cuda' if torch.cuda.is_available() else "cpu"
|
| 42 |
+
device = 'cpu'
|
| 43 |
+
|
| 44 |
+
if source_radio == 'Images':
|
| 45 |
+
source_img = st.sidebar.file_uploader(
|
| 46 |
+
"Choose an image...", type=("jpg", "jpeg", "png", 'bmp', 'webp'))
|
| 47 |
+
col1, col2 = st.columns(2)
|
| 48 |
+
|
| 49 |
+
with col1:
|
| 50 |
+
try:
|
| 51 |
+
if source_img is None:
|
| 52 |
+
st.image('default_img.png', caption="Default Image",
|
| 53 |
+
use_column_width=True)
|
| 54 |
+
else:
|
| 55 |
+
uploaded_image = PIL.Image.open(source_img)
|
| 56 |
+
st.image(source_img, caption="Uploaded Image",
|
| 57 |
+
use_column_width=True)
|
| 58 |
+
except Exception as ex:
|
| 59 |
+
st.error("Error occurred while opening the image.")
|
| 60 |
+
st.error(ex)
|
| 61 |
+
with col2:
|
| 62 |
+
if source_img is None:
|
| 63 |
+
st.image('default_img_res.png', caption="Detected Objects",
|
| 64 |
+
use_column_width=True)
|
| 65 |
+
else:
|
| 66 |
+
if st.sidebar.button('Detect Objects'):
|
| 67 |
+
res = model.to(device).predict(uploaded_image,
|
| 68 |
+
conf=confidence)
|
| 69 |
+
st.image(res.draw(), caption='Detected Image',
|
| 70 |
+
use_column_width=True)
|
| 71 |
+
|
| 72 |
+
elif source_radio == 'Videos':
|
| 73 |
+
source_vid = st.sidebar.file_uploader(
|
| 74 |
+
"Choose a video ...", type=("mp4", "mov", "webM"))
|
| 75 |
+
|
| 76 |
+
col1, col2 = st.columns(2)
|
| 77 |
+
|
| 78 |
+
with col1:
|
| 79 |
+
if source_vid is None:
|
| 80 |
+
st.image('default_img.png', caption="Default Image",
|
| 81 |
+
use_column_width=True)
|
| 82 |
+
else:
|
| 83 |
+
try:
|
| 84 |
+
uploaded_video = source_vid.getvalue()
|
| 85 |
+
st.video(uploaded_video)
|
| 86 |
+
except Exception as ex:
|
| 87 |
+
st.error("Error occurred while opening the video.")
|
| 88 |
+
st.error(ex)
|
| 89 |
+
with col2:
|
| 90 |
+
if source_vid is None:
|
| 91 |
+
st.image('default_img_res.png', caption="Detected Objects",
|
| 92 |
+
use_column_width=True)
|
| 93 |
+
else:
|
| 94 |
+
if st.sidebar.button('Detect Objects'):
|
| 95 |
+
temp_uploaded_path = write_video(source_vid)
|
| 96 |
+
res = model.to(device).predict(temp_uploaded_path, conf=confidence)
|
| 97 |
+
|
| 98 |
+
with st.spinner('Processing video ...'):
|
| 99 |
+
in_temp_res_path = "./temp/result.mp4"
|
| 100 |
+
out_temp_res_path = "./temp/result2.mp4"
|
| 101 |
+
|
| 102 |
+
res.save(in_temp_res_path)
|
| 103 |
+
convert_video(in_temp_res_path, out_temp_res_path)
|
| 104 |
+
st.video(out_temp_res_path)
|
| 105 |
+
|
| 106 |
+
os.remove(temp_uploaded_path)
|
| 107 |
+
os.remove(in_temp_res_path)
|
| 108 |
+
os.remove(out_temp_res_path)
|
| 109 |
+
else:
|
| 110 |
+
st.error("Please select a valid source type!")
|
ckpt_best_yolonas.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1a3a5dc7442e873aa894e672388910e87456292e9e4ff941630757fab2754e83
|
| 3 |
+
size 681102850
|
default_img.png
ADDED
|
default_img_res.png
ADDED
|
packages.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
freeglut3-dev
|
| 2 |
+
libgtk2.0-dev
|
| 3 |
+
libgl1-mesa-glx
|
| 4 |
+
tesseract-ocr
|
| 5 |
+
libtesseract-dev
|
| 6 |
+
libtesseract4
|
| 7 |
+
tesseract-ocr-all
|
requirements.txt
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
absl-py==2.1.0
|
| 2 |
+
alabaster==0.7.16
|
| 3 |
+
albumentations==1.3.1
|
| 4 |
+
altair==5.2.0
|
| 5 |
+
antlr4-python3-runtime==4.9.3
|
| 6 |
+
arabic-reshaper==3.0.0
|
| 7 |
+
asn1crypto==1.5.1
|
| 8 |
+
attrs==23.2.0
|
| 9 |
+
Babel==2.14.0
|
| 10 |
+
beautifulsoup4==4.12.3
|
| 11 |
+
blinker==1.7.0
|
| 12 |
+
boto3==1.34.54
|
| 13 |
+
botocore==1.34.54
|
| 14 |
+
build==1.1.1
|
| 15 |
+
cachetools==5.3.3
|
| 16 |
+
certifi==2024.2.2
|
| 17 |
+
cffi==1.16.0
|
| 18 |
+
charset-normalizer==3.3.2
|
| 19 |
+
click==8.1.7
|
| 20 |
+
coloredlogs==15.0.1
|
| 21 |
+
contourpy==1.2.0
|
| 22 |
+
coverage==5.3.1
|
| 23 |
+
cryptography==42.0.5
|
| 24 |
+
cssselect2==0.7.0
|
| 25 |
+
cycler==0.12.1
|
| 26 |
+
data-gradients==0.3.2
|
| 27 |
+
Deprecated==1.2.14
|
| 28 |
+
docutils==0.17.1
|
| 29 |
+
einops==0.3.2
|
| 30 |
+
filelock==3.13.1
|
| 31 |
+
flatbuffers==23.5.26
|
| 32 |
+
fonttools==4.49.0
|
| 33 |
+
fsspec==2024.2.0
|
| 34 |
+
future==1.0.0
|
| 35 |
+
gdown==5.1.0
|
| 36 |
+
gitdb==4.0.11
|
| 37 |
+
GitPython==3.1.42
|
| 38 |
+
grpcio==1.62.0
|
| 39 |
+
html5lib==1.1
|
| 40 |
+
humanfriendly==10.0
|
| 41 |
+
hydra-core==1.3.2
|
| 42 |
+
idna==3.6
|
| 43 |
+
imagededup==0.3.2
|
| 44 |
+
imageio==2.34.0
|
| 45 |
+
imagesize==1.4.1
|
| 46 |
+
importlib-metadata==7.0.1
|
| 47 |
+
importlib_resources==6.1.2
|
| 48 |
+
Jinja2==3.1.3
|
| 49 |
+
jmespath==1.0.1
|
| 50 |
+
joblib==1.3.2
|
| 51 |
+
json-tricks==3.16.1
|
| 52 |
+
jsonschema==4.21.1
|
| 53 |
+
jsonschema-specifications==2023.12.1
|
| 54 |
+
kiwisolver==1.4.5
|
| 55 |
+
lazy_loader==0.3
|
| 56 |
+
lxml==5.1.0
|
| 57 |
+
Markdown==3.5.2
|
| 58 |
+
markdown-it-py==3.0.0
|
| 59 |
+
MarkupSafe==2.1.5
|
| 60 |
+
matplotlib==3.8.3
|
| 61 |
+
mdurl==0.1.2
|
| 62 |
+
mpmath==1.3.0
|
| 63 |
+
networkx==3.2.1
|
| 64 |
+
numpy==1.23.0
|
| 65 |
+
nvidia-cublas-cu12==12.1.3.1
|
| 66 |
+
nvidia-cuda-cupti-cu12==12.1.105
|
| 67 |
+
nvidia-cuda-nvrtc-cu12==12.1.105
|
| 68 |
+
nvidia-cuda-runtime-cu12==12.1.105
|
| 69 |
+
nvidia-cudnn-cu12==8.9.2.26
|
| 70 |
+
nvidia-cufft-cu12==11.0.2.54
|
| 71 |
+
nvidia-curand-cu12==10.3.2.106
|
| 72 |
+
nvidia-cusolver-cu12==11.4.5.107
|
| 73 |
+
nvidia-cusparse-cu12==12.1.0.106
|
| 74 |
+
nvidia-nccl-cu12==2.19.3
|
| 75 |
+
nvidia-nvjitlink-cu12==12.3.101
|
| 76 |
+
nvidia-nvtx-cu12==12.1.105
|
| 77 |
+
omegaconf==2.3.0
|
| 78 |
+
onnx==1.13.0
|
| 79 |
+
onnxruntime==1.13.1
|
| 80 |
+
onnxsim==0.4.35
|
| 81 |
+
opencv-python==4.9.0.80
|
| 82 |
+
opencv-python-headless==4.9.0.80
|
| 83 |
+
oscrypto==1.3.0
|
| 84 |
+
packaging==23.2
|
| 85 |
+
pandas==2.2.1
|
| 86 |
+
pillow==10.2.0
|
| 87 |
+
pip-tools==7.4.0
|
| 88 |
+
platformdirs==4.2.0
|
| 89 |
+
protobuf==3.20.3
|
| 90 |
+
psutil==5.9.8
|
| 91 |
+
pyarrow==15.0.0
|
| 92 |
+
pycocotools==2.0.6
|
| 93 |
+
pycparser==2.21
|
| 94 |
+
pydeck==0.8.1b0
|
| 95 |
+
pyDeprecate==0.3.2
|
| 96 |
+
Pygments==2.17.2
|
| 97 |
+
pyHanko==0.21.0
|
| 98 |
+
pyhanko-certvalidator==0.26.3
|
| 99 |
+
pyparsing==2.4.5
|
| 100 |
+
pypdf==4.0.2
|
| 101 |
+
pypng==0.20220715.0
|
| 102 |
+
pyproject_hooks==1.0.0
|
| 103 |
+
PySocks==1.7.1
|
| 104 |
+
python-bidi==0.4.2
|
| 105 |
+
python-dateutil==2.9.0.post0
|
| 106 |
+
pytz==2024.1
|
| 107 |
+
PyWavelets==1.5.0
|
| 108 |
+
PyYAML==6.0.1
|
| 109 |
+
qrcode==7.4.2
|
| 110 |
+
qudida==0.0.4
|
| 111 |
+
rapidfuzz==3.6.1
|
| 112 |
+
referencing==0.33.0
|
| 113 |
+
reportlab==3.6.13
|
| 114 |
+
requests==2.31.0
|
| 115 |
+
rich==13.7.1
|
| 116 |
+
rpds-py==0.18.0
|
| 117 |
+
s3transfer==0.10.0
|
| 118 |
+
scikit-image==0.22.0
|
| 119 |
+
scikit-learn==1.4.1.post1
|
| 120 |
+
scipy==1.12.0
|
| 121 |
+
seaborn==0.13.2
|
| 122 |
+
six==1.16.0
|
| 123 |
+
smmap==5.0.1
|
| 124 |
+
snowballstemmer==2.2.0
|
| 125 |
+
soupsieve==2.5
|
| 126 |
+
Sphinx==4.0.3
|
| 127 |
+
sphinx-rtd-theme==1.3.0
|
| 128 |
+
sphinxcontrib-applehelp==1.0.8
|
| 129 |
+
sphinxcontrib-devhelp==1.0.6
|
| 130 |
+
sphinxcontrib-htmlhelp==2.0.5
|
| 131 |
+
sphinxcontrib-jquery==4.1
|
| 132 |
+
sphinxcontrib-jsmath==1.0.1
|
| 133 |
+
sphinxcontrib-qthelp==1.0.7
|
| 134 |
+
sphinxcontrib-serializinghtml==1.1.10
|
| 135 |
+
streamlit==1.31.1
|
| 136 |
+
stringcase==1.2.0
|
| 137 |
+
super-gradients==3.6.0
|
| 138 |
+
svglib==1.5.1
|
| 139 |
+
sympy==1.12
|
| 140 |
+
tenacity==8.2.3
|
| 141 |
+
tensorboard==2.16.2
|
| 142 |
+
tensorboard-data-server==0.7.2
|
| 143 |
+
termcolor==1.1.0
|
| 144 |
+
threadpoolctl==3.3.0
|
| 145 |
+
tifffile==2024.2.12
|
| 146 |
+
tinycss2==1.2.1
|
| 147 |
+
toml==0.10.2
|
| 148 |
+
tomli==2.0.1
|
| 149 |
+
toolz==0.12.1
|
| 150 |
+
torch==2.2.1
|
| 151 |
+
torchmetrics==0.8.0
|
| 152 |
+
torchvision==0.17.1
|
| 153 |
+
tornado==6.4
|
| 154 |
+
tqdm==4.66.2
|
| 155 |
+
treelib==1.6.1
|
| 156 |
+
triton==2.2.0
|
| 157 |
+
typing_extensions==4.10.0
|
| 158 |
+
tzdata==2024.1
|
| 159 |
+
tzlocal==5.2
|
| 160 |
+
uritools==4.0.2
|
| 161 |
+
urllib3==1.26.18
|
| 162 |
+
validators==0.22.0
|
| 163 |
+
watchdog==4.0.0
|
| 164 |
+
webencodings==0.5.1
|
| 165 |
+
Werkzeug==3.0.1
|
| 166 |
+
wrapt==1.16.0
|
| 167 |
+
xhtml2pdf==0.2.11
|
| 168 |
+
zipp==3.17.0
|