11import cv2
2- import numpy as np
32import openvino as ov
43
4+ from lib .CVModel import CVModel
5+
56"""
67This 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
10344if __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 ())
0 commit comments