hailo async processing, data, hef

- hef files for darias yolov5, yolov5m
  and yolov5s added to repo
- minimal data example with image and
  daria / coco labels
- inference now allows an async call,
  which keeps the connection to the hailo
  device open, which allows for a higher
  fps rate (as shown in the test example)
This commit is contained in:
Raphael Maenle
2022-03-02 16:43:07 +01:00
parent be89e739b4
commit 43d02593f2
8 changed files with 201 additions and 9 deletions

View File

@ -1,8 +1,11 @@
import json
import os
import time
t
import ipdb
from PIL import Image
from threading import Thread
from detection_tools.utils.visualization_utils import \
visualize_boxes_and_labels_on_image_array
@ -59,8 +62,8 @@ class DataHandler:
return coco_names
def _get_labels(self, label_name):
filename = os.path.join(os.path.dirname(__file__), label_name + '.json')
def get_labels(self, path):
filename = os.path.join(os.path.dirname(__file__), path)
names = json.load(open(filename))
names = {int(k): {'id': int(k), 'name': str(v)} for (k, v) in names.items()}
return names
@ -279,22 +282,79 @@ class HailoHandler:
out = [infer_results[i.name] for i in self.output_vstream_infos]
return out
def start_hailo_thread(self):
self.hailo_async = True
self.hailo_block = False
self.input_data = None
self.hailo_thread = Thread(target=self._hailo_async)
self.hailo_thread.start()
def _hailo_async(self):
with InferVStreams(self.network_group, self.input_vstreams_params, self.output_vstreams_params)\
as infer_pipeline:
with self.network_group.activate(self.network_group_params):
self._hailo_async_loop(infer_pipeline)
def process_yolo5():
def _hailo_async_loop(self, infer_pipeline):
while self.hailo_async:
if(not self.hailo_block and type(self.input_data) != type(None)):
self.infer_results = None
self.hailo_block = True
infer_results = infer_pipeline.infer(self.input_data)
self.infer_results = [infer_results[i.name] for i in self.output_vstream_infos]
self.input_data = None
self.hailo_block = False
def hailo_input(self, input_data):
while self.hailo_block:
time.sleep(0.01)
self.hailo_block = True
self.input_data = input_data
self.input_data = {self.input_vstream_info.name: input_data}
self.infer_results = None
self.hailo_block = False
def hailo_output(self):
while self.hailo_block:
time.sleep(0.01)
return self.infer_results
def stop_hailo_thread(self):
self.hailo_async = False
self.hailo_thread.join()
def test_async_yolo5():
imageMeta = ImageMeta(640, 640, 3)
processor = YoloProcessing(imageMeta, classes=3)
data = DataHandler('./minimal_data', imageMeta)
data = DataHandler('./data', imageMeta)
data.load_data(processor.preproc)
hailo = HailoHandler('hef/yolov5m_22_2.hef')
out = hailo.run_hailo(data.dataset)
hailo = HailoHandler('hef/yolov5m_daria.hef')
hailo.start_hailo_thread()
fps = 0
now = time.time()
for i in range(1000):
fps += 1
if now + 1 < time.time():
print(fps)
fps = 0
now = time.time()
hailo.hailo_input(data.dataset)
out = None
while(out == None):
time.sleep(0.01)
out = hailo.hailo_output()
hailo.stop_hailo_thread()
logits = processor.postprocessing(out)
labels = data._get_labels("daria_names")
labels = data.get_labels("data/daria_labels.json")
image = visualize_boxes_and_labels_on_image_array(
data.dataset[0],
logits['detection_boxes'].numpy()[0],
@ -308,6 +368,38 @@ def process_yolo5():
line_thickness=4)
Image.fromarray(np.uint8(image)).save('/home/maintenance/test.png')
print("Successfully saved image")
def test_process_yolo5():
imageMeta = ImageMeta(640, 640, 3)
processor = YoloProcessing(imageMeta, classes=3)
data = DataHandler('./data', imageMeta)
data.load_data(processor.preproc)
hailo = HailoHandler('hef/yolov5m_daria.hef')
out = hailo.run_hailo(data.dataset)
logits = processor.postprocessing(out)
labels = data.get_labels("data/daria_labels.json")
image = visualize_boxes_and_labels_on_image_array(
data.dataset[0],
logits['detection_boxes'].numpy()[0],
logits['detection_classes'][0],
logits['detection_scores'].numpy()[0],
labels,
use_normalized_coordinates=True,
max_boxes_to_draw=100,
min_score_thresh=.5,
agnostic_mode=False,
line_thickness=4)
Image.fromarray(np.uint8(image)).save('/home/maintenance/test.png')
print("Successfully saved image")
if __name__ == "__main__":
process_yolo5()
test_async_yolo5()