Skip to content

Commit

Permalink
Merge pull request #7 from robmarkcole/adds-config
Browse files Browse the repository at this point in the history
Adds config
  • Loading branch information
robmarkcole authored Jul 23, 2019
2 parents 67b334b + 93ca64b commit a4991c2
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 23 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
```
Expand Down
18 changes: 10 additions & 8 deletions camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]: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():
Expand All @@ -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__":
Expand Down
18 changes: 18 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -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"
9 changes: 8 additions & 1 deletion helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import datetime
import io
from PIL import Image
import yaml

DATETIME_STR_FORMAT = "%Y-%m-%d_%H:%M:%S.%f"

Expand All @@ -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
19 changes: 13 additions & 6 deletions processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ black
paho-mqtt
opencv-python
Pillow
imutils
imutils
PyYAML
17 changes: 10 additions & 7 deletions save-captures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()

Expand Down
11 changes: 11 additions & 0 deletions validate_config.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit a4991c2

Please sign in to comment.