예제 #1
0
class ProcessFrame:
    def __init__(self, args: Dict):
        # todo proper handling of self.modes
        self.ie = IECore()
        self.modes = self.__determine_processing_mode(args)
        net_face_detect = net_landmarks_detect = net_recognize_face = None

        if not self.modes['detect']:
            raise ValueError('detection model undefined')
        # load networks from file
        net_face_detect = self.__prepare_network(args['detection_model'])
        # put it to corresponding class
        # self.face_locator = FaceLocator(net_face_detect, args['detection_model_threshold'])
        self.face_locator = FaceLocator(net_face_detect,
                                        args['detection_model_threshold'],
                                        NetworkType(args['detection_model']))
        # setup device plugins
        if next(iter(args['device'])) == 'CPU':
            # CPU
            self.ie.set_config(config={
                "CPU_THROUGHPUT_STREAMS": "1",
                "CPU_THREADS_NUM": "8",
            },
                               device_name='CPU')
        elif next(iter(args['device'])) == 'GPU':
            # GPU
            pass
            self.ie.set_config(config={"GPU_THROUGHPUT_STREAMS": "1"},
                               device_name='GPU')
        elif next(iter(args['device'])) == 'MYRIAD':
            pass
        # load to device for inferencing
        self.face_locator.deploy_network(next(iter(args['device'])), self.ie)

        if self.modes['landmark']:
            net_landmarks_detect = self.__prepare_network(
                args['landmarks_model'])
            self.landmarks_locator = LandmarksLocator(net_landmarks_detect)
            self.landmarks_locator.deploy_network(next(iter(args['device'])),
                                                  self.ie)

        if self.modes['recognize']:
            net_recognize_face = self.__prepare_network(
                args['recognition_model'])
            self.face_recognizer = FaceRecognizer(net_recognize_face)
            self.face_recognizer.deploy_network(next(iter(args['device'])),
                                                self.ie)

        # todo other models or load separately

    @staticmethod
    def __determine_processing_mode(args: Dict) -> Dict[str, bool]:
        ret = {}
        if args['detection_model']:
            ret['detect'] = True
        else:
            ret['detect'] = False

        if args['landmarks_model']:
            ret['landmark'] = True
        else:
            ret['landmark'] = False

        if args['recognition_model']:
            ret['recognize'] = True
        else:
            ret['recognize'] = False
        return ret

    def __prepare_network(self, model_path: str) -> IENetwork:
        model_path = os.path.abspath(model_path)
        model = self.ie.read_network(model=model_path,
                                     weights=os.path.splitext(model_path)[0] +
                                     ".bin")
        return model

    def process_frame(
        self, frame: np.ndarray
    ) -> List[
            Union[List[FaceLocator.FacePosition],
                  List[LandmarksLocator.FaceLandmarks],
                  List[FaceRecognizer.FaceIdentity]]]:  # todo uniton with None
        faces_landmarks = faces_identities = None
        face_positions = self.face_locator.get_face_positions(frame)
        if self.modes['landmark']:
            faces_landmarks = self.landmarks_locator.get_landmarks(
                frame, face_positions)
        if self.modes['recognize']:
            faces_identities = self.face_recognizer.get_identities(
                frame, face_positions, faces_landmarks)
        return [face_positions, faces_landmarks, faces_identities]