From 024cfeeb9d4064bc7cb2baedcd1b5a70301e1426 Mon Sep 17 00:00:00 2001 From: Paul DeCarlo Date: Mon, 15 Jul 2019 11:37:47 -0500 Subject: [PATCH] Publish detected objects to Azure IoT Hub --- config/deployment.arm32v7.json | 12 +++--------- deployment.template.json | 9 +-------- modules/YoloModule/app/AppState.py | 3 +++ modules/YoloModule/app/YoloInference.py | 24 ++++++++++++++++++++++-- modules/YoloModule/app/main.py | 3 +++ 5 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 modules/YoloModule/app/AppState.py diff --git a/config/deployment.arm32v7.json b/config/deployment.arm32v7.json index 68861f8..010b97e 100644 --- a/config/deployment.arm32v7.json +++ b/config/deployment.arm32v7.json @@ -53,19 +53,13 @@ "$edgeHub": { "properties.desired": { "schemaVersion": "1.0", - "routes": {}, + "routes": { + "YoloModuleToIoTHub": "FROM /messages/modules/YoloModule/outputs/* INTO $upstream" + }, "storeAndForwardConfiguration": { "timeToLiveSecs": 7200 } } - }, - "YoloModule": { - "properties.desired": { - "ConfidenceLevel": "0.3", - "VerboseMode": 0, - "Inference": 1, - "VideoSource": "" - } } } } \ No newline at end of file diff --git a/deployment.template.json b/deployment.template.json index a016e4a..5a53f55 100644 --- a/deployment.template.json +++ b/deployment.template.json @@ -132,19 +132,12 @@ "properties.desired": { "schemaVersion": "1.0", "routes": { + "YoloModuleToIoTHub": "FROM /messages/modules/YoloModule/outputs/* INTO $upstream" }, "storeAndForwardConfiguration": { "timeToLiveSecs": 7200 } } - }, - "YoloModule" : { - "properties.desired": { - "ConfidenceLevel" : "0.3", - "VerboseMode" : 0, - "Inference" : 1, - "VideoSource" : "" - } } } } \ No newline at end of file diff --git a/modules/YoloModule/app/AppState.py b/modules/YoloModule/app/AppState.py new file mode 100644 index 0000000..a3b3594 --- /dev/null +++ b/modules/YoloModule/app/AppState.py @@ -0,0 +1,3 @@ +def init(hubManager): + global HubManager + HubManager = hubManager \ No newline at end of file diff --git a/modules/YoloModule/app/YoloInference.py b/modules/YoloModule/app/YoloInference.py index 5c8fac7..e3a55d8 100644 --- a/modules/YoloModule/app/YoloInference.py +++ b/modules/YoloModule/app/YoloInference.py @@ -4,11 +4,20 @@ from __future__ import absolute_import from darknet import darknet +import AppState + +import iothub_client +# pylint: disable=E0611 +# Disabling linting that is not supported by Pylint for C extensions such as iothub_client. See issue https://github.com/PyCQA/pylint/issues/1955 +from iothub_client import (IoTHubMessage) + import cv2 #import cv2.cv as cv import numpy as np import time import os +import json +from datetime import datetime yolocfg = r'yolo/yolov3-tiny.cfg' yoloweight = r'yolo/yolov3-tiny.weights' @@ -37,6 +46,7 @@ class YoloInference(object): self.net = None self.rgb = True self.verbose = False + self.lastMessageSentTime = datetime.now() # Read class names from text file print(" - Setting Classes") @@ -80,6 +90,8 @@ class YoloInference(object): detections = darknet.detect(darknet.netMain, darknet.metaMain, frame, confidenceLevel) + countsByClassId = {}; + for detection in detections: classLabel = detection[0] @@ -88,8 +100,10 @@ class YoloInference(object): if confidence > confidenceLevel: - if self.verbose: - print( "Class Label : %s Confidence %f" % (classLabel, confidence)) + if classID not in countsByClassId: + countsByClassId[classID] = 1 + else: + countsByClassId[classID] = countsByClassId[classID] + 1 bounds = detection[2] @@ -101,6 +115,12 @@ class YoloInference(object): self.__draw_rect(frame, classID, confidence, xCoord, yCoord, xCoord + xEntent, yCoord + yExtent) + if len(countsByClassId) > 0 and (datetime.now() - self.lastMessageSentTime).total_seconds() >= 1 : + strMessage = json.dumps(countsByClassId) + message = IoTHubMessage(strMessage) + AppState.HubManager.send_event_to_output("output1", message, 0) + self.lastMessageSentTime=datetime.now() + except Exception as e: print("Exception during AI Inference") print(e) \ No newline at end of file diff --git a/modules/YoloModule/app/main.py b/modules/YoloModule/app/main.py index 13cd998..5e9cc87 100644 --- a/modules/YoloModule/app/main.py +++ b/modules/YoloModule/app/main.py @@ -18,6 +18,8 @@ from iothub_client import (IoTHubModuleClient, IoTHubClientError, IoTHubError, import VideoCapture from VideoCapture import VideoCapture +import AppState + def send_to_Hub_callback(strMessage): message = IoTHubMessage(bytearray(strMessage, 'utf8')) print("\r\nsend_to_Hub_callback()") @@ -147,6 +149,7 @@ def main( try: hubManager = HubManager(10000, IoTHubTransportProvider.MQTT, False) + AppState.init(hubManager) except IoTHubError as iothub_error: print("Unexpected error %s from IoTHub" % iothub_error ) return