зеркало из
1
0
Форкнуть 0

Publish detected objects to Azure IoT Hub

This commit is contained in:
Paul DeCarlo 2019-07-15 11:37:47 -05:00
Родитель 593f730954
Коммит 024cfeeb9d
5 изменённых файлов: 32 добавлений и 19 удалений

Просмотреть файл

@ -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": ""
}
}
}
}

Просмотреть файл

@ -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" : ""
}
}
}
}

Просмотреть файл

@ -0,0 +1,3 @@
def init(hubManager):
global HubManager
HubManager = hubManager

Просмотреть файл

@ -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)

Просмотреть файл

@ -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