Skip to content

Commit 1fa32f9

Browse files
authored
Merge pull request #2 from Ahwar/docs-and-refactor
ref: Docs and Refactor Improvements
2 parents ce60aac + f6d77ab commit 1fa32f9

File tree

11 files changed

+241
-444
lines changed

11 files changed

+241
-444
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"python": "D:/ahar/Computer-Pointer-Controller-with-Eyes/.venv/Scripts/python.exe",
1414
"args": [
1515
"-i",
16-
"bin/face.png",
16+
// "bin/face.png", // for single image
17+
"CAM",
1718
"-ftm",
1819
"bin/models/face-detection-retail-0004.xml",
1920
"-ldm",

README.md

Lines changed: 83 additions & 201 deletions
Large diffs are not rendered by default.

download_models.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
curl --create-dirs https://storage.openvinotoolkit.org/repositories/open_model_zoo/2023.0/models_bin/1/face-detection-retail-0004/FP32/face-detection-retail-0004.xml -o bin/models/face-detection-retail-0004.xml
2+
curl https://storage.openvinotoolkit.org/repositories/open_model_zoo/2023.0/models_bin/1/face-detection-retail-0004/FP32/face-detection-retail-0004.bin -o bin/models/face-detection-retail-0004.bin
3+
4+
curl https://storage.openvinotoolkit.org/repositories/open_model_zoo/2023.0/models_bin/1/landmarks-regression-retail-0009/FP32/landmarks-regression-retail-0009.xml -o bin/models/landmarks_regression_retail_0009.xml
5+
curl https://storage.openvinotoolkit.org/repositories/open_model_zoo/2023.0/models_bin/1/landmarks-regression-retail-0009/FP32/landmarks-regression-retail-0009.bin -o bin/models/landmarks_regression_retail_0009.bin
6+
7+
8+
curl https://storage.openvinotoolkit.org/repositories/open_model_zoo/2023.0/models_bin/1/head-pose-estimation-adas-0001/FP32/head-pose-estimation-adas-0001.xml -o bin/models/head-pose-estimation-adas-0001.xml
9+
curl https://storage.openvinotoolkit.org/repositories/open_model_zoo/2023.0/models_bin/1/head-pose-estimation-adas-0001/FP32/head-pose-estimation-adas-0001.bin -o bin/models/head-pose-estimation-adas-0001.bin
10+
11+
curl https://storage.openvinotoolkit.org/repositories/open_model_zoo/2023.0/models_bin/1/gaze-estimation-adas-0002/FP32/gaze-estimation-adas-0002.xml -o bin/models/gaze-estimation-adas-0002.xml
12+
curl https://storage.openvinotoolkit.org/repositories/open_model_zoo/2023.0/models_bin/1/gaze-estimation-adas-0002/FP32/gaze-estimation-adas-0002.bin -o bin/models/gaze-estimation-adas-0002.bin

lib/CVModel.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import cv2
2+
import numpy as np
3+
import openvino as ov
4+
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+
"""
8+
9+
10+
class CVModel:
11+
"""
12+
Super Class for Running Computer vision Models.
13+
"""
14+
15+
def __init__(self, model_name, device="CPU"):
16+
self.core = None
17+
self.compiled_model = None
18+
self.infer_request = None
19+
self.device = device
20+
self.model_xml = model_name
21+
22+
def load_model(self):
23+
"""
24+
Load Model file and create Executable Network
25+
26+
This method is for loading the model to the device specified by the user.
27+
If your model requires any Plugins (e.g. CPU extensions), this is where you can load them.
28+
Initialize Core
29+
Read model in to IENetwork and add any necessary Extension
30+
Check for Supported Layers
31+
"""
32+
### Load the model ###
33+
# Take model .xml and .bin file and create IENetwork
34+
self.core = ov.Core()
35+
self.compiled_model = self.core.compile_model(self.model_xml, "AUTO")
36+
37+
self.infer_request = self.compiled_model.create_infer_request()
38+
39+
def preprocess_input(self, image):
40+
"""
41+
Before feeding the data into the model for inference,
42+
you might have to preprocess it. This function is where you can do that.
43+
"""
44+
(b, c, h, w) = self.get_input_shape()
45+
image = cv2.resize(image, (w, h))
46+
image = np.transpose(image, (2, 0, 1))
47+
image = np.expand_dims(image, axis=0)
48+
image = image.astype(np.float32)
49+
50+
return image
51+
52+
def predict(self, image):
53+
"""
54+
Perform Inference on Image and return Raw Output.
55+
56+
This method is meant for running predictions on the input image.
57+
Parameters:
58+
image (numpy.ndarray): Frame from input file
59+
"""
60+
61+
### PreProcess input image according to model Requirement
62+
input_img = self.preprocess_input(image)
63+
input_tensor = ov.Tensor(input_img, shared_memory=False)
64+
### run inference and return output
65+
# Start Async Inference Request
66+
self.infer_request.set_input_tensor(input_tensor)
67+
self.infer_request.start_async()
68+
self.infer_request.wait()
69+
70+
def get_input_shape(self):
71+
"""Return the shape of the input layer"""
72+
return self.compiled_model.inputs[0].shape

lib/face_detector.py

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,21 @@
11
import cv2
2-
import numpy as np
32
import openvino as ov
43

4+
from lib.CVModel import CVModel
5+
56
"""
67
This is a sample class for a model. You may choose to use it as-is or make any changes to it.
78
"""
89

910

10-
class FaceDetector:
11-
"""
12-
Class for the Face Detection Model.
13-
"""
14-
15-
def __init__(self, model_name, device="CPU"):
16-
"""Use this to set your instance variables."""
17-
self.core = None
18-
self.compiled_model = None
19-
self.infer_request = None
20-
self.device = device
21-
self.model_xml = model_name
22-
23-
def load_model(self):
24-
"""
25-
Load Model file and create Executable Network
26-
27-
This method is for loading the model to the device specified by the user.
28-
If your model requires any Plugins (e.g. CPU extensions), this is where you can load them.
29-
Initialize Core
30-
Read model in to IENetwork and add any necessary Extension
31-
Check for Supported Layers
32-
"""
33-
### Load the model ###
34-
# Take model .xml and .bin file and create IENetwork
35-
self.core = ov.Core()
36-
self.compiled_model = self.core.compile_model(self.model_xml, "AUTO")
37-
38-
self.infer_request = self.compiled_model.create_infer_request()
11+
class FaceDetector(CVModel):
3912

4013
def predict(self, image):
41-
"""
42-
Perform Inference on Image and return Raw Output.
43-
44-
This method is meant for running predictions on the input image.
45-
Parameters:
46-
image (numpy.ndarray): Frame from input file
47-
48-
Returns:
49-
model_output (numpy.ndarray): Raw Model Output
50-
"""
51-
52-
### PreProcess input image according to model Requirement
53-
input_img = self.preprocess_input(image)
54-
input_tensor = ov.Tensor(input_img, shared_memory=False)
55-
### run inference and return output
56-
# Start Async Inference Request
57-
self.infer_request.set_input_tensor(input_tensor)
58-
self.infer_request.start_async()
59-
self.infer_request.wait()
14+
super().predict(image=image)
6015

6116
model_output = self.infer_request.get_output_tensor().data
62-
6317
return model_output
6418

65-
def preprocess_input(self, image):
66-
"""
67-
Before feeding the data into the model for inference,
68-
you might have to preprocess it. This function is where you can do that.
69-
"""
70-
(b, c, h, w) = self.get_input_shape()
71-
image = cv2.resize(image, (w, h))
72-
image = np.transpose(image, (2, 0, 1))
73-
image = np.expand_dims(image, axis=0)
74-
image = image.astype(np.float32)
75-
76-
return image
77-
7819
def postprocess_output(self, outputs, threshold, image, image_w, image_h):
7920
"""
8021
Before feeding the output of this model to the next model,
@@ -101,7 +42,7 @@ def get_input_shape(self):
10142

10243

10344
if __name__ == "__main__":
104-
detector = FaceDetector("bin/models/1/face-detection-retail-0004.xml")
45+
detector = FaceDetector("bin/models/face-detection-retail-0004.xml")
10546
detector.load_model()
10647
image = cv2.imread("bin/face.png")
10748
print("Model Input shape:", detector.get_input_shape())

lib/gaze_estimator.py

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,15 @@
1-
import cv2
21
import numpy as np
32
import openvino as ov
43
import math
54

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-
"""
5+
from lib.CVModel import CVModel
96

107

11-
class GazeEstimator:
8+
class GazeEstimator(CVModel):
129
"""
1310
Class for the Gaze Estimation Model.
1411
"""
1512

16-
def __init__(self, model_name, device="CPU"):
17-
"""Use this to set your instance variables."""
18-
self.core = None
19-
self.compiled_model = None
20-
self.infer_request = None
21-
self.device = device
22-
self.model_xml = model_name
23-
24-
def load_model(self):
25-
"""
26-
Load Model file and create Executable Network
27-
28-
This method is for loading the model to the device specified by the user.
29-
If your model requires any Plugins (e.g. CPU extensions), this is where you can load them.
30-
Initialize Core
31-
Read model in to IENetwork and add any necessary Extension
32-
Check for Supported Layers
33-
"""
34-
### Load the model ###
35-
# Take model .xml and .bin file and create IENetwork
36-
self.core = ov.Core()
37-
self.compiled_model = self.core.compile_model(self.model_xml, "AUTO")
38-
39-
self.infer_request = self.compiled_model.create_infer_request()
40-
4113
def predict(self, left_eye, right_eye, head_pose):
4214
"""
4315
Perform Inference on Image and return Raw Output.
@@ -67,7 +39,6 @@ def predict(self, left_eye, right_eye, head_pose):
6739
dtype=np.float32,
6840
)
6941
poses = ov.Tensor(array=poses, shared_memory=False)
70-
print("++++", poses.shape)
7142

7243
self.infer_request.set_input_tensors(
7344
{
@@ -87,19 +58,6 @@ def predict(self, left_eye, right_eye, head_pose):
8758
output_buffer = output.data
8859
return output_buffer
8960

90-
def preprocess_input(self, image):
91-
"""
92-
Before feeding the data into the model for inference,
93-
you might have to preprocess it. This function is where you can do that.
94-
"""
95-
(b, c, h, w) = self.get_input_shape()
96-
image = cv2.resize(image, (w, h))
97-
image = np.transpose(image, (2, 0, 1))
98-
image = np.expand_dims(image, axis=0)
99-
image = image.astype(np.float32)
100-
101-
return image
102-
10361
def preprocess_output(self, outputs, hpe_cords):
10462
"""
10563
Model output is dictionary like this
@@ -121,7 +79,3 @@ def preprocess_output(self, outputs, hpe_cords):
12179
except Exception as e:
12280
print("Error While preprocessing output in Gaze Estimation Model" + str(e))
12381
return mouse_cord
124-
125-
def get_input_shape(self):
126-
"""Return the shape of the input layer"""
127-
return self.compiled_model.inputs[0].shape

lib/head_pose_estimator.py

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,15 @@
1-
import cv2
2-
import numpy as np
3-
import openvino as ov
1+
from lib.CVModel import CVModel
42

53
"""
64
This is a sample class for a model. You may choose to use it as-is or make any changes to it.
75
"""
86

97

10-
class HeadposeEstimator:
8+
class HeadposeEstimator(CVModel):
119
"""
1210
Class for the Face Detection Model.
1311
"""
1412

15-
def __init__(self, model_name, device="CPU"):
16-
"""Use this to set your instance variables."""
17-
self.core = None
18-
self.compiled_model = None
19-
self.infer_request = None
20-
self.device = device
21-
self.model_xml = model_name
22-
23-
def load_model(self):
24-
"""
25-
Load Model file and create Executable Network
26-
27-
This method is for loading the model to the device specified by the user.
28-
If your model requires any Plugins (e.g. CPU extensions), this is where you can load them.
29-
Initialize Core
30-
Read model in to IENetwork and add any necessary Extension
31-
Check for Supported Layers
32-
"""
33-
### Load the model ###
34-
# Take model .xml and .bin file and create IENetwork
35-
self.core = ov.Core()
36-
self.compiled_model = self.core.compile_model(self.model_xml, "AUTO")
37-
38-
self.infer_request = self.compiled_model.create_infer_request()
39-
4013
def predict(self, image):
4114
"""
4215
Perform Inference on Image and return Raw Output.
@@ -48,34 +21,9 @@ def predict(self, image):
4821
Returns:
4922
model_output (numpy.ndarray): Raw Model Output
5023
"""
24+
super().predict(image)
5125
model_output = {}
52-
### PreProcess input image according to model Requirement
53-
input_img = self.preprocess_input(image)
54-
input_tensor = ov.Tensor(input_img, shared_memory=False)
55-
### run inference and return output
56-
# Start Async Inference Request
57-
self.infer_request.set_input_tensor(input_tensor)
58-
self.infer_request.start_async()
59-
self.infer_request.wait()
60-
6126
model_output["yaw"] = self.infer_request.get_output_tensor(0).data[0][0]
6227
model_output["pitch"] = self.infer_request.get_output_tensor(1).data[0][0]
6328
model_output["role"] = self.infer_request.get_output_tensor(2).data[0][0]
6429
return model_output
65-
66-
def preprocess_input(self, image):
67-
"""
68-
Before feeding the data into the model for inference,
69-
you might have to preprocess it. This function is where you can do that.
70-
"""
71-
(b, c, h, w) = self.get_input_shape()
72-
image = cv2.resize(image, (w, h))
73-
image = np.transpose(image, (2, 0, 1))
74-
image = np.expand_dims(image, axis=0)
75-
image = image.astype(np.float32)
76-
77-
return image
78-
79-
def get_input_shape(self):
80-
"""Return the shape of the input layer"""
81-
return self.compiled_model.inputs[0].shape

0 commit comments

Comments
 (0)