Skip to content

Commit de80e09

Browse files
authored
Merge pull request #58 from NUTFes/feat/nose/sahi-yolov8
sahiを用いて物体検出を行う
2 parents 93a2e36 + c0538ca commit de80e09

11 files changed

Lines changed: 281 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ __pycache__/
55
*.env
66
*.yaml
77
.DS_store
8+
/sahi
9+
.env
10+
raspi/result/prediction_visual.png
1.08 MB
Loading
316 KB
Loading

raspi/demo_data/terrain2.png

426 KB
Loading

raspi/detect.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 入力:カメラで得た画像
2+
# 出力:検出台数
3+
4+
from sahi.predict import get_sliced_prediction
5+
6+
def detect(model,data,count_class):
7+
count = 0
8+
9+
# 入力データから検出を行う
10+
print("detect start...")
11+
result = get_sliced_prediction(
12+
data,
13+
model,
14+
slice_height=256,
15+
slice_width=256,
16+
overlap_height_ratio=0.2,
17+
overlap_width_ratio=0.2,
18+
)
19+
20+
print("detect finish")
21+
22+
# 検出した結果を画像にする
23+
result.export_visuals(export_dir="result/")
24+
25+
# 検出結果を辞書型のリストに変換
26+
detect_list = result.to_coco_predictions()
27+
28+
# 指定のカテゴリをカウント
29+
for detect_object in detect_list:
30+
if detect_object["category_id"] == count_class:
31+
count += 1
32+
33+
print(f'{count_class} is the number of {count}')
34+
35+
return count
36+
37+
38+

raspi/main.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# yoloで物体認識したものを、日付と車数に変換して送信する
2+
3+
import cv2
4+
from sahi import AutoDetectionModel
5+
from sahi.utils.yolov8 import download_yolov8s_model
6+
from send import send_mongo,connect_mongo
7+
from detect import detect
8+
import os
9+
from dotenv import load_dotenv
10+
11+
load_dotenv()
12+
USER = os.environ["USER_NAME"]
13+
PASS = os.environ["PASS"]
14+
HOST = os.environ["HOST"]
15+
PORT = os.environ["PORT"]
16+
DB_NAME = os.environ["DB_NAME"]
17+
COLLECTION_NAME = os.environ["COLLECTION_NAME"]
18+
19+
# 人なら0、車なら2
20+
count_class = 0
21+
22+
def main():
23+
# モデルをダウンロード
24+
yolov8_model_path = "models/yolov8s.pt"
25+
download_yolov8s_model(yolov8_model_path)
26+
27+
# mongoDBと接続
28+
collection,_ = connect_mongo(USER,PASS,HOST,PORT,DB_NAME,COLLECTION_NAME)
29+
30+
31+
# 画像パスまたはnumpy画像を用いて標準的な推論を行う。
32+
# YOLOv8 、このようにオブジェクト検出用のモデルをインスタンス化することができる:
33+
detection_model = AutoDetectionModel.from_pretrained(
34+
model_type="yolov8",
35+
model_path=yolov8_model_path,
36+
confidence_threshold=0.3,
37+
device="cpu", # or 'cuda:0'
38+
)
39+
40+
# カメラデバイスを開く
41+
cap = cv2.VideoCapture(0)
42+
43+
while True:
44+
ret, frame = cap.read()
45+
print("isCap",ret)
46+
if not ret:
47+
break
48+
49+
# detect.pyでカウントした数を取り出す
50+
count = detect(detection_model,frame,count_class)
51+
52+
# データを送信する
53+
send_mongo(count,collection)
54+
55+
56+
if __name__ == "__main__":
57+
main()

raspi/models/yolov8s.pt

21.5 MB
Binary file not shown.

raspi/requirements.txt

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
aiofiles==23.2.1
2+
anyio==4.3.0
3+
argcomplete==3.3.0
4+
asttokens==2.4.1
5+
attrs==23.2.0
6+
beautifulsoup4==4.12.3
7+
boto3==1.34.102
8+
botocore==1.34.102
9+
Brotli==1.1.0
10+
cachetools==5.3.3
11+
certifi==2024.2.2
12+
charset-normalizer==3.3.2
13+
click==8.1.7
14+
contourpy==1.2.1
15+
cycler==0.12.1
16+
dacite==1.7.0
17+
decorator==5.1.1
18+
Deprecated==1.2.14
19+
dill==0.3.8
20+
dnspython==2.6.1
21+
exceptiongroup==1.2.1
22+
executing==2.0.1
23+
fiftyone==0.23.8
24+
fiftyone-brain==0.16.1
25+
fiftyone_db==1.1.2
26+
filelock==3.14.0
27+
fire==0.6.0
28+
fonttools==4.51.0
29+
fsspec==2024.3.1
30+
ftfy==6.2.0
31+
future==1.0.0
32+
glob2==0.7
33+
graphql-core==3.2.3
34+
h11==0.14.0
35+
h2==4.1.0
36+
hpack==4.0.0
37+
httpcore==1.0.5
38+
httpx==0.27.0
39+
humanize==4.9.0
40+
Hypercorn==0.16.0
41+
hyperframe==6.0.1
42+
idna==3.7
43+
imageio==2.34.1
44+
imantics==0.1.12
45+
inflate64==1.0.0
46+
ipython==8.24.0
47+
jedi==0.19.1
48+
Jinja2==3.1.4
49+
jmespath==1.0.1
50+
joblib==1.4.2
51+
jsonlines==4.0.0
52+
kaleido==0.2.1
53+
kiwisolver==1.4.5
54+
lazy_loader==0.4
55+
lxml==5.2.1
56+
MarkupSafe==2.1.5
57+
matplotlib==3.8.4
58+
matplotlib-inline==0.1.7
59+
mongoengine==0.24.2
60+
motor==3.4.0
61+
mpmath==1.3.0
62+
multivolumefile==0.2.3
63+
networkx==3.3
64+
numpy==1.26.4
65+
opencv-python==4.7.0.72
66+
opencv-python-headless==4.9.0.80
67+
packaging==24.0
68+
pandas==2.2.2
69+
parso==0.8.4
70+
pexpect==4.9.0
71+
pillow==10.3.0
72+
plotly==5.22.0
73+
pprintpp==0.4.0
74+
priority==2.0.0
75+
prompt-toolkit==3.0.43
76+
psutil==5.9.8
77+
ptyprocess==0.7.0
78+
pure-eval==0.2.2
79+
py-cpuinfo==9.0.0
80+
py7zr==0.21.0
81+
pybboxes==0.1.6
82+
pybcj==1.0.2
83+
pycryptodomex==3.20.0
84+
Pygments==2.18.0
85+
pymongo==4.7.2
86+
pyparsing==3.1.2
87+
pyppmd==1.1.0
88+
python-dateutil==2.9.0.post0
89+
python-dotenv==1.0.1
90+
pytz==2024.1
91+
PyYAML==6.0.1
92+
pyzstd==0.15.10
93+
rarfile==4.2
94+
regex==2024.5.10
95+
requests==2.31.0
96+
retrying==1.3.4
97+
s3transfer==0.10.1
98+
sahi==0.11.15
99+
scikit-image==0.23.2
100+
scikit-learn==1.4.2
101+
scipy==1.13.0
102+
seaborn==0.13.2
103+
shapely==2.0.4
104+
six==1.16.0
105+
sniffio==1.3.1
106+
sortedcontainers==2.4.0
107+
soupsieve==2.5
108+
sse-starlette==0.10.3
109+
sseclient-py==1.8.0
110+
stack-data==0.6.3
111+
starlette==0.37.2
112+
strawberry-graphql==0.138.1
113+
sympy==1.12
114+
tabulate==0.9.0
115+
taskgroup==0.0.0a4
116+
tenacity==8.3.0
117+
termcolor==2.4.0
118+
terminaltables==3.1.10
119+
texttable==1.7.0
120+
thop==0.1.1.post2209072238
121+
threadpoolctl==3.5.0
122+
tifffile==2024.5.10
123+
tomli==2.0.1
124+
torch==2.3.0
125+
torchvision==0.18.0
126+
tqdm==4.66.4
127+
traitlets==5.14.3
128+
typing_extensions==4.11.0
129+
tzdata==2024.1
130+
tzlocal==5.2
131+
ultralytics==8.2.11
132+
universal-analytics-python3==1.1.1
133+
urllib3==2.2.1
134+
voxel51-eta==0.12.6
135+
wcwidth==0.2.13
136+
wrapt==1.16.0
137+
wsproto==1.2.0
138+
xmljson==0.2.1
139+
xmltodict==0.13.0
1.07 MB
Loading
1.07 MB
Loading

0 commit comments

Comments
 (0)