1- import os
2- import sys
31import cv2
4- import math
52import 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"""
107This 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
0 commit comments