|
| 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 | + |
0 commit comments