Skip to content

Commit 815197d

Browse files
committed
fix: gaze estimation inference
fixed and updated gaze estimation inference code according to the new library version
1 parent 222ff0f commit 815197d

File tree

3 files changed

+50
-85
lines changed

3 files changed

+50
-85
lines changed

lib/gaze_estimator.py

Lines changed: 36 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import os
2-
import sys
31
import cv2
4-
import math
52
import numpy as np
6-
import logging as log
7-
from openvino.inference_engine import IECore
3+
import openvino as ov
4+
import math
85

96
"""
107
This is a sample class for a model. You may choose to use it as-is or make any changes to it.
@@ -16,15 +13,12 @@ class GazeEstimator:
1613
Class for the Gaze Estimation Model.
1714
"""
1815

19-
def __init__(self, model_name, device="CPU", extensions=None):
16+
def __init__(self, model_name, device="CPU"):
2017
"""Use this to set your instance variables."""
2118
self.core = None
22-
self.network = None
23-
self.output_blob = None
24-
self.exec_network = None
19+
self.compiled_model = None
2520
self.infer_request = None
2621
self.device = device
27-
self.extenstions = extensions
2822
self.model_xml = model_name
2923

3024
def load_model(self):
@@ -37,34 +31,12 @@ def load_model(self):
3731
Read model in to IENetwork and add any necessary Extension
3832
Check for Supported Layers
3933
"""
40-
self.model_weights = os.path.splitext(self.model_xml)[0] + ".bin"
4134
### Load the model ###
4235
# Take model .xml and .bin file and create IENetwork
43-
self.core = IECore()
44-
self.network = self.core.read_network(
45-
model=self.model_xml, weights=self.model_weights
46-
)
47-
48-
### Add any necessary extensions ###
49-
try:
50-
if self.extenstions and self.device == "CPU":
51-
self.core.add_extension(
52-
extension_path=self.extenstions, device_name=self.device
53-
)
54-
except:
55-
"Error in Loading {} Extension".format(self.device)
56-
57-
### Check model for unsupported layers
58-
self.check_model()
59-
60-
# retrieve name of model's output layer
61-
self.output_blob = next(iter(self.network.outputs))
36+
self.core = ov.Core()
37+
self.compiled_model = self.core.compile_model(self.model_xml, "AUTO")
6238

63-
### load IENetwork to Executable Network ###
64-
### Note: You may need to update the function parameters. ###
65-
self.exec_network = self.core.load_network(
66-
network=self.network, device_name=self.device, num_requests=1
67-
)
39+
self.infer_request = self.compiled_model.create_infer_request()
6840

6941
def predict(self, left_eye, right_eye, head_pose):
7042
"""
@@ -80,50 +52,40 @@ def predict(self, left_eye, right_eye, head_pose):
8052
### PreProcess input image according to model Requirement
8153
left_eye = self.preprocess_input(left_eye)
8254
right_eye = self.preprocess_input(right_eye)
55+
left_eye = ov.Tensor(array=left_eye, shared_memory=False)
56+
right_eye = ov.Tensor(array=right_eye, shared_memory=False)
8357
### run inference and return output
8458
# Start Async Inference Request
85-
poses = [
59+
poses = np.array(
8660
[
87-
head_pose["angle_y_fc"][0][0], # Estimated Head yaw (in degrees)
88-
head_pose["angle_p_fc"][0][0], # Estimated Head pitch (in degrees)
89-
head_pose["angle_r_fc"][0][0], # Estimated Head roll (in degrees)
90-
]
91-
]
61+
[
62+
head_pose["yaw"], # Estimated Head yaw (in degrees)
63+
head_pose["pitch"], # Estimated Head pitch (in degrees)
64+
head_pose["role"], # Estimated Head roll (in degrees)
65+
]
66+
],
67+
dtype=np.float32,
68+
)
69+
poses = ov.Tensor(array=poses, shared_memory=False)
70+
print("++++", poses.shape)
9271

93-
# print("aksdfjdskfajskdfjasdfk", v)
94-
infer_request_handle = self.exec_network.start_async(
95-
request_id=0,
96-
# Input dictionary
97-
inputs={
72+
self.infer_request.set_input_tensors(
73+
{
9874
# image of left eye
99-
"left_eye_image": left_eye,
75+
0: left_eye,
10076
# image of right eye
101-
"right_eye_image": right_eye,
77+
1: right_eye,
10278
# head pose angles
103-
"head_pose_angles": poses,
104-
},
79+
2: poses,
80+
}
10581
)
106-
107-
# wait for the output and return.
108-
if infer_request_handle.wait(-1) == 0:
109-
model_output = infer_request_handle.outputs[self.output_blob]
110-
return model_output
111-
112-
def check_model(self):
113-
"""Check for supported layers"""
114-
layers_map = self.core.query_network(
115-
network=self.network, device_name=self.device
116-
)
117-
118-
unsupported_layers = [
119-
l for l in self.network.layers.keys() if l not in layers_map
120-
]
121-
122-
if unsupported_layers != []:
123-
sys.exit(
124-
"Those mention layers in your model are not supported by OpenVino Inference Engine:"
125-
" \n\t" + "\n\t".join(unsupported_layers)
126-
)
82+
# run inference
83+
self.infer_request.start_async()
84+
self.infer_request.wait()
85+
# Get output tensor for model with one output
86+
output = self.infer_request.get_output_tensor()
87+
output_buffer = output.data
88+
return output_buffer
12789

12890
def preprocess_input(self, image):
12991
"""
@@ -133,7 +95,8 @@ def preprocess_input(self, image):
13395
(b, c, h, w) = self.get_input_shape()
13496
image = cv2.resize(image, (w, h))
13597
image = np.transpose(image, (2, 0, 1))
136-
image = image.reshape(b, c, h, w)
98+
image = np.expand_dims(image, axis=0)
99+
image = image.astype(np.float32)
137100

138101
return image
139102

@@ -161,4 +124,4 @@ def preprocess_output(self, outputs, hpe_cords):
161124

162125
def get_input_shape(self):
163126
"""Return the shape of the input layer"""
164-
return self.network.inputs["left_eye_image"].shape
127+
return self.compiled_model.inputs[0].shape

lib/head_pose_estimator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def predict(self, image):
4848
Returns:
4949
model_output (numpy.ndarray): Raw Model Output
5050
"""
51+
model_output = {}
5152
### PreProcess input image according to model Requirement
5253
input_img = self.preprocess_input(image)
5354
input_tensor = ov.Tensor(input_img, shared_memory=False)
@@ -57,8 +58,9 @@ def predict(self, image):
5758
self.infer_request.start_async()
5859
self.infer_request.wait()
5960

60-
model_output = self.infer_request.get_output_tensor(0).data
61-
61+
model_output["yaw"] = self.infer_request.get_output_tensor(0).data[0][0]
62+
model_output["pitch"] = self.infer_request.get_output_tensor(1).data[0][0]
63+
model_output["role"] = self.infer_request.get_output_tensor(2).data[0][0]
6264
return model_output
6365

6466
def preprocess_input(self, image):

main.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from lib.face_detector import FaceDetector
1313
from lib.head_pose_estimator import HeadposeEstimator
1414
from lib.landmark_detector import LandmarkDetector
15-
# from lib.gaze_estimator import GazeEstimator
15+
from lib.gaze_estimator import GazeEstimator
1616
from lib.input_feeder import InputFeeder
1717

1818
# from lib.mouse_controller import MouseController
@@ -56,7 +56,7 @@ def build_argparser():
5656
help="Path to an xml file of Head Pose Estimation model.")
5757
parser.add_argument("-gem",
5858
"--g_est_m",
59-
required=False,
59+
required=True,
6060
type=str,
6161
help="Path to an xml file of Gaze Estimation Model.")
6262
parser.add_argument("-i",
@@ -108,7 +108,7 @@ def infer_on_stream(args):
108108
"""
109109
# Check if all input files are present
110110
for _ in [
111-
args.face_det_m,# args.lmar_det_m, args.h_pose_m, args.g_est_m,
111+
args.face_det_m, args.lmar_det_m, args.h_pose_m, args.g_est_m,
112112
args.input
113113
]:
114114
if not Path(_).is_file():
@@ -163,11 +163,10 @@ def infer_on_stream(args):
163163
landmark_detector.load_model()
164164
logger.info("Landmark Detector model loaded successfully")
165165

166-
# ## Load Gaze Estimation Model
167-
# gaze_estimator = GazeEstimator(args.g_est_m, args.device,
168-
# args.cpu_extension)
169-
# gaze_estimator.load_model()
170-
# logger.info("Gaze Estimation model loaded successfully")
166+
## Load Gaze Estimation Model
167+
gaze_estimator = GazeEstimator(args.g_est_m, args.device)
168+
gaze_estimator.load_model()
169+
logger.info("Gaze Estimation model loaded successfully")
171170
### Initialize Input Feeder
172171
input_feeder = InputFeeder(input_type, args.input)
173172
(initial_w, initial_h) = input_feeder.load_data()
@@ -212,6 +211,7 @@ def infer_on_stream(args):
212211
### Estimate Gaze
213212
gaze = gaze_estimator.predict(left_eye, right_eye, head_pose)
214213
logger.info("Gaze Estimated successfully")
214+
break
215215
## Get mouse coords (x, y)
216216
mouse_coords = gaze_estimator.preprocess_output(gaze, head_pose)
217217
logger.info("New mouse coordinates: {}".format(mouse_coords))
@@ -288,5 +288,5 @@ def main():
288288
logger.info("Every Thing Complete Exiting Program")
289289

290290

291-
if __name__ == '__main__':
292-
main()
291+
if __name__ == "__main__":
292+
main()

0 commit comments

Comments
 (0)