Beispiel #1
0
        def process_data(self, pf):  #pf=ProcessingFrame
            cfg = pf.cfg.copy()

            frame_info = pf.frame_info
            filepath = frame_info.filepath

            if len(frame_info.landmarks_list) == 0:
                self.log_info(
                    f'no faces found for {filepath.name}, copying without faces'
                )
                if cfg.mode == 'raw-predict':
                    h, w, c = self.predictor_input_shape
                    img_bgr = np.zeros((h, w, 3), dtype=np.uint8)
                    img_mask = np.zeros((h, w, 1), dtype=np.uint8)
                else:
                    img_bgr = cv2_imread(filepath)
                    imagelib.normalize_channels(img_bgr, 3)
                    h, w, c = img_bgr.shape
                    img_mask = np.zeros((h, w, 1), dtype=img_bgr.dtype)

                cv2_imwrite(pf.output_filepath, img_bgr)
                cv2_imwrite(pf.output_mask_filepath, img_mask)

                if pf.need_return_image:
                    pf.image = np.concatenate([img_bgr, img_mask], axis=-1)

            else:
                if cfg.type == MergerConfig.TYPE_MASKED:
                    try:
                        final_img = MergeMasked(
                            self.predictor_func,
                            self.predictor_input_shape,
                            face_enhancer_func=self.face_enhancer_func,
                            xseg_256_extract_func=self.xseg_256_extract_func,
                            cfg=cfg,
                            frame_info=frame_info)
                    except Exception as e:
                        e_str = traceback.format_exc()
                        if 'MemoryError' in e_str:
                            raise Subprocessor.SilenceException
                        else:
                            raise Exception(
                                f'Error while merging file [{filepath}]: {e_str}'
                            )

                elif cfg.type == MergerConfig.TYPE_FACE_AVATAR:
                    final_img = MergeFaceAvatar(self.predictor_func,
                                                self.predictor_input_shape,
                                                cfg,
                                                pf.prev_temporal_frame_infos,
                                                pf.frame_info,
                                                pf.next_temporal_frame_infos)

                cv2_imwrite(pf.output_filepath, final_img[..., 0:3])
                cv2_imwrite(pf.output_mask_filepath, final_img[..., 3:4])

                if pf.need_return_image:
                    pf.image = final_img

            return pf
Beispiel #2
0
        def process_data(self, pf):  #pf=ProcessingFrame
            cfg = pf.cfg.copy()
            cfg.blursharpen_func = self.blursharpen_func
            cfg.superres_func = self.superres_func

            frame_info = pf.frame_info

            filepath = frame_info.filepath
            landmarks_list = frame_info.landmarks_list

            output_filepath = pf.output_filepath
            need_return_image = pf.need_return_image

            if len(landmarks_list) == 0:
                self.log_info('no faces found for %s, copying without faces' %
                              (filepath.name))

                if cfg.export_mask_alpha:
                    img_bgr = cv2_imread(filepath)
                    h, w, c = img_bgr.shape
                    if c == 1:
                        img_bgr = np.repeat(img_bgr, 3, -1)
                    if c == 3:
                        img_bgr = np.concatenate([
                            img_bgr,
                            np.zeros((h, w, 1), dtype=img_bgr.dtype)
                        ],
                                                 axis=-1)

                    cv2_imwrite(output_filepath, img_bgr)
                else:
                    if filepath.suffix == '.png':
                        shutil.copy(str(filepath), str(output_filepath))
                    else:
                        img_bgr = cv2_imread(filepath)
                        cv2_imwrite(output_filepath, img_bgr)

                if need_return_image:
                    img_bgr = cv2_imread(filepath)
                    pf.image = img_bgr
            else:
                if cfg.type == MergerConfig.TYPE_MASKED:
                    cfg.fanseg_input_size = self.fanseg_input_size
                    cfg.fanseg_extract_func = self.fanseg_extract_func

                    try:
                        final_img = MergeMasked(self.predictor_func,
                                                self.predictor_input_shape,
                                                cfg, frame_info)
                    except Exception as e:
                        e_str = traceback.format_exc()
                        if 'MemoryError' in e_str:
                            raise Subprocessor.SilenceException
                        else:
                            raise Exception(
                                f'Error while merging file [{filepath}]: {e_str}'
                            )

                elif cfg.type == MergerConfig.TYPE_FACE_AVATAR:
                    final_img = MergeFaceAvatar(self.predictor_func,
                                                self.predictor_input_shape,
                                                cfg,
                                                pf.prev_temporal_frame_infos,
                                                pf.frame_info,
                                                pf.next_temporal_frame_infos)

                if output_filepath is not None and final_img is not None:
                    cv2_imwrite(output_filepath, final_img)

                if need_return_image:
                    pf.image = final_img

            return pf