コード例 #1
0
    def preprocess(self, data):
        preprocessed_data = []
        data_infos = []
        for img in data:
            img = np.array(img)[:, :, ::-1]
            height, width, _ = img.shape
            if self.input_height / self.input_width < height / width:
                scale = self.input_height / height
            else:
                scale = self.input_width / width

            scaled_img = cv2.resize(
                img, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC,
            )
            padded_img, pad = pad_img(
                scaled_img, (0, 0, 0), [self.input_height, self.input_width],
            )

            padded_img = padded_img.transpose((2, 0, 1))
            padded_img = padded_img[np.newaxis].astype(np.float32)
            preprocessed_data.append(padded_img)
            data_infos.append(
                DataInfo(
                    scales=(scale, scale),
                    pads=tuple(pad),
                    original_width=width,
                    original_height=height,
                ),
            )

        return [preprocessed_data, data_infos]
コード例 #2
0
ファイル: model.py プロジェクト: opencv-ai/oak-model-samples
    def preprocess(self, data):
        face_bboxes = self.get_first_stage_result(data)
        preprocessed_data = []
        preprocessed_bboxes = []
        if face_bboxes == [[]]:
            return None
        for i, img in enumerate(data):
            areas = [(bbox.y2 - bbox.y1) * (bbox.x2 - bbox.x1)
                     for bbox in face_bboxes[i]]
            max_area = max(areas)
            preprocessed_img = []
            img_bboxes = []
            img = np.array(img)[:, :, ::-1]
            for j, face_bbox in enumerate(face_bboxes[i]):
                if areas[j] < self.area_threshold * max_area:
                    continue
                if face_bbox.x2 - face_bbox.x1 < self.input_width:
                    continue

                cropped_face = img[face_bbox.y1:face_bbox.y2,
                                   face_bbox.x1:face_bbox.x2, ]
                height, width, _ = cropped_face.shape
                if self.input_height / self.input_width < height / width:
                    scale = self.input_height / height
                else:
                    scale = self.input_width / width

                scaled_img = cv2.resize(
                    cropped_face,
                    (0, 0),
                    fx=scale,
                    fy=scale,
                    interpolation=cv2.INTER_CUBIC,
                )
                padded_img, pad = pad_img(
                    scaled_img,
                    (0, 0, 0),
                    [self.input_height, self.input_width],
                )

                padded_img = padded_img.transpose((2, 0, 1))
                padded_img = padded_img[np.newaxis].astype(np.float32)
                preprocessed_img.append(padded_img)
                img_bboxes.append(face_bbox)
            preprocessed_data.append(preprocessed_img)
            preprocessed_bboxes.append(img_bboxes)

        return [preprocessed_data, preprocessed_bboxes]
コード例 #3
0
ファイル: model.py プロジェクト: hernymtz/oak-model-samples
    def preprocess(self, data):
        face_bboxes = self.get_first_stage_result(data)
        preprocessed_data = []
        preprocessed_bboxes = []
        if face_bboxes == [[]]:
            return None
        for i, img in enumerate(data):
            areas = [
                (bbox.y2 - bbox.y1) * (bbox.x2 - bbox.x1) for bbox in face_bboxes[i]
            ]
            max_area = max(areas)
            preprocessed_img = []
            img_bboxes = []
            img = np.array(img)[:, :, ::-1]
            for j, face_bbox in enumerate(face_bboxes[i]):
                if areas[j] < self.area_threshold * max_area:
                    continue
                # apply padding to face bbox to increase quality of facial landmark model
                face_bbox_width = face_bbox.x2 - face_bbox.x1
                face_bbox_hight = face_bbox.y2 - face_bbox.y1
                face_bbox.x1 = int(
                    np.clip(
                        face_bbox.x1 - face_bbox_width * self.face_bbox_pad_percent,
                        0,
                        img.shape[1],
                    ),
                )
                face_bbox.y2 = int(
                    np.clip(
                        face_bbox.y2 + face_bbox_hight * self.face_bbox_pad_percent,
                        0,
                        img.shape[0],
                    ),
                )
                face_bbox.x2 = int(
                    np.clip(
                        face_bbox.x2 + face_bbox_width * self.face_bbox_pad_percent,
                        0,
                        img.shape[1],
                    ),
                )
                cropped_face = img[
                    face_bbox.y1 : face_bbox.y2, face_bbox.x1 : face_bbox.x2,
                ]
                height, width, _ = cropped_face.shape
                if self.input_height / self.input_width < height / width:
                    scale = self.input_height / height
                else:
                    scale = self.input_width / width

                scaled_img = cv2.resize(
                    cropped_face,
                    (0, 0),
                    fx=scale,
                    fy=scale,
                    interpolation=cv2.INTER_CUBIC,
                )
                padded_img, pad = pad_img(
                    scaled_img, (0, 0, 0), [self.input_height, self.input_width],
                )

                padded_img = padded_img.transpose((2, 0, 1))
                padded_img = padded_img[np.newaxis].astype(np.float32)
                preprocessed_img.append(padded_img)
                img_bboxes.append(face_bbox)
            preprocessed_data.append(preprocessed_img)
            preprocessed_bboxes.append(img_bboxes)

        return [preprocessed_data, preprocessed_bboxes]