def main(args): prediction_client = PredictionClient(args.address, args.port) if DEVICE_CONNECTION_STRING and not args.suppress_messages: iothub_client = IoTHubClient(DEVICE_CONNECTION_STRING, PROTOCOL) while True: for image in os.listdir(args.image_dir): # score image try: start_time = time.time() results = prediction_client.score_file( path=os.path.join(args.image_dir, image), input_name=args.input_tensors, outputs=args.output_tensors) inference_time = (time.time() - start_time) * 1000 result = 'cat' if results[0] > results[1] else 'dog' msg_string = "(%.3f ms) The image %s is a '%s'." % ( inference_time, image, result) print(msg_string) except: tb = traceback.format_exc() if "StatusCode.UNAVAILABLE" in tb: msg_string = "Unable to inference because AzureML host container is not done flashing the FPGA. If still not available in 5 minutes, check logs of module." elif "Object reference not set to an instance of an object" in tb: msg_string = "Unable to inference because the names of the input and output tensors used for scoring are incorrect.\n" + \ "Please update the docker CMD arguments to include the correct --input-tensors and --output-tensors parameters." else: msg_string = "Unable to inference for unknown reason. See stack trace below:\n{}".format( tb) print(msg_string) print("Trying again in {} seconds...".format(args.wait)) if DEVICE_CONNECTION_STRING and not args.suppress_messages: send_iothub_message(iothub_client, msg_string) time.sleep(args.wait)
def main(args): prediction_client = PredictionClient(args.address, args.port) if DEVICE_CONNECTION_STRING and not args.suppress_messages: iothub_client = IoTHubClient(DEVICE_CONNECTION_STRING, PROTOCOL) classes_entries = requests.get( "https://raw.githubusercontent.com/Lasagne/Recipes/master/examples/resnet50/imagenet_classes.txt" ).text.splitlines() while True: for image in os.listdir(args.image_dir): # score image try: start_time = time.time() results = prediction_client.score_file( path=os.path.join(args.image_dir, image), input_name=args.input_tensors, outputs=args.output_tensors) inference_time = (time.time() - start_time) * 1000 # map results [class_id] => [confidence] results = enumerate(results) # sort results by confidence sorted_results = sorted(results, key=lambda x: x[1], reverse=True) top_result = sorted_results[0] msg_string = "(%.0f ms) The image %s was classified as %s with confidence %s." % ( inference_time, os.path.join(args.image_dir, image), classes_entries[top_result[0]], top_result[1]) print(msg_string) except: tb = traceback.format_exc() if "StatusCode.UNAVAILABLE" in tb: msg_string = "Unable to inference because AzureML host container is not done flashing the FPGA. If still not available in 5 minutes, check logs of module." elif "Object reference not set to an instance of an object" in tb: msg_string = "Unable to inference because the names of the input and output tensors used for scoring are incorrect.\n" + \ "Please update the docker CMD arguments to include the correct --input-tensors and --output-tensors parameters." else: msg_string = "Unable to inference for unknown reason. See stack trace below:\n{}".format( tb) print(msg_string) print("Trying again in {} seconds...".format(args.wait)) if DEVICE_CONNECTION_STRING and not args.suppress_messages: send_iothub_message(iothub_client, msg_string) time.sleep(args.wait)
async def main(): try: if not sys.version >= "3.5.3": raise Exception( "The sample requires python 3.5.3+. Current version of Python: %s" % sys.version) print("IoT Hub Client for Python") # The client object is used to interact with your Azure IoT hub. module_client = IoTHubModuleClient.create_from_edge_environment() # connect the client. await module_client.connect() prediction_client = PredictionClient(address='resnet50', port=50051) classes_entries = requests.get( "https://raw.githubusercontent.com/Lasagne/Recipes/master/examples/resnet50/imagenet_classes.txt" ).text.splitlines() image_dir = "./assets/" for image in os.listdir(image_dir): try: start_time = time.time() results = prediction_client.score_file( path=os.path.join(image_dir, image), input_name='Placeholder:0', outputs='classifier/resnet_v1_50/predictions/Softmax:0') inference_time = (time.time() - start_time) * 1000 # map results [class_id] => [confidence] results = enumerate(results) # sort results by confidence sorted_results = sorted(results, key=lambda x: x[1], reverse=True) top_result = sorted_results[0] msg_string = "(%.0f ms) The image %s was classified as %s with confidence %s." % ( inference_time, os.path.join(image_dir, image), classes_entries[top_result[0]], top_result[1]) print(msg_string) except: print('except') # define behavior for receiving an input message on input1 async def input1_listener(module_client): while True: input_message = await module_client.receive_message_on_input( "input1") # blocking call print("the data in the message received on input1 was ") print(input_message.data) print("custom properties are") print(input_message.custom_properties) print("forwarding mesage to output1") await module_client.send_message_to_output( input_message, "output1") # define behavior for halting the application def stdin_listener(): while True: try: selection = input("Press Q to quit\n") if selection == "Q" or selection == "q": print("Quitting...") break except: time.sleep(10) # Schedule task for C2D Listener listeners = asyncio.gather(input1_listener(module_client)) print("The sample is now waiting for messages. ") # Run the stdin listener in the event loop loop = asyncio.get_event_loop() user_finished = loop.run_in_executor(None, stdin_listener) # Wait for user to indicate they are done listening for messages await user_finished # Cancel listening listeners.cancel() # Finally, disconnect await module_client.disconnect() except Exception as e: print("Unexpected error %s " % e) raise
async def processImages(module_client): global scoring global predictClient global fps async def scoreImage(module_client): global scoring global predictClient global fps currentFPS = fps.tick()[0] image = cv2.imread("image.jpg") result = predictClient.score_file(path="image.jpg", input_name=input_tensor, outputs=output_tensors) classes, scores, bboxes = ssdvgg_utils.postprocess( result, select_threshold=0.5) if bboxes.size > 0: message = { "classes": str(classes.tolist()).strip( '[]'), # because Azure IoT does not deal with list in JSON "scores": str(scores.tolist()).strip('[]'), "boxes": str(bboxes.tolist()).strip('[]'), "datetime": str(datetime.datetime.now()), "imageH": image.shape[0], "imageW": image.shape[1], "CameraURL": CameraURL, "scoredFPS": currentFPS } await sendmessage(module_client, message) scoring = False while True: try: global predictClient if FPGAinCloudAddress is not None and CameraURL is not None and FPGAinCloudaks_servicename is not None: predictClient = PredictionClient( address=FPGAinCloudAddress, port=FPGAinCloudPort, use_ssl=FPGAinCloudssl_enabled, service_name=FPGAinCloudaks_servicename) if "RTSP" in str(CameraURL).upper(): OpenCV = cv2.VideoCapture(CameraURL) while True: image = OpenCV.read()[1] if scoring == False: scoring = True cv2.imwrite(image, "image.jpg") scoreImage(module_client) if PauseBetweenScoreSeconds > 0: print("sleeping " + str(PauseBetweenScoreSeconds) + " seconds between scoring") time.sleep(PauseBetweenScoreSeconds) elif "HTTP" in str(CameraURL).upper(): while True: urllib.request.urlretrieve(url=CameraURL, filename="image.jpg") await scoreImage(module_client) if PauseBetweenScoreSeconds > 0: print("sleeping " + str(PauseBetweenScoreSeconds) + " seconds between scoring") time.sleep(PauseBetweenScoreSeconds) else: print( "FPGAinCloudAddress and/or CamaraURL and/or FPGAinCloudaks_servicename in TWINS are not set." ) time.sleep(15) except Exception as e: print(e) print("sleeping for 15 seconds until retry") time.sleep(15)
self.respond(bytes(json.dumps(message), "utf8")) else: self.respond(bytes("{}", "utf8")) return if __name__ == "__main__": FPGAinCloudAddress = "40.121.68.174" FPGAinCloudPort = 80 FPGAinCloudssl_enabled = False FPGAinCloudaks_servicename = "fpga-aks-service" if 'FPGAinCloudAddress' in os.environ.keys(): FPGAinCloudAddress = os.environ['FPGAinCloudAddress'] if 'FPGAinCloudPort' in os.environ.keys(): FPGAinCloudPort = os.environ['FPGAinCloudPort'] if 'FPGAinCloudssl_enabled' in os.environ.keys(): FPGAinCloudssl_enabled = util.strtobool(os.environ['FPGAinCloudssl_enabled']) if 'FPGAinCloudaks_servicename' in os.environ.keys(): FPGAinCloudaks_servicename = os.environ['FPGAinCloudaks_servicename'] print("Connecting to " + FPGAinCloudAddress + ":" + str(FPGAinCloudPort) + " ssl:" + str(FPGAinCloudssl_enabled) + " service:" + FPGAinCloudaks_servicename) # starting the PredictClient predictClient = PredictionClient(address=FPGAinCloudAddress, port=FPGAinCloudPort, use_ssl=FPGAinCloudssl_enabled, service_name=FPGAinCloudaks_servicename) # starting the web server thread _thread.start_new_thread(webServer, (80, )) while True: time.sleep(60) # allow the background thread to serve web request
def processImages(module_client): global scoring global predictClient global fps def scoreImage(module_client): global scoring global predictClient global fps currentFPS = fps.tick()[0] image = cv2.imread("image.jpg") result = predictClient.score_file(path="image.jpg", input_name=input_tensor, outputs=output_tensors) classes, scores, bboxes = ssdvgg_utils.postprocess( result, select_threshold=0.5) if bboxes.size > 0: message = { "classes": classes, "scores": scores, "boxes": bboxes, "datetime": str(datetime.datetime.now()), "imageH": image.shape[0], "imageW": image.shape[1], "CameraURL": CameraURL, "scoredFPS": currentFPS } IoTHubMessage = IoTHubMessage(bytearray(json.dumps(message))) module_client.forward_event_to_output("output1", IoTHubMessage, SEND_MESSAGECOUNTER) scoring = False while True: try: global predictClient if FPGAinCloudAddress is not None and CameraURL is not None and FPGAinCloudaks_servicename is not None: predictClient = PredictionClient( address=FPGAinCloudAddress, port=FPGAinCloudPort, use_ssl=FPGAinCloudssl_enabled, service_name=FPGAinCloudaks_servicename) if "RTSP" in str(CameraURL).upper(): OpenCV = cv2.VideoCapture(CameraURL) while True: image = OpenCV.read()[1] if scoring == False: scoring = True cv2.imwrite(image, "image.jpg") scoreImage(module_client) elif "HTTP" in str(CameraURL).upper(): while True: urllib.request.urlretrieve(url=CameraURL, filename="image.jpg") scoreImage(module_client) else: print( "FPGAinCloudAddress and/or CamaraURL and/or FPGAinCloudaks_servicename in TWINS are not set." ) except Exception as e: print(e) print("sleeping for 15 seconds until retry") time.sleep(15)