diff --git a/.gitignore b/.gitignore index 0036458..33cc9c4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ __pycache__/ *.env *.yaml .DS_store +/sahi +.env +raspi/result/prediction_visual.png \ No newline at end of file diff --git a/raspi/demo_data/prediction_visual.png b/raspi/demo_data/prediction_visual.png new file mode 100644 index 0000000..15ca3de Binary files /dev/null and b/raspi/demo_data/prediction_visual.png differ diff --git a/raspi/demo_data/small-vehicles1.jpeg b/raspi/demo_data/small-vehicles1.jpeg new file mode 100644 index 0000000..5e258c9 Binary files /dev/null and b/raspi/demo_data/small-vehicles1.jpeg differ diff --git a/raspi/demo_data/terrain2.png b/raspi/demo_data/terrain2.png new file mode 100644 index 0000000..4469c41 Binary files /dev/null and b/raspi/demo_data/terrain2.png differ diff --git a/raspi/detect.py b/raspi/detect.py new file mode 100644 index 0000000..998e616 --- /dev/null +++ b/raspi/detect.py @@ -0,0 +1,38 @@ +# 入力:カメラで得た画像 +# 出力:検出台数 + +from sahi.predict import get_sliced_prediction + +def detect(model,data,count_class): + count = 0 + + # 入力データから検出を行う + print("detect start...") + result = get_sliced_prediction( + data, + model, + slice_height=256, + slice_width=256, + overlap_height_ratio=0.2, + overlap_width_ratio=0.2, + ) + + print("detect finish") + + # 検出した結果を画像にする + result.export_visuals(export_dir="result/") + + # 検出結果を辞書型のリストに変換 + detect_list = result.to_coco_predictions() + + # 指定のカテゴリをカウント + for detect_object in detect_list: + if detect_object["category_id"] == count_class: + count += 1 + + print(f'{count_class} is the number of {count}') + + return count + + + diff --git a/raspi/main.py b/raspi/main.py new file mode 100644 index 0000000..7ca724d --- /dev/null +++ b/raspi/main.py @@ -0,0 +1,57 @@ +# yoloで物体認識したものを、日付と車数に変換して送信する + +import cv2 +from sahi import AutoDetectionModel +from sahi.utils.yolov8 import download_yolov8s_model +from send import send_mongo,connect_mongo +from detect import detect +import os +from dotenv import load_dotenv + +load_dotenv() +USER = os.environ["USER_NAME"] +PASS = os.environ["PASS"] +HOST = os.environ["HOST"] +PORT = os.environ["PORT"] +DB_NAME = os.environ["DB_NAME"] +COLLECTION_NAME = os.environ["COLLECTION_NAME"] + +# 人なら0、車なら2 +count_class = 0 + +def main(): + # モデルをダウンロード + yolov8_model_path = "models/yolov8s.pt" + download_yolov8s_model(yolov8_model_path) + + # mongoDBと接続 + collection,_ = connect_mongo(USER,PASS,HOST,PORT,DB_NAME,COLLECTION_NAME) + + + # 画像パスまたはnumpy画像を用いて標準的な推論を行う。 + # YOLOv8 、このようにオブジェクト検出用のモデルをインスタンス化することができる: + detection_model = AutoDetectionModel.from_pretrained( + model_type="yolov8", + model_path=yolov8_model_path, + confidence_threshold=0.3, + device="cpu", # or 'cuda:0' + ) + + # カメラデバイスを開く + cap = cv2.VideoCapture(0) + + while True: + ret, frame = cap.read() + print("isCap",ret) + if not ret: + break + + # detect.pyでカウントした数を取り出す + count = detect(detection_model,frame,count_class) + + # データを送信する + send_mongo(count,collection) + + +if __name__ == "__main__": + main() diff --git a/raspi/models/yolov8s.pt b/raspi/models/yolov8s.pt new file mode 100644 index 0000000..5f7efb1 Binary files /dev/null and b/raspi/models/yolov8s.pt differ diff --git a/raspi/requirements.txt b/raspi/requirements.txt new file mode 100644 index 0000000..f59b3bf --- /dev/null +++ b/raspi/requirements.txt @@ -0,0 +1,139 @@ +aiofiles==23.2.1 +anyio==4.3.0 +argcomplete==3.3.0 +asttokens==2.4.1 +attrs==23.2.0 +beautifulsoup4==4.12.3 +boto3==1.34.102 +botocore==1.34.102 +Brotli==1.1.0 +cachetools==5.3.3 +certifi==2024.2.2 +charset-normalizer==3.3.2 +click==8.1.7 +contourpy==1.2.1 +cycler==0.12.1 +dacite==1.7.0 +decorator==5.1.1 +Deprecated==1.2.14 +dill==0.3.8 +dnspython==2.6.1 +exceptiongroup==1.2.1 +executing==2.0.1 +fiftyone==0.23.8 +fiftyone-brain==0.16.1 +fiftyone_db==1.1.2 +filelock==3.14.0 +fire==0.6.0 +fonttools==4.51.0 +fsspec==2024.3.1 +ftfy==6.2.0 +future==1.0.0 +glob2==0.7 +graphql-core==3.2.3 +h11==0.14.0 +h2==4.1.0 +hpack==4.0.0 +httpcore==1.0.5 +httpx==0.27.0 +humanize==4.9.0 +Hypercorn==0.16.0 +hyperframe==6.0.1 +idna==3.7 +imageio==2.34.1 +imantics==0.1.12 +inflate64==1.0.0 +ipython==8.24.0 +jedi==0.19.1 +Jinja2==3.1.4 +jmespath==1.0.1 +joblib==1.4.2 +jsonlines==4.0.0 +kaleido==0.2.1 +kiwisolver==1.4.5 +lazy_loader==0.4 +lxml==5.2.1 +MarkupSafe==2.1.5 +matplotlib==3.8.4 +matplotlib-inline==0.1.7 +mongoengine==0.24.2 +motor==3.4.0 +mpmath==1.3.0 +multivolumefile==0.2.3 +networkx==3.3 +numpy==1.26.4 +opencv-python==4.7.0.72 +opencv-python-headless==4.9.0.80 +packaging==24.0 +pandas==2.2.2 +parso==0.8.4 +pexpect==4.9.0 +pillow==10.3.0 +plotly==5.22.0 +pprintpp==0.4.0 +priority==2.0.0 +prompt-toolkit==3.0.43 +psutil==5.9.8 +ptyprocess==0.7.0 +pure-eval==0.2.2 +py-cpuinfo==9.0.0 +py7zr==0.21.0 +pybboxes==0.1.6 +pybcj==1.0.2 +pycryptodomex==3.20.0 +Pygments==2.18.0 +pymongo==4.7.2 +pyparsing==3.1.2 +pyppmd==1.1.0 +python-dateutil==2.9.0.post0 +python-dotenv==1.0.1 +pytz==2024.1 +PyYAML==6.0.1 +pyzstd==0.15.10 +rarfile==4.2 +regex==2024.5.10 +requests==2.31.0 +retrying==1.3.4 +s3transfer==0.10.1 +sahi==0.11.15 +scikit-image==0.23.2 +scikit-learn==1.4.2 +scipy==1.13.0 +seaborn==0.13.2 +shapely==2.0.4 +six==1.16.0 +sniffio==1.3.1 +sortedcontainers==2.4.0 +soupsieve==2.5 +sse-starlette==0.10.3 +sseclient-py==1.8.0 +stack-data==0.6.3 +starlette==0.37.2 +strawberry-graphql==0.138.1 +sympy==1.12 +tabulate==0.9.0 +taskgroup==0.0.0a4 +tenacity==8.3.0 +termcolor==2.4.0 +terminaltables==3.1.10 +texttable==1.7.0 +thop==0.1.1.post2209072238 +threadpoolctl==3.5.0 +tifffile==2024.5.10 +tomli==2.0.1 +torch==2.3.0 +torchvision==0.18.0 +tqdm==4.66.4 +traitlets==5.14.3 +typing_extensions==4.11.0 +tzdata==2024.1 +tzlocal==5.2 +ultralytics==8.2.11 +universal-analytics-python3==1.1.1 +urllib3==2.2.1 +voxel51-eta==0.12.6 +wcwidth==0.2.13 +wrapt==1.16.0 +wsproto==1.2.0 +xmljson==0.2.1 +xmltodict==0.13.0 diff --git a/raspi/runs/predict/exp5/visuals/small-vehicles1.png b/raspi/runs/predict/exp5/visuals/small-vehicles1.png new file mode 100644 index 0000000..28da1ed Binary files /dev/null and b/raspi/runs/predict/exp5/visuals/small-vehicles1.png differ diff --git a/raspi/runs/predict/exp6/visuals/small-vehicles1.png b/raspi/runs/predict/exp6/visuals/small-vehicles1.png new file mode 100644 index 0000000..28da1ed Binary files /dev/null and b/raspi/runs/predict/exp6/visuals/small-vehicles1.png differ diff --git a/raspi/send.py b/raspi/send.py new file mode 100644 index 0000000..73a4a73 --- /dev/null +++ b/raspi/send.py @@ -0,0 +1,44 @@ +# 検出数と時刻を送信 +from pymongo import MongoClient +import time +import os +from dotenv import load_dotenv + +def connect_mongo(user,password,host,port,db_name,collection_name): + client = MongoClient(f"mongodb://{user}:{password}@{host}:{port}") + db = client[db_name] + collection = db[collection_name] + return collection,client + +def get_time(): + ut = time.time() * 1000 + return ut + +def make_doc(num,time): + num_int = f"{num}" + document = {"count": int(num_int), "time": time } + return document + +def send_mongo(num,mongo_col): + print("send mongo") + ut = get_time() + doc = make_doc(num,ut) + mongo_col.insert_one(doc) + print("send finish") + #mongo_cl.close() + return + +# print(get_time()) + +if __name__ == "__main__": + load_dotenv() + USER = os.environ["USER_NAME"] + PASS = os.environ["PASS"] + HOST = os.environ["HOST"] + PORT = os.environ["PORT"] + DB_NAME = os.environ["DB_NAME"] + COLLECTION_NAME = os.environ["COLLECTION_NAME"] + collection,client = connect_mongo(USER,PASS,HOST,PORT,DB_NAME,COLLECTION_NAME) + send_mongo("20") + print(collection) +