From 9c673e6b4df8d5ae91606b1d371881f2474a2f45 Mon Sep 17 00:00:00 2001 From: lijianshe02 <48898730+lijianshe02@users.noreply.github.com> Date: Wed, 11 Nov 2020 20:41:03 +0800 Subject: [PATCH] refine duplicate frames and fix some bugs (#81) * refine duplicate frames and fix some bugs --- docs/en_US/tutorials/psgan.md | 4 ++-- docs/zh_CN/tutorials/psgan.md | 4 ++-- ppgan/apps/dain_predictor.py | 12 +++++++++++- ppgan/apps/psgan_predictor.py | 21 +++++++-------------- ppgan/models/makeup_model.py | 1 + tools/psgan_infer.py | 7 +++++++ 6 files changed, 30 insertions(+), 19 deletions(-) diff --git a/docs/en_US/tutorials/psgan.md b/docs/en_US/tutorials/psgan.md index c5046bf..278ee45 100644 --- a/docs/en_US/tutorials/psgan.md +++ b/docs/en_US/tutorials/psgan.md @@ -33,8 +33,8 @@ python tools/psgan_infer.py \ 2. Downloading the landmarks [data](https://paddlegan.bj.bcebos.com/landmarks.tar), and uncompress it 3. Runnint the following command to substitute files: ``` -mv landmarks/makeup MT-Dataset/landmarks/makeup -mv landmarks/non-makeup MT-Dataset/landmarks/non-makeup +rm -rf MT-Dataset/landmarks/makeup && mv landmarks/makeup MT-Dataset/landmarks/ +rm -rf MT-Dataset/landmarks/non-makeup && mv landmarks/non-makeup MT-Dataset/landmarks/ cp landmarks/train_makeup.txt MT-Dataset/train_makeup.txt cp landmarks/train_non-makeup.txt MT-Dataset/train_non-makeup.txt ``` diff --git a/docs/zh_CN/tutorials/psgan.md b/docs/zh_CN/tutorials/psgan.md index 5b5b2de..1f33638 100644 --- a/docs/zh_CN/tutorials/psgan.md +++ b/docs/zh_CN/tutorials/psgan.md @@ -33,8 +33,8 @@ python tools/psgan_infer.py \ 2. 下载landmarks数据[lmks](https://paddlegan.bj.bcebos.com/landmarks.tar),并解压 3. 运行如下命令进行文件夹及文件替换: ``` -mv landmarks/makeup MT-Dataset/landmarks/makeup -mv landmarks/non-makeup MT-Dataset/landmarks/non-makeup +rm -rf MT-Dataset/landmarks/makeup && mv landmarks/makeup MT-Dataset/landmarks/ +rm -rf MT-Dataset/landmarks/non-makeup && mv landmarks/non-makeup MT-Dataset/landmarks/ cp landmarks/train_makeup.txt MT-Dataset/train_makeup.txt cp landmarks/train_non-makeup.txt MT-Dataset/train_non-makeup.txt ``` diff --git a/ppgan/apps/dain_predictor.py b/ppgan/apps/dain_predictor.py index 28e0a62..332cf58 100644 --- a/ppgan/apps/dain_predictor.py +++ b/ppgan/apps/dain_predictor.py @@ -269,6 +269,7 @@ class DAINPredictor(BasePredictor): return sum([2**i for (i, v) in enumerate(diff.flatten()) if v]) hashes = {} + max_interp = 9 image_paths = sorted(glob.glob(os.path.join(paths, '*.png'))) for image_path in image_paths: image = cv2.imread(image_path) @@ -283,7 +284,16 @@ class DAINPredictor(BasePredictor): last_index = int( hashed_paths[-1].split('/')[-1].split('.')[-2]) + 1 gap = 2 * (last_index - first_index) - 1 - if gap > 9: + if gap > 2 * max_interp: + cut1 = len(hashed_paths) // 3 + cut2 = cut1 * 2 + for p in hashed_paths[1:cut1 - 1]: + os.remove(p) + for p in hashed_paths[cut1 + 1:cut2]: + os.remove(p) + for p in hashed_paths[cut2 + 1:]: + os.remove(p) + if gap > max_interp: mid = len(hashed_paths) // 2 for p in hashed_paths[1:mid - 1]: os.remove(p) diff --git a/ppgan/apps/psgan_predictor.py b/ppgan/apps/psgan_predictor.py index 29fa0e8..46b5922 100644 --- a/ppgan/apps/psgan_predictor.py +++ b/ppgan/apps/psgan_predictor.py @@ -43,16 +43,6 @@ def toImage(net_output): return img -def mask2image(mask: np.array, format="HWC"): - H, W = mask.shape - - canvas = np.zeros((H, W, 3), dtype=np.uint8) - for i in range(int(mask.max())): - color = np.random.rand(1, 1, 3) * 255 - canvas += (mask == i)[:, :, None] * color.astype(np.uint8) - return canvas - - PS_WEIGHT_URL = "https://paddlegan.bj.bcebos.com/models/psgan_weight.pdparams" @@ -81,6 +71,7 @@ class PreProcess: self.down_ratio, self.width_ratio) np_image = np.array(image) + image_trans = self.transform(np_image) mask = self.face_parser.parse( np.float32(cv2.resize(np_image, (512, 512)))) mask = cv2.resize(mask.numpy(), (self.img_size, self.img_size), @@ -88,7 +79,8 @@ class PreProcess: mask = mask.astype(np.uint8) mask_tensor = paddle.to_tensor(mask) - lms = futils.dlib.landmarks(image, face) * self.img_size / image.width + lms = futils.dlib.landmarks( + image, face) / image_trans.shape[:2] * self.img_size lms = lms.round() P_np = generate_P_from_lmks(lms, self.img_size, self.img_size, @@ -96,10 +88,8 @@ class PreProcess: mask_aug = generate_mask_aug(mask, lms) - image = self.transform(np_image) - return [ - self.norm(image).unsqueeze(0), + self.norm(image_trans).unsqueeze(0), np.float32(mask_aug), np.float32(P_np), np.float32(mask) @@ -212,6 +202,9 @@ class PSGANPredictor(BasePredictor): image = postprocess(source_crop, image) ref_img_name = os.path.split(reference_path)[1] + if not os.path.exists(self.output_path): + os.makedirs(sefl.output_path) + save_path = os.path.join(self.output_path, 'transfered_ref_' + ref_img_name) image.save(save_path) diff --git a/ppgan/models/makeup_model.py b/ppgan/models/makeup_model.py index 74834f7..1031fd5 100644 --- a/ppgan/models/makeup_model.py +++ b/ppgan/models/makeup_model.py @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import os import numpy as np import paddle diff --git a/tools/psgan_infer.py b/tools/psgan_infer.py index 2eff190..1ed2f28 100644 --- a/tools/psgan_infer.py +++ b/tools/psgan_infer.py @@ -12,7 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os +import sys import argparse + +cur_path = os.path.abspath(os.path.dirname(__file__)) +root_path = os.path.split(cur_path)[0] +sys.path.append(root_path) + from ppgan.utils.options import parse_args from ppgan.utils.config import get_config from ppgan.apps.psgan_predictor import PSGANPredictor -- GitLab