Skip to content

Commit 76e77c5

Browse files
authored
Add files via upload
1 parent f8787c7 commit 76e77c5

7 files changed

+712
-0
lines changed

src/face_detection.py

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import os
2+
import cv2
3+
import numpy as np
4+
from openvino.inference_engine import IENetwork, IECore
5+
6+
'''
7+
This is a sample class for a model. You may choose to use it as-is or make any changes to it.
8+
This has been provided just to give you an idea of how to structure your model class.
9+
'''
10+
11+
class FaceDetectionModel:
12+
'''
13+
Class for the Face Detection Model.
14+
'''
15+
def __init__(self, model_name, device='CPU', extensions=None):
16+
'''
17+
TODO: Use this to set your instance variables.
18+
'''
19+
self.model_name = model_name
20+
self.device = device
21+
self.extensions = extensions
22+
23+
self.model_structure = self.model_name
24+
self.model_weights = self.model_name.split('.')[0]+'.bin'
25+
26+
self.core = None
27+
self.network = None
28+
self.exec_net = None
29+
30+
self.input = None
31+
self.output = None
32+
self.mode = 'async'
33+
self.request_id = 0
34+
35+
def load_model(self):
36+
'''
37+
TODO: You will need to complete this method.
38+
This method is for loading the model to the device specified by the user.
39+
If your model requires any Plugins, this is where you can load them.
40+
'''
41+
self.core = IECore()
42+
43+
self.network = self.core.read_network(model=self.model_structure, weights=self.model_weights)
44+
self.exec_net = self.core.load_network(network=self.network, device_name=self.device,num_requests=self.num_requests)
45+
46+
self.input = next(iter(self.network.inputs))
47+
self.output = next(iter(self.network.outputs))
48+
49+
return self.exec_net
50+
51+
def predict(self, image, prob_threshold):
52+
'''
53+
TODO: You will need to complete this method.
54+
This method is meant for running predictions on the input image.
55+
'''
56+
processed_frame = self.preprocess_input(image)
57+
self.exec_net.start_async(request_id=self.request_id,
58+
inputs={self.input: processed_frame})
59+
self.exec_net.requests[0].get_perf_counts()
60+
61+
if self.mode == 'async':
62+
self.exec_net.requests[self.request_id].wait()
63+
result = self.exec_net.requests[self.request_id].outputs[self.output]
64+
croppedFace, box = self.preprocess_output(result[0][0], image,prob_threshold)
65+
return croppedFace, box
66+
else:
67+
if self.exec_net.requests[self.request_id].wait(-1) == 0:
68+
result = self.exec_net.requests[self.request_id].outputs[self.output]
69+
croppedFace, box = self.preprocess_output(result[0][0], image,prob_threshold)
70+
return croppedFace, box
71+
72+
def check_model(self):
73+
supported_layers = self.core.query_network(network=self.network, device_name=self.device)
74+
unsupported_layers = [l for l in self.network.layers.keys() if l not in supported_layers]
75+
76+
# if len(unsupported_layers) > 0:
77+
# print("Please check extention for these unsupported layers =>" + str(unsupported_layers))
78+
# exit(1)
79+
# print("All layers are supported !!")
80+
81+
if len(unsupported_layers)!=0 and self.device=='CPU':
82+
print("unsupported layers found:{}".format(unsupported_layers))
83+
if not self.extensions==None:
84+
print("Adding cpu_extension")
85+
self.core.add_extension(self.extensions, self.device)
86+
supported_layers = self.core.query_network(network = self.network, device_name=self.device)
87+
unsupported_layers = [l for l in self.network.layers.keys() if l not in supported_layers]
88+
if len(unsupported_layers)!=0:
89+
print("After adding the extension still unsupported layers found")
90+
exit(1)
91+
print("After adding the extension the issue is resolved")
92+
else:
93+
print("Give the path of cpu extension")
94+
exit(1)
95+
96+
97+
def preprocess_input(self, image):
98+
'''
99+
Before feeding the data into the model for inference,
100+
you might have to preprocess it. This function is where you can do that.
101+
'''
102+
model_input_shape = self.network.inputs[self.input].shape
103+
104+
image_resized = cv2.resize(image, (model_input_shape[3], model_input_shape[2]))
105+
p_frame = np.transpose(np.expand_dims(image_resized,axis=0), (0,3,1,2))
106+
return p_frame
107+
108+
def preprocess_output(self, outputs, prob_threshold):
109+
'''
110+
Before feeding the output of this model to the next model,
111+
you might have to preprocess the output. This function is where you can do that.
112+
'''
113+
coords =[]
114+
result = outputs[self.output_names][0][0]
115+
for res in result:
116+
conf = res[2]
117+
if conf>prob_threshold:
118+
x_min=res[3]
119+
y_min=res[4]
120+
x_max=res[5]
121+
y_max=res[6]
122+
coords.append([x_min,y_min,x_max,y_max])
123+
return coords

src/facial_landmarks_detection.py

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import os
2+
import cv2
3+
import numpy as np
4+
from openvino.inference_engine import IENetwork, IECore
5+
6+
'''
7+
This is a sample class for a model. You may choose to use it as-is or make any changes to it.
8+
This has been provided just to give you an idea of how to structure your model class.
9+
'''
10+
11+
class FaceLandmarkModel:
12+
'''
13+
Class for the Face Detection Model.
14+
'''
15+
def __init__(self, model_name, device='CPU', extensions=None):
16+
'''
17+
TODO: Use this to set your instance variables.
18+
'''
19+
self.model_name = model_name
20+
self.device = device
21+
self.extensions = extensions
22+
self.model_structure = self.model_name
23+
self.model_weights = self.model_name.split(".")[0]+'.bin'
24+
self.core = None
25+
self.network = None
26+
self.exec_net = None
27+
28+
self.input = None
29+
self.output = None
30+
self.mode = 'async'
31+
self.request_id = 0
32+
33+
def load_model(self):
34+
'''
35+
TODO: You will need to complete this method.
36+
This method is for loading the model to the device specified by the user.
37+
If your model requires any Plugins, this is where you can load them.
38+
'''
39+
self.core = IECore()
40+
41+
self.network = self.core.read_network(model=self.model_structure, weights=self.model_weights)
42+
self.exec_net = self.core.load_network(self.network, self.device)
43+
44+
self.input = next(iter(self.network.inputs))
45+
self.output = next(iter(self.network.outputs))
46+
47+
return self.exec_net
48+
49+
def predict(self, image, EYE_ROI):
50+
'''
51+
TODO: You will need to complete this method.
52+
This method is meant for running predictions on the input image.
53+
'''
54+
55+
processed_frame = self.preprocess_input(image)
56+
self.exec_net.start_async(request_id=self.request_id,inputs={self.input: processed_frame})
57+
self.exec_net.requests[0].get_perf_counts()
58+
59+
if self.mode == 'async':
60+
self.exec_net.requests[0].wait()
61+
result = self.exec_net.requests[0].outputs[self.output]
62+
return self.preprocess_output(result, image, EYE_ROI)
63+
64+
else:
65+
if self.exec_net.requests[0].wait(-1) == 0:
66+
result = self.exec_net.requests[0].outputs[self.output]
67+
return self.preprocess_output(result, image, EYE_ROI)
68+
69+
def check_model(self):
70+
supported_layers = self.core.query_network(network=self.network, device_name=self.device)
71+
unsupported_layers = [l for l in self.network.layers.keys() if l not in supported_layers]
72+
73+
# if len(unsupported_layers) > 0:
74+
# print("Please check extention for these unsupported layers =>" + str(unsupported_layers))
75+
# exit(1)
76+
# print("All layers are supported !!")
77+
78+
if len(unsupported_layers)!=0 and self.device=='CPU':
79+
print("unsupported layers found:{}".format(unsupported_layers))
80+
if not self.extensions==None:
81+
print("Adding cpu_extension")
82+
self.core.add_extension(self.extensions, self.device)
83+
supported_layers = self.core.query_network(network = self.network, device_name=self.device)
84+
unsupported_layers = [l for l in self.network.layers.keys() if l not in supported_layers]
85+
if len(unsupported_layers)!=0:
86+
print("After adding the extension still unsupported layers found")
87+
exit(1)
88+
print("After adding the extension the issue is resolved")
89+
else:
90+
print("Give the path of cpu extension")
91+
exit(1)
92+
93+
def preprocess_input(self, image):
94+
'''
95+
Before feeding the data into the model for inference,
96+
you might have to preprocess it. This function is where you can do that.
97+
'''
98+
99+
model_input_shape = self.network.inputs[self.input].shape
100+
101+
image_cvt = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
102+
image_resized = cv2.resize(image_cvt, (model_input_shape[3], model_input_shape[2]))
103+
p_frame = np.transpose(np.expand_dims(image_resized,axis=0), (0,3,1,2))
104+
return p_frame
105+
106+
def preprocess_output(self, outputs, image, EYE_ROI):
107+
'''
108+
Before feeding the output of this model to the next model,
109+
you might have to preprocess the output. This function is where you can do that.
110+
'''
111+
left_x = outputs[0][0].tolist()[0][0]
112+
left_y = outputs[0][1].tolist()[0][0]
113+
right_x = outputs[0][2].tolist()[0][0]
114+
right_y = outputs[0][3].tolist()[0][0]
115+
116+
box = (left_x, left_y, right_x, right_y)
117+
118+
h, w = image.shape[0:2]
119+
box = box * np.array([w, h, w, h])
120+
box = box.astype(np.int32)
121+
122+
(lefteye_x, lefteye_y, righteye_x, righteye_y) = box
123+
124+
leftxmin = lefteye_x - EYE_ROI
125+
leftymin = lefteye_y - EYE_ROI
126+
leftxmax = lefteye_x + EYE_ROI
127+
leftymax = lefteye_y + EYE_ROI
128+
129+
rightxmin = righteye_x - EYE_ROI
130+
rightymin = righteye_y - EYE_ROI
131+
rightxmax = righteye_x + EYE_ROI
132+
rightymax = righteye_y + EYE_ROI
133+
134+
left_eye = image[leftymin:leftymax, leftxmin:leftxmax]
135+
right_eye = image[rightymin:rightymax, rightxmin:rightxmax]
136+
eye_coords = [[leftxmin, leftymin, leftxmax, leftymax], [rightxmin, rightymin, rightxmax, rightymax]]
137+
138+
# cv2.rectangle(image,(lefteye_x,lefteye_y),(righteye_x,righteye_y),(255,0,0))
139+
return (lefteye_x, lefteye_y), (righteye_x, righteye_y), eye_coords, left_eye, right_eye
140+

src/gaze_estimation.py

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import cv2
2+
import numpy as np
3+
from openvino.inference_engine import IECore
4+
import math
5+
'''
6+
This is a sample class for a model. You may choose to use it as-is or make any changes to it.
7+
This has been provided just to give you an idea of how to structure your model class.
8+
'''
9+
10+
class EyeGazeModel:
11+
'''
12+
Class for the Face Detection Model.
13+
'''
14+
def __init__(self, model_name, device='CPU', extensions=None):
15+
'''
16+
TODO: Use this to set your instance variables.
17+
'''
18+
self.model_name = model_name
19+
self.device = device
20+
self.extensions = extensions
21+
22+
self.model_structure = self.model_name
23+
self.model_weights = self.model_name.split('.')[0]+'.bin'
24+
25+
self.core = None
26+
self.network = None
27+
self.exec_net = None
28+
29+
self.mode = 'async'
30+
self.request_id = 0
31+
32+
self.input_name = None
33+
self.input_shape = None
34+
self.output_names = None
35+
self.output_shape = None
36+
37+
def load_model(self):
38+
'''
39+
TODO: You will need to complete this method.
40+
This method is for loading the model to the device specified by the user.
41+
If your model requires any Plugins, this is where you can load them.
42+
'''
43+
self.core = IECore()
44+
45+
self.network = self.core.read_network(model=self.model_structure, weights=self.model_weights)
46+
self.exec_net = self.core.load_network(network=self.network, device_name=self.device,num_requests=self.num_requests)
47+
48+
self.input_name = [i for i in self.network.inputs.keys()]
49+
self.input_shape = self.network.inputs[self.input_name[1]].shape
50+
self.output_names = [i for i in self.network.outputs.keys()]
51+
52+
return self.exec_net
53+
54+
def predict(self, left_eye, right_eye, head_position):
55+
'''
56+
TODO: You will need to complete this method.
57+
This method is meant for running predictions on the input image.
58+
'''
59+
processed_left_eye = self.preprocess_input(left_eye)
60+
processed_right_eye = self.preprocess_input(right_eye)
61+
self.exec_net.start_async(request_id=self.request_id,
62+
inputs={'left_eye_image': processed_left_eye,
63+
'right_eye_image': processed_right_eye,
64+
'head_pose_angles': head_position})
65+
self.exec_net.requests[0].get_perf_counts()
66+
67+
if self.exec_net.requests[0].wait(-1) == 0:
68+
result = self.exec_net.requests[0].outputs[self.output]
69+
new_mouse_coord, gaze_vector = self.preprocess_output(result, head_position)
70+
return new_mouse_coord, gaze_vector
71+
72+
def check_model(self):
73+
supported_layers = self.core.query_network(network=self.network, device_name=self.device)
74+
unsupported_layers = [l for l in self.network.layers.keys() if l not in supported_layers]
75+
76+
# if len(unsupported_layers) > 0:
77+
# print("Please check extention for these unsupported layers =>" + str(unsupported_layers))
78+
# exit(1)
79+
# print("All layers are supported !!")
80+
81+
if len(unsupported_layers)!=0 and self.device=='CPU':
82+
print("unsupported layers found:{}".format(unsupported_layers))
83+
if not self.extensions==None:
84+
print("Adding cpu_extension")
85+
self.core.add_extension(self.extensions, self.device)
86+
supported_layers = self.core.query_network(network = self.network, device_name=self.device)
87+
unsupported_layers = [l for l in self.network.layers.keys() if l not in supported_layers]
88+
if len(unsupported_layers)!=0:
89+
print("After adding the extension still unsupported layers found")
90+
exit(1)
91+
print("After adding the extension the issue is resolved")
92+
else:
93+
print("Give the path of cpu extension")
94+
exit(1)
95+
96+
def preprocess_input(self, image):
97+
'''
98+
Before feeding the data into the model for inference,
99+
you might have to preprocess it. This function is where you can do that.
100+
'''
101+
102+
model_input_shape = self.network.inputs[self.input_name[1]].shape
103+
image_resized = cv2.resize(image, (model_input_shape[3], model_input_shape[2]))
104+
p_frame = np.transpose(np.expand_dims(image_resized,axis=0), (0,3,1,2))
105+
return p_frame
106+
107+
def preprocess_output(self, output, head_position):
108+
'''
109+
Before feeding the output of this model to the next model,
110+
you might have to preprocess the output. This function is where you can do that.
111+
'''
112+
gaze_vector = output[self.output_names[0]].tolist()[0]
113+
rollValue = hpa[2]
114+
cosValue = math.cos(rollValue * math.pi / 180.0)
115+
sinValue = math.sin(rollValue * math.pi / 180.0)
116+
117+
newx = gaze_vector[0] * cosValue + gaze_vector[1] * sinValue
118+
newy = -gaze_vector[0] * sinValue+ gaze_vector[1] * cosValue
119+
return (newx,newy), gaze_vector
120+
121+

0 commit comments

Comments
 (0)