diff --git a/poetry.lock b/poetry.lock index edad7bf..dd09901 100644 --- a/poetry.lock +++ b/poetry.lock @@ -375,18 +375,18 @@ test = ["pytest (>=6)"] [[package]] name = "filelock" -version = "3.16.0" +version = "3.16.1" description = "A platform independent file lock." optional = false python-versions = ">=3.8" files = [ - {file = "filelock-3.16.0-py3-none-any.whl", hash = "sha256:f6ed4c963184f4c84dd5557ce8fece759a3724b37b80c6c4f20a2f63a4dc6609"}, - {file = "filelock-3.16.0.tar.gz", hash = "sha256:81de9eb8453c769b63369f87f11131a7ab04e367f8d97ad39dc230daa07e3bec"}, + {file = "filelock-3.16.1-py3-none-any.whl", hash = "sha256:2082e5703d51fbf98ea75855d9d5527e33d8ff23099bec374a134febee6946b0"}, + {file = "filelock-3.16.1.tar.gz", hash = "sha256:c249fbfcd5db47e5e2d6d62198e565475ee65e4831e2561c8e313fa7eb961435"}, ] [package.extras] -docs = ["furo (>=2024.8.6)", "sphinx (>=8.0.2)", "sphinx-autodoc-typehints (>=2.4)"] -testing = ["covdefaults (>=2.3)", "coverage (>=7.6.1)", "diff-cover (>=9.1.1)", "pytest (>=8.3.2)", "pytest-asyncio (>=0.24)", "pytest-cov (>=5)", "pytest-mock (>=3.14)", "pytest-timeout (>=2.3.1)", "virtualenv (>=20.26.3)"] +docs = ["furo (>=2024.8.6)", "sphinx (>=8.0.2)", "sphinx-autodoc-typehints (>=2.4.1)"] +testing = ["covdefaults (>=2.3)", "coverage (>=7.6.1)", "diff-cover (>=9.2)", "pytest (>=8.3.3)", "pytest-asyncio (>=0.24)", "pytest-cov (>=5)", "pytest-mock (>=3.14)", "pytest-timeout (>=2.3.1)", "virtualenv (>=20.26.4)"] typing = ["typing-extensions (>=4.12.2)"] [[package]] @@ -531,15 +531,18 @@ tqdm = ["tqdm"] [[package]] name = "idna" -version = "3.8" +version = "3.10" description = "Internationalized Domain Names in Applications (IDNA)" optional = false python-versions = ">=3.6" files = [ - {file = "idna-3.8-py3-none-any.whl", hash = "sha256:050b4e5baadcd44d760cedbd2b8e639f2ff89bbc7a5730fcc662954303377aac"}, - {file = "idna-3.8.tar.gz", hash = "sha256:d838c2c0ed6fced7693d5e8ab8e734d5f8fda53a039c0164afb0b82e771e3603"}, + {file = "idna-3.10-py3-none-any.whl", hash = "sha256:946d195a0d259cbba61165e88e65941f16e9b36ea6ddb97f00452bae8b1287d3"}, + {file = "idna-3.10.tar.gz", hash = "sha256:12f65c9b470abda6dc35cf8e63cc574b1c52b11df2c86030af0ac09b01b13ea9"}, ] +[package.extras] +all = ["flake8 (>=7.1.1)", "mypy (>=1.11.2)", "pytest (>=8.3.2)", "ruff (>=0.6.2)"] + [[package]] name = "iniconfig" version = "2.0.0" @@ -1415,13 +1418,13 @@ six = ">=1.5" [[package]] name = "pytz" -version = "2024.1" +version = "2024.2" description = "World timezone definitions, modern and historical" optional = false python-versions = "*" files = [ - {file = "pytz-2024.1-py2.py3-none-any.whl", hash = "sha256:328171f4e3623139da4983451950b28e95ac706e13f3f2630a879749e7a8b319"}, - {file = "pytz-2024.1.tar.gz", hash = "sha256:2a29735ea9c18baf14b448846bde5a48030ed267578472d8955cd0e7443a9812"}, + {file = "pytz-2024.2-py2.py3-none-any.whl", hash = "sha256:31c7c1817eb7fae7ca4b8c7ee50c72f93aa2dd863de768e1ef4245d426aa0725"}, + {file = "pytz-2024.2.tar.gz", hash = "sha256:2aa355083c50a0f93fa581709deac0c9ad65cca8a9e9beac660adcbd493c798a"}, ] [[package]] @@ -1580,18 +1583,18 @@ stats = ["scipy (>=1.7)", "statsmodels (>=0.12)"] [[package]] name = "setuptools" -version = "74.1.2" +version = "75.1.0" description = "Easily download, build, install, upgrade, and uninstall Python packages" optional = false python-versions = ">=3.8" files = [ - {file = "setuptools-74.1.2-py3-none-any.whl", hash = "sha256:5f4c08aa4d3ebcb57a50c33b1b07e94315d7fc7230f7115e47fc99776c8ce308"}, - {file = "setuptools-74.1.2.tar.gz", hash = "sha256:95b40ed940a1c67eb70fc099094bd6e99c6ee7c23aa2306f4d2697ba7916f9c6"}, + {file = "setuptools-75.1.0-py3-none-any.whl", hash = "sha256:35ab7fd3bcd95e6b7fd704e4a1539513edad446c097797f2985e0e4b960772f2"}, + {file = "setuptools-75.1.0.tar.gz", hash = "sha256:d59a21b17a275fb872a9c3dae73963160ae079f1049ed956880cd7c09b120538"}, ] [package.extras] check = ["pytest-checkdocs (>=2.4)", "pytest-ruff (>=0.2.1)", "ruff (>=0.5.2)"] -core = ["importlib-metadata (>=6)", "importlib-resources (>=5.10.2)", "jaraco.text (>=3.7)", "more-itertools (>=8.8)", "packaging (>=24)", "platformdirs (>=2.6.2)", "tomli (>=2.0.1)", "wheel (>=0.43.0)"] +core = ["importlib-metadata (>=6)", "importlib-resources (>=5.10.2)", "jaraco.collections", "jaraco.functools", "jaraco.text (>=3.7)", "more-itertools", "more-itertools (>=8.8)", "packaging", "packaging (>=24)", "platformdirs (>=2.6.2)", "tomli (>=2.0.1)", "wheel (>=0.43.0)"] cover = ["pytest-cov"] doc = ["furo", "jaraco.packaging (>=9.3)", "jaraco.tidelift (>=1.4)", "pygments-github-lexers (==0.0.5)", "pyproject-hooks (!=1.1)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-favicon", "sphinx-inline-tabs", "sphinx-lint", "sphinx-notfound-page (>=1,<2)", "sphinx-reredirects", "sphinxcontrib-towncrier", "towncrier (<24.7)"] enabler = ["pytest-enabler (>=2.2)"] @@ -1838,13 +1841,13 @@ files = [ [[package]] name = "ultralytics" -version = "8.2.90" -description = "Ultralytics YOLOv8 for SOTA object detection, multi-object tracking, instance segmentation, pose estimation and image classification." +version = "8.2.95" +description = "Ultralytics YOLO for SOTA object detection, multi-object tracking, instance segmentation, pose estimation and image classification." optional = false python-versions = ">=3.8" files = [ - {file = "ultralytics-8.2.90-py3-none-any.whl", hash = "sha256:6333508d233d4bbeda8f0e0ac12e0b85a888fa06cc4b59121d28e839598b5499"}, - {file = "ultralytics-8.2.90.tar.gz", hash = "sha256:43c8228f13806a2274e7161f5da1285cd2b83a41765f9afbdeec05065fb3cb7f"}, + {file = "ultralytics-8.2.95-py3-none-any.whl", hash = "sha256:035adc3610097365c98c7e78ebadc6a5694d6c23ed51765157246677b80372d2"}, + {file = "ultralytics-8.2.95.tar.gz", hash = "sha256:9fb0139808c0acf928803213f57a3b757ea15401ee3ee34cacea052d75631c97"}, ] [package.dependencies] @@ -1891,13 +1894,13 @@ torch = "*" [[package]] name = "urllib3" -version = "2.2.2" +version = "2.2.3" description = "HTTP library with thread-safe connection pooling, file post, and more." optional = false python-versions = ">=3.8" files = [ - {file = "urllib3-2.2.2-py3-none-any.whl", hash = "sha256:a448b2f64d686155468037e1ace9f2d2199776e17f0a46610480d311f73e3472"}, - {file = "urllib3-2.2.2.tar.gz", hash = "sha256:dd505485549a7a552833da5e6063639d0d177c04f23bc3864e41e5dc5f612168"}, + {file = "urllib3-2.2.3-py3-none-any.whl", hash = "sha256:ca899ca043dcb1bafa3e262d73aa25c465bfb49e0bd9dd5d59f1d0acba2f8fac"}, + {file = "urllib3-2.2.3.tar.gz", hash = "sha256:e7d814a81dad81e6caf2ec9fdedb284ecc9c73076b62654547cc64ccdcae26e9"}, ] [package.extras] diff --git a/scripts/kill_official_server.sh b/scripts/kill_official_server.sh index b569d30..5b380d0 100644 --- a/scripts/kill_official_server.sh +++ b/scripts/kill_official_server.sh @@ -6,4 +6,4 @@ # - テスト用の競技システムとLANケーブルで接続している KILL_SERVER_COMMAND='ps aux | grep node | grep -v "grep" | awk '\''{print $2}'\'' | sudo xargs -r kill -9' -ssh et2024@official-system "$KILL_SERVER_COMMAND" +ssh taki@192.168.11.26 "$KILL_SERVER_COMMAND" diff --git a/scripts/run_official_server.sh b/scripts/run_official_server.sh index 963bcde..61ae070 100644 --- a/scripts/run_official_server.sh +++ b/scripts/run_official_server.sh @@ -6,4 +6,4 @@ # - テスト用の競技システムとLANケーブルで接続している RUN_SERVER_COMMAND="cd /opt/compesys && sudo npm run start" -ssh et2024@official-system "${RUN_SERVER_COMMAND}" +ssh taki@192.168.11.26 "${RUN_SERVER_COMMAND}" diff --git a/src/image_processor.py b/src/image_processor.py new file mode 100644 index 0000000..f9afe8a --- /dev/null +++ b/src/image_processor.py @@ -0,0 +1,75 @@ +""" +画像処理に関するクラス. + +@author bizyutyu +""" + +import cv2 +import numpy as np +import os + + +class ImageProcessor: + """画像の先鋭化のためのクラス.""" + + @staticmethod + def sharpen_image(image_path: str) -> str: + """画像の先鋭化処理を行うメソッド. + + 手法:カラー画像のアンシャープマスクを用いる + + Args: + image_path(str): 先鋭化対象の画像パス + Return: + sharpened_image_path: 先鋭化後画像パス + + Raises: + FileNotFoundError: 画像が見つからない場合に発生 + """ + try: + # 読み込み + img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED) + + if img is None: + raise FileNotFoundError(f"'{image_path}' is not found") + + # アンシャープマスクを適用する + blurred = cv2.GaussianBlur(img, (0, 0), 2) # ぼかし処理 + + # 引数: 元画像, 元の画像に対する加重係数(強度) + # ブラー画像, ブラー画像に対する減重係数(強度), 画像の明るさ(0は無視) + result = cv2.addWeighted(img, 2.5, blurred, -1.5, 0) # 差分から鮮明化 + + # 出力パスの生成 + dir_path = os.path.dirname(image_path) + file_name = os.path.basename(image_path) + sharpened_image_path = os.path.join(dir_path, + f"Sharpened_{file_name}") + + # 先鋭化画像保存処理 + os.makedirs(os.path.dirname(sharpened_image_path), exist_ok=True) + cv2.imwrite(sharpened_image_path, result) + + return sharpened_image_path + + except FileNotFoundError as e: + print("Error:", e) + return None + +# if __name__ == '__main__': + +# import argparse + +# parser = argparse.ArgumentParser(description="画像処理に関するプログラム") + +# parser.add_argument("-ipath", "--image_path", type=str, +# default=IMAGE_DIR_PATH/'test_image.jpeg', help='入力画像') + +# args = parser.parse_args() + +# sharpened_image = ImageProcessor.sharpen_image(args.input_path) + +# if sharpened_image: +# print(f"先鋭化完了。結果は {sharpened_image_path} に保存しています。") +# else: +# print("先鋭化失敗。") diff --git a/src/server/flask_server.py b/src/server/flask_server.py index a3a0a05..cba25b0 100644 --- a/src/server/flask_server.py +++ b/src/server/flask_server.py @@ -11,6 +11,7 @@ from ..csv_to_json import CSVToJSONConverter from ..official_interface import OfficialInterface from ..detect_object import DetectObject +from ..image_processor import ImageProcessor from flask import Flask, request, jsonify, send_file @@ -62,11 +63,18 @@ def get_image() -> jsonify: file_path = os.path.join(upload_folder, file_name) file.save(file_path) - # TODO: 現在は、1枚目のフィグ画像、プラレール画像の場合に競技システムへアップロードしている - if file_name == 'Fig_1.jpeg' or file_name == 'Pla.jpeg': - OfficialInterface.upload_snap(file_path) + # 画像の先鋭化処理を行う + sharpened_file_path = ImageProcessor.sharpen_image(file_path) - return jsonify({"message": "File uploaded successfully"}), 200 + if sharpened_file_path: + # 先鋭化した画像ファイルを競技システムに送信する + OfficialInterface.upload_snap(sharpened_file_path) + return jsonify({"message": + "Sharpened File uploaded successfully"}), 200 + else: + # 受け取った画像ファイルを競技システムに送信する + OfficialInterface.upload_snap(file_path) + return jsonify({"message": "File uploaded successfully"}), 200 # '/detect'へのPOSTリクエストに対する操作 @@ -112,6 +120,18 @@ def get_detection_image() -> jsonify: with open(empty_file, 'w') as file: pass + # 判定したふぃぐの向きがが正面だった場合、競技システムに送信する + if cls == 0: + # 画像の先鋭化処理を行う + sharpened_file_path = ImageProcessor.sharpen_image(file_path) + + if sharpened_file_path: + # 先鋭化したふぃぐ画像を競技システムに送信する + OfficialInterface.upload_snap(sharpened_file_path) + else: + # ふぃぐ画像をそのまま競技システムに送信する + OfficialInterface.upload_snap(file_path) + return send_file(empty_file, as_attachment=True, download_name=empty_file, @@ -119,8 +139,19 @@ def get_detection_image() -> jsonify: except Exception: print("Error: detect failed") objects = [] - return jsonify({"message": "File uploaded successfully", - "detect_results": "detect failed"}), 200 + cls = -1 + empty_file = os.path.abspath(f"{cls}_skip_camera_action.flag") + + # 空のフラグ管理用ファイルを作成 + with open(empty_file, 'w') as file: + pass + + OfficialInterface.upload_snap(file_path) + + return send_file(empty_file, + as_attachment=True, + download_name=empty_file, + mimetype='text/plain'), 200 # '/run-log'へのPOSTリクエストに対する操作