Spaces:
Runtime error
Runtime error
Upload 15 files
#1
by tkau - opened
- .gitignore +144 -0
- algorithm.py +177 -0
- app.py +125 -0
- assets/Objdetectionyoutubegif-1.m4v +0 -0
- assets/pic1.png +0 -0
- assets/pic3.png +0 -0
- assets/segmentation.png +0 -0
- helper.py +80 -0
- images/office_4.jpg +0 -0
- images/office_4_detected.jpg +0 -0
- requirements.txt +4 -0
- settings.py +42 -0
- weights/yolov8n-sec.pt +3 -0
- weights/yolov8n-seg.pt +3 -0
- weights/yolov8n.pt +3 -0
.gitignore
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Local folder
|
| 10 |
+
local_folder
|
| 11 |
+
project_demo
|
| 12 |
+
project_demo/
|
| 13 |
+
logs/
|
| 14 |
+
weights/
|
| 15 |
+
local_folder/
|
| 16 |
+
/demo.py
|
| 17 |
+
demo.py
|
| 18 |
+
videos/
|
| 19 |
+
runs/
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Distribution / packaging
|
| 23 |
+
.Python
|
| 24 |
+
build/
|
| 25 |
+
develop-eggs/
|
| 26 |
+
dist/
|
| 27 |
+
downloads/
|
| 28 |
+
eggs/
|
| 29 |
+
.eggs/
|
| 30 |
+
lib/
|
| 31 |
+
lib64/
|
| 32 |
+
parts/
|
| 33 |
+
sdist/
|
| 34 |
+
var/
|
| 35 |
+
wheels/
|
| 36 |
+
pip-wheel-metadata/
|
| 37 |
+
share/python-wheels/
|
| 38 |
+
*.egg-info/
|
| 39 |
+
.installed.cfg
|
| 40 |
+
*.egg
|
| 41 |
+
MANIFEST
|
| 42 |
+
|
| 43 |
+
# PyInstaller
|
| 44 |
+
# Usually these files are written by a python script from a template
|
| 45 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 46 |
+
*.manifest
|
| 47 |
+
*.spec
|
| 48 |
+
|
| 49 |
+
.vscode
|
| 50 |
+
|
| 51 |
+
# Installer logs
|
| 52 |
+
pip-log.txt
|
| 53 |
+
pip-delete-this-directory.txt
|
| 54 |
+
|
| 55 |
+
# Unit test / coverage reports
|
| 56 |
+
htmlcov/
|
| 57 |
+
.tox/
|
| 58 |
+
.nox/
|
| 59 |
+
.coverage
|
| 60 |
+
.coverage.*
|
| 61 |
+
.cache
|
| 62 |
+
nosetests.xml
|
| 63 |
+
coverage.xml
|
| 64 |
+
*.cover
|
| 65 |
+
*.py,cover
|
| 66 |
+
.hypothesis/
|
| 67 |
+
.pytest_cache/
|
| 68 |
+
|
| 69 |
+
# Translations
|
| 70 |
+
*.mo
|
| 71 |
+
*.pot
|
| 72 |
+
|
| 73 |
+
# Django stuff:
|
| 74 |
+
*.log
|
| 75 |
+
local_settings.py
|
| 76 |
+
db.sqlite3
|
| 77 |
+
db.sqlite3-journal
|
| 78 |
+
|
| 79 |
+
# Flask stuff:
|
| 80 |
+
instance/
|
| 81 |
+
.webassets-cache
|
| 82 |
+
|
| 83 |
+
# Scrapy stuff:
|
| 84 |
+
.scrapy
|
| 85 |
+
|
| 86 |
+
# Sphinx documentation
|
| 87 |
+
docs/_build/
|
| 88 |
+
|
| 89 |
+
# PyBuilder
|
| 90 |
+
target/
|
| 91 |
+
|
| 92 |
+
# Jupyter Notebook
|
| 93 |
+
.ipynb_checkpoints
|
| 94 |
+
|
| 95 |
+
# IPython
|
| 96 |
+
profile_default/
|
| 97 |
+
ipython_config.py
|
| 98 |
+
|
| 99 |
+
# pyenv
|
| 100 |
+
.python-version
|
| 101 |
+
|
| 102 |
+
# pipenv
|
| 103 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 104 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 105 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 106 |
+
# install all needed dependencies.
|
| 107 |
+
#Pipfile.lock
|
| 108 |
+
|
| 109 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
| 110 |
+
__pypackages__/
|
| 111 |
+
|
| 112 |
+
# Celery stuff
|
| 113 |
+
celerybeat-schedule
|
| 114 |
+
celerybeat.pid
|
| 115 |
+
|
| 116 |
+
# SageMath parsed files
|
| 117 |
+
*.sage.py
|
| 118 |
+
|
| 119 |
+
# Environments
|
| 120 |
+
.env
|
| 121 |
+
.venv
|
| 122 |
+
env/
|
| 123 |
+
venv/
|
| 124 |
+
ENV/
|
| 125 |
+
env.bak/
|
| 126 |
+
venv.bak/
|
| 127 |
+
|
| 128 |
+
# Spyder project settings
|
| 129 |
+
.spyderproject
|
| 130 |
+
.spyproject
|
| 131 |
+
|
| 132 |
+
# Rope project settings
|
| 133 |
+
.ropeproject
|
| 134 |
+
|
| 135 |
+
# mkdocs documentation
|
| 136 |
+
/site
|
| 137 |
+
|
| 138 |
+
# mypy
|
| 139 |
+
.mypy_cache/
|
| 140 |
+
.dmypy.json
|
| 141 |
+
dmypy.json
|
| 142 |
+
|
| 143 |
+
# Pyre type checker
|
| 144 |
+
.pyre/
|
algorithm.py
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import cv2
|
| 4 |
+
#import pafy
|
| 5 |
+
import numpy as np
|
| 6 |
+
import settings
|
| 7 |
+
import requests
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class YoloV8Detection:
|
| 12 |
+
|
| 13 |
+
def __init__(self):
|
| 14 |
+
pass
|
| 15 |
+
|
| 16 |
+
def display_tracker_options(self):
|
| 17 |
+
col1, col2 = st.columns(2)
|
| 18 |
+
with col1:
|
| 19 |
+
display_tracker = st.radio("显示追踪", ('是', '否'))
|
| 20 |
+
is_display_tracker = True if display_tracker == '是' else False
|
| 21 |
+
if is_display_tracker:
|
| 22 |
+
with col2:
|
| 23 |
+
tracker_type = st.radio("追踪器选择", ("bytetrack.yaml", "botsort.yaml"))
|
| 24 |
+
return is_display_tracker, tracker_type
|
| 25 |
+
return is_display_tracker, None
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def _display_detected_frames(self,conf, model, st_frame, image, is_display_tracking=None, tracker=None):
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
image = cv2.resize(image, (720, int(720*(9/16))))
|
| 33 |
+
|
| 34 |
+
if is_display_tracking:
|
| 35 |
+
res = model.track(image, conf=conf, persist=True, tracker=tracker)
|
| 36 |
+
else:
|
| 37 |
+
res = model.predict(image, conf=conf)
|
| 38 |
+
|
| 39 |
+
res_plotted = res[0].plot()
|
| 40 |
+
try:
|
| 41 |
+
st_frame.image(res_plotted,
|
| 42 |
+
caption='实时检测',
|
| 43 |
+
channels="BGR",
|
| 44 |
+
use_column_width=True
|
| 45 |
+
)
|
| 46 |
+
except requests.exceptions.RequestException as e:
|
| 47 |
+
st.write("Unable to get image, using placeholder")
|
| 48 |
+
st.image("placeholder.png")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def play_rtsp_stream(self,conf, model):
|
| 52 |
+
|
| 53 |
+
source_rtsp = st.sidebar.text_input("rtsp stream url")
|
| 54 |
+
is_display_tracker, tracker = self.display_tracker_options()
|
| 55 |
+
|
| 56 |
+
start = st.sidebar.button('检测预览')
|
| 57 |
+
stop = st.sidebar.button('停止')
|
| 58 |
+
|
| 59 |
+
if start:
|
| 60 |
+
try:
|
| 61 |
+
vid_cap = cv2.VideoCapture(source_rtsp)
|
| 62 |
+
st_frame = st.empty()
|
| 63 |
+
|
| 64 |
+
while (vid_cap.isOpened()):
|
| 65 |
+
success, image = vid_cap.read()
|
| 66 |
+
if success:
|
| 67 |
+
#print("Success")
|
| 68 |
+
if stop:
|
| 69 |
+
break
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
self._display_detected_frames(conf,
|
| 73 |
+
model,
|
| 74 |
+
st_frame,
|
| 75 |
+
image,
|
| 76 |
+
is_display_tracker,
|
| 77 |
+
tracker
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
else:
|
| 81 |
+
vid_cap.release()
|
| 82 |
+
print("Error")
|
| 83 |
+
break
|
| 84 |
+
except Exception as e:
|
| 85 |
+
st.sidebar.error("Error loading RTSP stream: " + str(e))
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
class BoundaryDetection(YoloV8Detection):
|
| 90 |
+
def __init__(self,intrusion_area= [(100, 200), (400, 200), (500, 300), (100, 300)]):
|
| 91 |
+
self.intrusion_area = intrusion_area
|
| 92 |
+
|
| 93 |
+
def _display_detected_frames(self, conf, model, st_frame, image, is_display_tracking=None, tracker=None):
|
| 94 |
+
|
| 95 |
+
image = cv2.resize(image, (720, int(720*(9/16))))
|
| 96 |
+
|
| 97 |
+
if is_display_tracking:
|
| 98 |
+
res = model.track(image, conf=conf, persist=True, tracker=tracker)
|
| 99 |
+
else:
|
| 100 |
+
res = model.predict(image, conf=conf)
|
| 101 |
+
|
| 102 |
+
cv2.polylines(image, [np.array(self.intrusion_area)], isClosed=True, color=(0, 0, 255), thickness=2)
|
| 103 |
+
intrusion_detected = False
|
| 104 |
+
|
| 105 |
+
for result in res:
|
| 106 |
+
# for xyxy_box in result.boxes.xyxy:
|
| 107 |
+
print(f"boxes是:{result.boxes}\n")
|
| 108 |
+
|
| 109 |
+
box = result.boxes.xyxy
|
| 110 |
+
|
| 111 |
+
print(f"box是:{box}\n")
|
| 112 |
+
if box.size(0) != 0:
|
| 113 |
+
for (xyxy,obj) in zip(box,result.boxes):
|
| 114 |
+
x, y, x2, y2 = int(xyxy[0]), int(xyxy[1]), int(xyxy[2]), int(xyxy[3])
|
| 115 |
+
cx, cy = (x + x2) / 2, (y + y2) / 2
|
| 116 |
+
|
| 117 |
+
print(f"中点坐标是:{cx, cy}\n")
|
| 118 |
+
if cv2.pointPolygonTest(np.array(self.intrusion_area), (cx, cy), False) >= 0:
|
| 119 |
+
# 有人进入特定区域,进行报警
|
| 120 |
+
print("有人入侵!\n")
|
| 121 |
+
intrusion_detected = True
|
| 122 |
+
|
| 123 |
+
# 添加框出入侵者的代码
|
| 124 |
+
cv2.rectangle(image, (x, y), (x2, y2), (0, 255, 0), 2)
|
| 125 |
+
cv2.putText(image, f"{result.names[int(obj.cls[0])]} {obj.conf[0]:.2f}", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
|
| 126 |
+
|
| 127 |
+
else:
|
| 128 |
+
continue
|
| 129 |
+
|
| 130 |
+
if intrusion_detected:
|
| 131 |
+
cv2.putText(image, "Intrusion detected!", (10, 30), cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 0, 255), 2)
|
| 132 |
+
|
| 133 |
+
# res_plotted = res[0].plot()
|
| 134 |
+
# res_plotted=cv2.polylines(res_plotted, [np.array(self.intrusion_area)], isClosed=True, color=(0, 0, 255), thickness=2)
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
try:
|
| 138 |
+
st_frame.image(image,
|
| 139 |
+
caption='实时检测',
|
| 140 |
+
channels="BGR",
|
| 141 |
+
use_column_width=True
|
| 142 |
+
)
|
| 143 |
+
except requests.exceptions.RequestException as e:
|
| 144 |
+
st.write("Unable to get image, using placeholder")
|
| 145 |
+
st.image("placeholder.png")
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
def play_rtsp_stream(self, conf, model):
|
| 149 |
+
source_rtsp = st.sidebar.text_input("rtsp stream url")
|
| 150 |
+
is_display_tracker, tracker = self.display_tracker_options()
|
| 151 |
+
|
| 152 |
+
start = st.sidebar.button('检测预览')
|
| 153 |
+
stop = st.sidebar.button('停止')
|
| 154 |
+
|
| 155 |
+
if start:
|
| 156 |
+
try:
|
| 157 |
+
vid_cap = cv2.VideoCapture(source_rtsp)
|
| 158 |
+
st_frame = st.empty()
|
| 159 |
+
while (vid_cap.isOpened()):
|
| 160 |
+
success, image = vid_cap.read()
|
| 161 |
+
if success:
|
| 162 |
+
if stop:
|
| 163 |
+
break
|
| 164 |
+
self._display_detected_frames(conf,
|
| 165 |
+
model,
|
| 166 |
+
st_frame,
|
| 167 |
+
image,
|
| 168 |
+
is_display_tracker,
|
| 169 |
+
tracker
|
| 170 |
+
)
|
| 171 |
+
else:
|
| 172 |
+
vid_cap.release()
|
| 173 |
+
break
|
| 174 |
+
except Exception as e:
|
| 175 |
+
st.sidebar.error("Error loading RTSP stream: " + str(e))
|
| 176 |
+
|
| 177 |
+
|
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python In-built packages
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
import PIL
|
| 5 |
+
# External packages
|
| 6 |
+
import streamlit as st
|
| 7 |
+
|
| 8 |
+
import algorithm
|
| 9 |
+
import helper
|
| 10 |
+
# Local Modules
|
| 11 |
+
import settings
|
| 12 |
+
|
| 13 |
+
st.set_page_config(
|
| 14 |
+
page_title="YOLOv8 目标检测",
|
| 15 |
+
page_icon="🤖",
|
| 16 |
+
layout="wide",
|
| 17 |
+
initial_sidebar_state="expanded" # 或者 "collapsed"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# # Main page heading
|
| 23 |
+
st.title("目标检测预览")
|
| 24 |
+
|
| 25 |
+
# Sidebar
|
| 26 |
+
st.sidebar.header("模型配置")
|
| 27 |
+
|
| 28 |
+
# Model Options
|
| 29 |
+
model_type = st.sidebar.selectbox(
|
| 30 |
+
"任务选择", ['检测', '分割',"周界入侵","安防检测"])
|
| 31 |
+
|
| 32 |
+
confidence = float(st.sidebar.slider(
|
| 33 |
+
"选择模型Confidence", 25, 100, 40)) / 100
|
| 34 |
+
|
| 35 |
+
# Selecting Detection Or Segmentation
|
| 36 |
+
if model_type == '检测':
|
| 37 |
+
model_path = Path(settings.DETECTION_MODEL)
|
| 38 |
+
elif model_type == '分割':
|
| 39 |
+
model_path = Path(settings.SEGMENTATION_MODEL)
|
| 40 |
+
elif model_type == "周界入侵":
|
| 41 |
+
model_path = Path(settings.DETECTION_MODEL)
|
| 42 |
+
elif model_type == "安防检测":
|
| 43 |
+
model_path = Path(settings.SECURITY_MODEL)
|
| 44 |
+
|
| 45 |
+
# Load Pre-trained ML Model
|
| 46 |
+
try:
|
| 47 |
+
model = helper.load_model(model_path)
|
| 48 |
+
except Exception as ex:
|
| 49 |
+
st.error(f"Unable to load model. Check the specified path: {model_path}")
|
| 50 |
+
st.error(ex)
|
| 51 |
+
|
| 52 |
+
st.sidebar.header("图像/视频 配置")
|
| 53 |
+
source_radio = st.sidebar.radio(
|
| 54 |
+
"选择来源", settings.SOURCES_LIST)
|
| 55 |
+
|
| 56 |
+
source_img = None
|
| 57 |
+
# If image is selected
|
| 58 |
+
if source_radio == settings.IMAGE:
|
| 59 |
+
source_img = st.sidebar.file_uploader(
|
| 60 |
+
"选择一张图像...", type=("jpg", "jpeg", "png", 'bmp', 'webp'))
|
| 61 |
+
|
| 62 |
+
col1, col2 = st.columns(2)
|
| 63 |
+
|
| 64 |
+
with col1:
|
| 65 |
+
try:
|
| 66 |
+
if source_img is None:
|
| 67 |
+
default_image_path = str(settings.DEFAULT_IMAGE)
|
| 68 |
+
default_image = PIL.Image.open(default_image_path)
|
| 69 |
+
st.image(default_image_path, caption="默认图像",
|
| 70 |
+
use_column_width=True)
|
| 71 |
+
else:
|
| 72 |
+
uploaded_image = PIL.Image.open(source_img)
|
| 73 |
+
st.image(source_img, caption="Uploaded Image",
|
| 74 |
+
use_column_width=True)
|
| 75 |
+
except Exception as ex:
|
| 76 |
+
st.error("Error occurred while opening the image.")
|
| 77 |
+
st.error(ex)
|
| 78 |
+
|
| 79 |
+
with col2:
|
| 80 |
+
if source_img is None:
|
| 81 |
+
default_detected_image_path = str(settings.DEFAULT_DETECT_IMAGE)
|
| 82 |
+
default_detected_image = PIL.Image.open(
|
| 83 |
+
default_detected_image_path)
|
| 84 |
+
st.image(default_detected_image_path, caption='检测图像',
|
| 85 |
+
use_column_width=True)
|
| 86 |
+
else:
|
| 87 |
+
if st.sidebar.button('检测目标'):
|
| 88 |
+
res = model.predict(uploaded_image,
|
| 89 |
+
conf=confidence
|
| 90 |
+
)
|
| 91 |
+
boxes = res[0].boxes
|
| 92 |
+
res_plotted = res[0].plot()[:, :, ::-1]
|
| 93 |
+
st.image(res_plotted, caption='Detected Image',
|
| 94 |
+
use_column_width=True)
|
| 95 |
+
try:
|
| 96 |
+
with st.expander("Detection Results"):
|
| 97 |
+
for box in boxes:
|
| 98 |
+
st.write(box.data)
|
| 99 |
+
except Exception as ex:
|
| 100 |
+
# st.write(ex)
|
| 101 |
+
st.write("No image is uploaded yet!")
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
elif source_radio == settings.RTSP:
|
| 106 |
+
if model_type == '检测':
|
| 107 |
+
|
| 108 |
+
algorithm.YoloV8Detection().play_rtsp_stream(confidence,model)
|
| 109 |
+
|
| 110 |
+
elif model_type == '分割':
|
| 111 |
+
|
| 112 |
+
algorithm.YoloV8Detection().play_rtsp_stream(confidence,model)
|
| 113 |
+
|
| 114 |
+
elif model_type == "周界入侵":
|
| 115 |
+
|
| 116 |
+
algorithm.BoundaryDetection().play_rtsp_stream(confidence,model)
|
| 117 |
+
|
| 118 |
+
elif model_type == "安防检测":
|
| 119 |
+
|
| 120 |
+
algorithm.YoloV8Detection().play_rtsp_stream(confidence,model)
|
| 121 |
+
#helper.play_rtsp_stream(confidence, model)
|
| 122 |
+
|
| 123 |
+
else:
|
| 124 |
+
st.error("Please select a valid source type!")
|
| 125 |
+
|
assets/Objdetectionyoutubegif-1.m4v
ADDED
|
Binary file (528 kB). View file
|
|
|
assets/pic1.png
ADDED
|
assets/pic3.png
ADDED
|
assets/segmentation.png
ADDED
|
helper.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import cv2
|
| 4 |
+
#import pafy
|
| 5 |
+
import requests
|
| 6 |
+
import settings
|
| 7 |
+
import numpy as np
|
| 8 |
+
from test_api.algorithm import *
|
| 9 |
+
|
| 10 |
+
def load_model(model_path):
|
| 11 |
+
|
| 12 |
+
model = YOLO(model_path)
|
| 13 |
+
return model
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def display_tracker_options():
|
| 17 |
+
col1, col2 = st.columns(2)
|
| 18 |
+
with col1:
|
| 19 |
+
display_tracker = st.radio("显示追踪", ('Yes', 'No'))
|
| 20 |
+
is_display_tracker = True if display_tracker == 'Yes' else False
|
| 21 |
+
if is_display_tracker:
|
| 22 |
+
with col2:
|
| 23 |
+
tracker_type = st.radio("追踪器", ("bytetrack.yaml", "botsort.yaml"))
|
| 24 |
+
return is_display_tracker, tracker_type
|
| 25 |
+
return is_display_tracker, None
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def _display_detected_frames(conf, model, st_frame, image, is_display_tracking=None, tracker=None):
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# Resize the image to a standard size
|
| 33 |
+
image = cv2.resize(image, (720, int(720*(9/16))))
|
| 34 |
+
|
| 35 |
+
# Display object tracking, if specified
|
| 36 |
+
if is_display_tracking:
|
| 37 |
+
res = model.track(image, conf=conf, persist=True, tracker=tracker)
|
| 38 |
+
else:
|
| 39 |
+
# Predict the objects in the image using the YOLOv8 model
|
| 40 |
+
res = model.predict(image, conf=conf)
|
| 41 |
+
|
| 42 |
+
# # Plot the detected objects on the video frame
|
| 43 |
+
res_plotted = res[0].plot()
|
| 44 |
+
try:
|
| 45 |
+
st_frame.image(res_plotted,
|
| 46 |
+
caption='实时检测',
|
| 47 |
+
channels="BGR",
|
| 48 |
+
use_column_width=True
|
| 49 |
+
)
|
| 50 |
+
except requests.exceptions.RequestException as e:
|
| 51 |
+
st.write("Unable to get image, using placeholder")
|
| 52 |
+
st.image("placeholder.png")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def play_rtsp_stream(conf, model):
|
| 56 |
+
source_rtsp = st.sidebar.text_input("rtsp stream url")
|
| 57 |
+
is_display_tracker, tracker = display_tracker_options()
|
| 58 |
+
if st.sidebar.button('检测目标'):
|
| 59 |
+
try:
|
| 60 |
+
vid_cap = cv2.VideoCapture(source_rtsp)
|
| 61 |
+
st_frame = st.empty()
|
| 62 |
+
while (vid_cap.isOpened()):
|
| 63 |
+
success, image = vid_cap.read()
|
| 64 |
+
if success:
|
| 65 |
+
_display_detected_frames(conf,
|
| 66 |
+
model,
|
| 67 |
+
st_frame,
|
| 68 |
+
image,
|
| 69 |
+
is_display_tracker,
|
| 70 |
+
tracker
|
| 71 |
+
)
|
| 72 |
+
else:
|
| 73 |
+
vid_cap.release()
|
| 74 |
+
break
|
| 75 |
+
except Exception as e:
|
| 76 |
+
st.sidebar.error("Error loading RTSP stream: " + str(e))
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
images/office_4.jpg
ADDED
|
images/office_4_detected.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
opencv-python
|
| 3 |
+
numpy
|
| 4 |
+
requests
|
settings.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
# 获取当前文件的绝对路径
|
| 5 |
+
file_path = Path(__file__).resolve()
|
| 6 |
+
print("file_path",file_path)
|
| 7 |
+
# 获取当前文件的父路径
|
| 8 |
+
root_path = file_path.parent
|
| 9 |
+
print("root_path",root_path)
|
| 10 |
+
# 获取总项目的根路径
|
| 11 |
+
model_root_path = root_path.parent
|
| 12 |
+
print("model_root_path",model_root_path)
|
| 13 |
+
|
| 14 |
+
# Add the root path to the sys.path list if it is not already there
|
| 15 |
+
if root_path not in sys.path:
|
| 16 |
+
sys.path.append(str(root_path))
|
| 17 |
+
|
| 18 |
+
# 获取settings文件对于当前工作目录的相对路径
|
| 19 |
+
ROOT = root_path.relative_to(Path.cwd())
|
| 20 |
+
print("Path.cwd()",Path.cwd())
|
| 21 |
+
print("ROOT",ROOT)# .
|
| 22 |
+
|
| 23 |
+
# Sources
|
| 24 |
+
IMAGE = 'Image'
|
| 25 |
+
|
| 26 |
+
RTSP = 'RTSP'
|
| 27 |
+
|
| 28 |
+
SOURCES_LIST = [IMAGE, RTSP]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Images config
|
| 32 |
+
IMAGES_DIR = ROOT / 'images'
|
| 33 |
+
DEFAULT_IMAGE = IMAGES_DIR / 'office_4.jpg'
|
| 34 |
+
DEFAULT_DETECT_IMAGE = IMAGES_DIR / 'office_4_detected.jpg'
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# ML Model config
|
| 39 |
+
MODEL_DIR = ROOT / 'weights'
|
| 40 |
+
DETECTION_MODEL = MODEL_DIR / 'yolov8n.pt'
|
| 41 |
+
SEGMENTATION_MODEL = MODEL_DIR / 'yolov8n-seg.pt'
|
| 42 |
+
SECURITY_MODEL = MODEL_DIR / 'yolov8n-sec.pt'
|
weights/yolov8n-sec.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5b5b866206e52d9b8a20666da29623a135c9a7df660319f18354506ee5cd2a4f
|
| 3 |
+
size 6247961
|
weights/yolov8n-seg.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d39e867b2c3a5dbc1aa764411544b475cb14727bf6af1ec46c238f8bb1351ab9
|
| 3 |
+
size 7054355
|
weights/yolov8n.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:31e20dde3def09e2cf938c7be6fe23d9150bbbe503982af13345706515f2ef95
|
| 3 |
+
size 6534387
|