diff --git a/README.md b/README.md index 75e1b7b..dbc54ad 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,10 @@ $ sudo apt install python3-opencv ``` ## Usage +Use the `config.yaml` file to setup the system and validate the config by running: +``` +$ (venv) python3 validate_config.py +``` To publish camera frames over MQTT: ``` diff --git a/camera.py b/camera.py index 0b9b8b6..42956c3 100644 --- a/camera.py +++ b/camera.py @@ -4,19 +4,21 @@ import time from mqtt import get_mqtt_client -from helpers import pil_image_to_byte_array, get_now_string +from helpers import pil_image_to_byte_array, get_now_string, get_config from imutils.video import WebcamVideoStream from imutils import opencv2matplotlib from PIL import Image -MQTT_BROKER = "192.168.1.164" -MQTT_PORT = 1883 -MQTT_TOPIC_CAMERA = "homie/mac_webcam/capture" -MQTT_QOS = 1 +CONFIG = get_config("config.yml") -VIDEO_SOURCE = 0 # "rtsp://admin:password@192.168.1.94:554/11" # Int or string path -FPS = 2 # Limit to prevent CPU overheating! +MQTT_BROKER = CONFIG["mqtt"]["broker"] +MQTT_PORT = CONFIG["mqtt"]["port"] +MQTT_QOS = CONFIG["mqtt"]["QOS"] + +MQTT_TOPIC_CAMERA = CONFIG["camera"]["mqtt_topic"] +VIDEO_SOURCE = CONFIG["camera"]["vide_source"] +FPS = CONFIG["camera"]["fps"] def main(): @@ -38,7 +40,7 @@ def main(): client.publish(MQTT_TOPIC_CAMERA, byte_array, qos=MQTT_QOS) now = get_now_string() print(f"published frame on topic: {MQTT_TOPIC_CAMERA} at {now}") - time.sleep(1/FPS) + time.sleep(1 / FPS) if __name__ == "__main__": diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..42d5550 --- /dev/null +++ b/config.yml @@ -0,0 +1,18 @@ +mqtt: + broker: 192.168.1.164 + port: 1883 + QOS: 1 + +camera: + vide_source: 0 + fps: 2 + mqtt_topic: homie/mac_webcam/capture + +processing: + subscribe_topic: homie/mac_webcam/capture + publish_topic: homie/mac_webcam/capture/rotated + +save-captures: + mqtt_topic: homie/mac_webcam/capture + captures_directory: tests/captures/ + datetime_str_format: "%Y-%m-%d_%H:%M:%S.%f" \ No newline at end of file diff --git a/helpers.py b/helpers.py index 6e6b67b..0313b59 100644 --- a/helpers.py +++ b/helpers.py @@ -6,6 +6,7 @@ import datetime import io from PIL import Image +import yaml DATETIME_STR_FORMAT = "%Y-%m-%d_%H:%M:%S.%f" @@ -20,5 +21,11 @@ def byte_array_to_pil_image(byte_array): return Image.open(io.BytesIO(byte_array)) -def get_now_string(): +def get_now_string() -> str: return datetime.datetime.now().strftime(DATETIME_STR_FORMAT) + + +def get_config(config_filepath: str) -> dict: + with open(config_filepath) as f: + config = yaml.safe_load(f) + return config diff --git a/processing.py b/processing.py index 095b66f..266bba2 100644 --- a/processing.py +++ b/processing.py @@ -4,15 +4,22 @@ import time -from helpers import pil_image_to_byte_array, byte_array_to_pil_image, get_now_string +from helpers import ( + pil_image_to_byte_array, + byte_array_to_pil_image, + get_now_string, + get_config, +) from mqtt import get_mqtt_client -MQTT_BROKER = "192.168.1.164" -MQTT_PORT = 1883 -MQTT_SUBSCRIBE_TOPIC = "homie/mac_webcam/capture" -MQTT_PUBLISH_TOPIC = "homie/mac_webcam/capture/rotated" -MQTT_QOS = 1 +CONFIG = get_config("config.yml") +MQTT_BROKER = CONFIG["mqtt"]["broker"] +MQTT_PORT = CONFIG["mqtt"]["port"] +MQTT_QOS = CONFIG["mqtt"]["QOS"] + +MQTT_SUBSCRIBE_TOPIC = CONFIG["processing"]["subscribe_topic"] +MQTT_PUBLISH_TOPIC = CONFIG["processing"]["publish_topic"] ROTATE_ANGLE = 45 # Angle of rotation in degrees to apply diff --git a/requirements.txt b/requirements.txt index ec2806f..641fa71 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ black paho-mqtt opencv-python Pillow -imutils \ No newline at end of file +imutils +PyYAML \ No newline at end of file diff --git a/save-captures.py b/save-captures.py index 55c3b16..5bf538f 100644 --- a/save-captures.py +++ b/save-captures.py @@ -3,15 +3,18 @@ """ import time -from helpers import byte_array_to_pil_image, get_now_string +from helpers import byte_array_to_pil_image, get_now_string, get_config from mqtt import get_mqtt_client -CAPTURES_DIRECTORY = "tests/captures/" -DATETIME_STR_FORMAT = "%Y-%m-%d_%H:%M:%S.%f" +CONFIG = get_config("config.yml") -MQTT_BROKER = "192.168.1.164" -MQTT_PORT = 1883 -MQTT_TOPIC = "homie/mac_webcam/capture" +MQTT_BROKER = CONFIG["mqtt"]["broker"] +MQTT_PORT = CONFIG["mqtt"]["port"] +MQTT_QOS = CONFIG["mqtt"]["QOS"] + +SAVE_TOPIC = CONFIG["save-captures"]["mqtt_topic"] +CAPTURES_DIRECTORY = CONFIG["save-captures"]["captures_directory"] +DATETIME_STR_FORMAT = CONFIG["save-captures"]["datetime_str_format"] # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): @@ -34,7 +37,7 @@ def main(): client = get_mqtt_client() client.on_message = on_message client.connect(MQTT_BROKER, port=MQTT_PORT) - client.subscribe(MQTT_TOPIC) + client.subscribe(SAVE_TOPIC) time.sleep(4) # Wait for connection setup to complete client.loop_forever() diff --git a/validate_config.py b/validate_config.py new file mode 100644 index 0000000..69abc05 --- /dev/null +++ b/validate_config.py @@ -0,0 +1,11 @@ +from helpers import get_config +import pprint + +CONFIG_FILE = "config.yml" + +try: + CONFIG = get_config(CONFIG_FILE) + pprint.pprint(CONFIG) +except Exception as exc: + print(f"Invalid config in {CONFIG_FILE}") + print(exc)