Skip to content

Commit 06b1d1d

Browse files
authored
Merge pull request #3397 from anzhella-pankratova/action_recognition_demo_fix
action_recognition_demo.py: use AsyncInferQueue
2 parents 02a599f + e2802ae commit 06b1d1d

File tree

3 files changed

+24
-11
lines changed
  • demos
    • action_recognition_demo/python/action_recognition_demo
    • gesture_recognition_demo/python/gesture_recognition_demo

3 files changed

+24
-11
lines changed

demos/action_recognition_demo/python/action_recognition_demo/models.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import cv2
2323
import numpy as np
2424

25+
from openvino.runtime import AsyncInferQueue
26+
2527

2628
def center_crop(frame, crop_size):
2729
img_h, img_w, _ = frame.shape
@@ -93,22 +95,27 @@ def __init__(self, model_path, core, target_device, num_requests, model_type):
9395
log.error("Demo supports only models with 1 output")
9496
sys.exit(1)
9597

98+
self.outputs = {}
9699
compiled_model = core.compile_model(self.model, target_device)
97100
self.output_tensor = compiled_model.outputs[0]
98101
self.input_name = self.model.inputs[0].get_any_name()
99102
self.input_shape = self.model.inputs[0].shape
100103

101104
self.num_requests = num_requests
102-
self.infer_requests = [compiled_model.create_infer_request() for _ in range(self.num_requests)]
105+
self.infer_queue = AsyncInferQueue(compiled_model, num_requests)
106+
self.infer_queue.set_callback(self.completion_callback)
103107
log.info('The {} model {} is loaded to {}'.format(model_type, model_path, target_device))
104108

109+
def completion_callback(self, infer_request, id):
110+
self.outputs[id] = infer_request.results[self.output_tensor]
111+
105112
def async_infer(self, frame, req_id):
106113
input_data = {self.input_name: frame}
107-
self.infer_requests[req_id].start_async(inputs=input_data)
114+
self.infer_queue.start_async(input_data, req_id)
108115

109116
def wait_request(self, req_id):
110-
self.infer_requests[req_id].wait()
111-
return self.infer_requests[req_id].results[self.output_tensor]
117+
self.infer_queue[req_id].wait()
118+
return self.outputs.pop(req_id, None)
112119

113120

114121
class DummyDecoder:

demos/action_recognition_demo/python/action_recognition_demo/steps.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def __init__(self, encoder):
101101
self.encoder = encoder
102102
self.async_model = AsyncWrapper(self.encoder, self.encoder.num_requests)
103103

104+
def __del__(self):
105+
for ireq in self.encoder.infer_queue:
106+
ireq.cancel()
107+
104108
def process(self, frame):
105109
preprocessed = preprocess_frame(frame)
106110
preprocessed = preprocessed[np.newaxis, ...] # add batch dimension
@@ -121,6 +125,10 @@ def __init__(self, decoder, sequence_size=16):
121125
self.async_model = AsyncWrapper(self.decoder, self.decoder.num_requests)
122126
self._embeddings = deque(maxlen=self.sequence_size)
123127

128+
def __del__(self):
129+
for ireq in self.decoder.infer_queue:
130+
ireq.cancel()
131+
124132
def process(self, item):
125133
if item is None:
126134
return None

demos/gesture_recognition_demo/python/gesture_recognition_demo/common.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ def completion_callback(self, infer_request, id):
6868
def infer(self, data):
6969
"""Runs model on the specified input"""
7070

71-
self.async_infer(data, 0)
72-
return self.wait_request(0)
71+
input_data = {self.input_tensor_name: data}
72+
self.infer_queue[0].infer(input_data)
73+
return self.infer_queue[0].get_tensor(self.output_tensor_name).data[:]
7374

7475
def async_infer(self, data, req_id):
7576
"""Requests model inference for the specified input"""
@@ -79,8 +80,5 @@ def async_infer(self, data, req_id):
7980

8081
def wait_request(self, req_id):
8182
"""Waits for the model output by the specified request ID"""
82-
self.infer_queue.wait_all()
83-
try:
84-
return self.outputs.pop(req_id)
85-
except KeyError:
86-
return None
83+
self.infer_queue[req_id].wait()
84+
return self.outputs.pop(req_id, None)

0 commit comments

Comments
 (0)