Skip to content

Commit

Permalink
Merge pull request #2 from JIIOryo/feature/multiple-picture
Browse files Browse the repository at this point in the history
multiple cameras
  • Loading branch information
JIIOryo authored Sep 22, 2020
2 parents f9e8720 + 4f749b4 commit 14739c5
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 83 deletions.
3 changes: 2 additions & 1 deletion camera_config_template.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"client_id": "client_id_sample",
"port": 5364,
"tmp_picture_file_path": "/home/pi/ams-camera/tmp/now.jpg"
"tmp_base_picture_path": "/home/pi/ams-camera/tmp/base.jpg",
"tmp_trimmed_picture_path": "/home/pi/ams-camera/tmp/trimmed.jpg"
}
63 changes: 35 additions & 28 deletions controller/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,49 @@
from service.uploader import s3_upload, publish_picture
from lib.config import get_config_item

TMP_PICTURE_PATH = get_config_item('tmp_picture_file_path')
TMP_BASE_PICTURE_PATH = get_config_item('tmp_base_picture_path')
TMP_TRIMMED_PICTURE_PATH = get_config_item('tmp_trimmed_picture_path')

def picture(request: dict) -> None:

# case no camera
if len(request['cameras']) == 0:
return

take_picture(
file_ = TMP_PICTURE_PATH,
file_ = TMP_BASE_PICTURE_PATH,
x = request['resolution']['x'],
y = request['resolution']['y'],
warm_up_time = request['cameraWarmUpTime']
)

trimming(
file_ = TMP_PICTURE_PATH,
top = request['trimming']['top'],
bottom = request['trimming']['bottom'],
left = request['trimming']['left'],
right = request['trimming']['right']
)

if 'mqtt' in request['uploader']:
publish_picture(
file_ = TMP_PICTURE_PATH,
host = request['uploader']['mqtt']['host'],
port = request['uploader']['mqtt']['port'],
user_name = request['uploader']['mqtt']['userName'],
password = request['uploader']['mqtt']['password'],
retain = request['uploader']['mqtt']['retain'],
topic = request['uploader']['mqtt']['topic'],
for camera in request['cameras']:
trimming(
input_path = TMP_BASE_PICTURE_PATH,
output_path = TMP_TRIMMED_PICTURE_PATH,
top = camera['trimming']['top'],
bottom = camera['trimming']['bottom'],
left = camera['trimming']['left'],
right = camera['trimming']['right']
)

if 'aws' in request['uploader']:
s3_upload(
file_ = TMP_PICTURE_PATH,
object_name = request['objectName'],
access_key_id = request['uploader']['aws']['accessKeyId'],
secret_access_key = request['uploader']['aws']['secretAccessKey'],
bucket = request['uploader']['aws']['s3Bucket'],
region = request['uploader']['aws']['region'],
)
if 'mqtt' in request['uploader'] and 'topic' in camera:
publish_picture(
file_ = TMP_TRIMMED_PICTURE_PATH,
host = request['uploader']['mqtt']['host'],
port = request['uploader']['mqtt']['port'],
user_name = request['uploader']['mqtt']['userName'],
password = request['uploader']['mqtt']['password'],
retain = request['uploader']['mqtt']['retain'],
topic = camera['topic'],
)

if 'aws' in request['uploader'] and 'objectName' in camera:
s3_upload(
file_ = TMP_TRIMMED_PICTURE_PATH,
object_name = camera['objectName'],
access_key_id = request['uploader']['aws']['accessKeyId'],
secret_access_key = request['uploader']['aws']['secretAccessKey'],
bucket = request['uploader']['aws']['s3Bucket'],
region = request['uploader']['aws']['region'],
)
120 changes: 69 additions & 51 deletions schemas/picture.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,55 @@
"picture": {
"type": "object",
"properties": {
"objectName": {
"type": "string"
"cameras": {
"type": "array",
"items": {
"type": "object",
"properties": {
"objectName": {
"type": "string"
},
"topic": {
"type": "string"
},
"trimming": {
"type": "object",
"properties": {
"top": {
"type": "number",
"minimum": 0,
"maximum": 2048
},
"bottom": {
"type": "number",
"minimum": 0,
"maximum": 2048
},
"left": {
"type": "number",
"minimum": 0,
"maximum": 2048
},
"right": {
"type": "number",
"minimum": 0,
"maximum": 2048
}
},
"required": [
"top",
"bottom",
"left",
"right"
],
"additionalProperties": false
}
},
"required": [
"trimming"
],
"additionalProperties": false
}
},
"resolution": {
"type": "object",
Expand All @@ -22,38 +69,8 @@
"required": [
"x",
"y"
]
},
"trimming": {
"type": "object",
"properties": {
"top": {
"type": "number",
"minimum": 0,
"maximum": 2048
},
"bottom": {
"type": "number",
"minimum": 0,
"maximum": 2048
},
"left": {
"type": "number",
"minimum": 0,
"maximum": 2048
},
"right": {
"type": "number",
"minimum": 0,
"maximum": 2048
}
},
"required": [
"top",
"bottom",
"left",
"right"
]
],
"additionalProperties": false
},
"cameraWarmUpTime": {
"type": "number",
Expand Down Expand Up @@ -104,16 +121,12 @@
},
"retain": {
"type": "boolean"
},
"topic": {
"type": "string"
}
},
"required": [
"host",
"port",
"retain",
"topic"
"retain"
]
}
},
Expand All @@ -126,25 +139,31 @@
}
},
"required": [
"objectName",
"cameras",
"resolution",
"trimming",
"cameraWarmUpTime"
"cameraWarmUpTime",
"uploader"
],
"additionalProperties": false,
"examples": [
{
"objectName": "tank_id/camera_id/2020/07/24/14_38_12.jpg",
"cameras": [
{
"objectName": "tank_id/camera_id/2020/07/24/14_38_12.jpg",
"topic": "picture/test",
"trimming": {
"top": 100,
"bottom": 1024,
"left": 720,
"right": 1700
}
}
],
"cameraWarmUpTime": 2.5,
"resolution": {
"x": 1700,
"y": 1024
},
"trimming": {
"top": 100,
"bottom": 1024,
"left": 720,
"right": 1700
},
"cameraWarmUpTime": 2.5,
"uploader": {
"aws": {
"accessKeyId": "accessKeyId",
Expand All @@ -155,7 +174,6 @@
"mqtt": {
"host": "broker_ip_address",
"port": 1883,
"topic": "picture/test",
"retain": false
}
},
Expand Down
6 changes: 3 additions & 3 deletions service/trimming.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cv2

def trimming(file_: str, top: int, bottom: int, left: int, right: int) -> None:
img = cv2.imread(file_)
def trimming(input_path: str, output_path: str, top: int, bottom: int, left: int, right: int) -> None:
img = cv2.imread(input_path)
trimmed_img = img[top : bottom, left: right]
cv2.imwrite(file_, trimmed_img)
cv2.imwrite(output_path, trimmed_img)
return

0 comments on commit 14739c5

Please sign in to comment.