From fa4c7eef036795d4e9a0eeeff52bb7a4c825d7bc Mon Sep 17 00:00:00 2001 From: Cyrille Nofficial Date: Thu, 20 Oct 2022 10:48:35 +0200 Subject: [PATCH] refactor(cli): remove docopt dependency --- camera/cli.py | 100 ++++++++++++++++++++++++++++--------------------- poetry.lock | 13 +------ pyproject.toml | 1 - 3 files changed, 58 insertions(+), 56 deletions(-) diff --git a/camera/cli.py b/camera/cli.py index 8c8c9dc..b205136 100644 --- a/camera/cli.py +++ b/camera/cli.py @@ -1,31 +1,8 @@ -""" -Publish data from oak-lite device - -Usage: rc-oak-camera [-u USERNAME | --mqtt-username=USERNAME] [--mqtt-password=PASSWORD] \ - [--mqtt-broker-host=HOSTNAME] [--mqtt-broker-port=PORT] \ - [--mqtt-topic-robocar-oak-camera="TOPIC_CAMERA"] [--mqtt-topic-robocar-objects="TOPIC_OBJECTS"] \ - [--mqtt-client-id=CLIENT_ID] \ - [-H IMG_HEIGHT | --image-height=IMG_HEIGHT] [-W IMG_WIDTH | --image-width=IMG_width] \ - [-t OBJECTS_THRESHOLD | --objects-threshold=OBJECTS_THRESHOLD] - -Options: --h --help Show this screen. --u USERID --mqtt-username=USERNAME MQTT user --p PASSWORD --mqtt-password=PASSWORD MQTT password --b HOSTNAME --mqtt-broker-host=HOSTNAME MQTT broker host --P HOSTNAME --mqtt-broker-port=PORT MQTT broker port --C CLIENT_ID --mqtt-client-id=CLIENT_ID MQTT client id --c TOPIC_CAMERA --mqtt-topic-robocar-oak-camera=TOPIC_CAMERA MQTT topic where to publish robocar-oak-camera frames --o TOPIC_OBJECTS --mqtt-topic-robocar-objects=TOPIC_OBJECTS MQTT topic where to publish objects detection results --H IMG_HEIGHT --image-height=IMG_HEIGHT IMG_HEIGHT image height --W IMG_WIDTH --image-width=IMG_width IMG_WIDTH image width --t OBJECTS_THRESHOLD --objects-threshold=OBJECTS_THRESHOLD OBJECTS_THRESHOLD threshold to filter objects detected -""" import logging import os from . import depthai as cam -from docopt import docopt import paho.mqtt.client as mqtt +import argparse logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) @@ -47,31 +24,68 @@ def init_mqtt_client(broker_host: str, broker_port, user: str, password: str, cl def execute_from_command_line(): logging.basicConfig(level=logging.INFO) - args = docopt(__doc__) + parser = argparse.ArgumentParser() + parser.add_argument("-u", "--mqtt-username", + help="MQTT user", + default=_get_env_value("MQTT_USERNAME", "")) + parser.add_argument("-p", "--mqtt-password", + help="MQTT password", + default=_get_env_value("MQTT_PASSWORD", "")) + parser.add_argument("-b", "--mqtt-broker-host", + help="MQTT broker host", + default=_get_env_value("MQTT_BROKER_HOST", "localhost")) + parser.add_argument("-P", "--mqtt-broker-port", + help="MQTT broker port", + type=int, + default=_get_env_int_value("MQTT_BROKER_PORT", 1883)) + parser.add_argument("-C", "--mqtt-client-id", + help="MQTT client id", + default=_get_env_value("MQTT_CLIENT_ID", default_client_id)) + parser.add_argument("-c", "--mqtt-topic-robocar-oak-camera", + help="MQTT topic where to publish robocar-oak-camera frames", + default=_get_env_value("MQTT_TOPIC_CAMERA","/oak/camera_rgb")) + parser.add_argument("-o", "---mqtt-topic-robocar-objects", + help="MQTT topic where to publish objects detection results", + default=_get_env_value("MQTT_TOPIC_OBJECTS", "/objects")) + parser.add_argument("-t", "--objects-threshold", + help="threshold to filter detected objects", + type=float, + default=_get_env_float_value("OBJECTS_THRESHOLD", 0.2)) + parser.add_argument("-H", "--image-height", help="image height", + type=int, + default=_get_env_int_value("IMAGE_HEIGHT", 120)) + parser.add_argument("-W", "--image-width", help="image width", + type=int, + default=_get_env_int_value("IMAGE_WIDTH", 126)) - client = init_mqtt_client(broker_host=get_default_value(args["--mqtt-broker-host"], "MQTT_BROKER_HOST", "localhost"), - broker_port=int(get_default_value(args["--mqtt-broker-port"], "MQTT_BROKER_PORT", "1883")), - user=get_default_value(args["--mqtt-username"], "MQTT_USERNAME", ""), - password=get_default_value(args["--mqtt-password"], "MQTT_PASSWORD", ""), - client_id=get_default_value(args["--mqtt-client-id"], "MQTT_CLIENT_ID", - default_client_id), + args = parser.parse_args() + + client = init_mqtt_client(broker_host=args.mqtt_broker_host, + broker_port=args.mqtt_broker_port, + user=args.mqtt_username, + password=args.mqtt_password, + client_id=args.mqtt_client_id, ) - frame_topic = get_default_value(args["--mqtt-topic-robocar-oak-camera"], "MQTT_TOPIC_CAMERA", "/oak/camera_rgb") - objects_topic = get_default_value(args["--mqtt-topic-robocar-objects"], "MQTT_TOPIC_OBJECTS", "/objects") - frame_processor = cam.FramePublisher(mqtt_client=client, - frame_topic=frame_topic, - objects_topic=objects_topic, - objects_threshold=float(get_default_value(args["--objects-threshold"], - "OBJECTS_THRESHOLD", 0.2)), - img_width=int(get_default_value(args["--image-width"], "IMAGE_WIDTH", 160)), - img_height=int(get_default_value(args["--image-height"], "IMAGE_HEIGHT", 120))) + frame_topic=args.mqtt_topic_robocar_oak_camera, + objects_topic=args.mqtt_topic_robocar_objects, + objects_threshold=args.objects_threshold, + img_width=args.image_width, + img_height=args.image_height) frame_processor.run() -def get_default_value(value, env_var: str, default_value) -> str: - if value: - return value +def _get_env_value(env_var: str, default_value: str) -> str: if env_var in os.environ: return os.environ[env_var] return default_value + + +def _get_env_int_value(env_var: str, default_value: int) -> int: + value = _get_env_value(env_var, str(default_value)) + return int(value) + + +def _get_env_float_value(env_var: str, default_value: float) -> float: + value = _get_env_value(env_var, str(default_value)) + return float(value) diff --git a/poetry.lock b/poetry.lock index a5035d2..d0d0134 100644 --- a/poetry.lock +++ b/poetry.lock @@ -107,14 +107,6 @@ category = "main" optional = false python-versions = ">=3.6" -[[package]] -name = "docopt" -version = "0.6.2" -description = "Pythonic argument parser, that will make you smile" -category = "main" -optional = false -python-versions = "*" - [[package]] name = "google" version = "3.0.0" @@ -354,7 +346,7 @@ socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"] [metadata] lock-version = "1.1" python-versions = "^3.10" -content-hash = "93f922ddb9c03189bf1efabdcf702513ff5526332a78f54c21ab532574f4519a" +content-hash = "b165038947d8a9e0e256c91b449c4cbd1e65ccfd0e65f271d002fc8e21e2640e" [metadata.files] attrs = [ @@ -421,9 +413,6 @@ depthai = [ {file = "depthai-2.17.4.0-cp39-cp39-win_amd64.whl", hash = "sha256:de7b5e4a15b0a9e15fdf19c5388e6aa9629ecc25a955675d400173f9a2dd199b"}, {file = "depthai-2.17.4.0.tar.gz", hash = "sha256:b8e3c2eaa643c64e737435adaa23d54e3fda58baad1477f5176908689473a85f"}, ] -docopt = [ - {file = "docopt-0.6.2.tar.gz", hash = "sha256:49b3a825280bd66b3aa83585ef59c4a8c82f2c8a522dbe754a8bc8d08c85c491"}, -] google = [ {file = "google-3.0.0-py2.py3-none-any.whl", hash = "sha256:889cf695f84e4ae2c55fbc0cfdaf4c1e729417fa52ab1db0485202ba173e4935"}, {file = "google-3.0.0.tar.gz", hash = "sha256:143530122ee5130509ad5e989f0512f7cb218b2d4eddbafbad40fd10e8d8ccbe"}, diff --git a/pyproject.toml b/pyproject.toml index 8a25931..b54897b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,6 @@ packages = [ [tool.poetry.dependencies] python = "^3.10" paho-mqtt = "^1.6.1" -docopt = "^0.6.2" depthai = "^2.17.4.0" protobuf3 = "^0.2.1" google = "^3.0.0"