Skip to content
Permalink
Browse files

moving some files

  • Loading branch information...
iperov committed Oct 14, 2019
1 parent 2b264da commit e013cb0f6baff3e52c61e2612a95afe69d44a941
@@ -56,14 +56,14 @@ def ConvertMaskedFace (predictor_func, predictor_input_shape, cfg, frame_info, i

if cfg.mask_mode == 2: #dst
prd_face_mask_a_0 = cv2.resize (dst_face_mask_a_0, (output_size,output_size), cv2.INTER_CUBIC)
elif cfg.mask_mode >= 3 and cfg.mask_mode <= 7:
elif cfg.mask_mode >= 3 and cfg.mask_mode <= 8:

if cfg.mask_mode == 3 or cfg.mask_mode == 5 or cfg.mask_mode == 6:
prd_face_fanseg_bgr = cv2.resize (prd_face_bgr, (cfg.fanseg_input_size,)*2 )
prd_face_fanseg_mask = cfg.fanseg_extract_func(FaceType.FULL, prd_face_fanseg_bgr)
FAN_prd_face_mask_a_0 = cv2.resize ( prd_face_fanseg_mask, (output_size, output_size), cv2.INTER_CUBIC)

if cfg.mask_mode >= 4 or cfg.mask_mode <= 7:
if cfg.mask_mode >= 4 and cfg.mask_mode <= 7:

full_face_fanseg_mat = LandmarksProcessor.get_transform_mat (img_face_landmarks, cfg.fanseg_input_size, face_type=FaceType.FULL)
dst_face_fanseg_bgr = cv2.warpAffine(img_bgr, full_face_fanseg_mat, (cfg.fanseg_input_size,)*2, flags=cv2.INTER_CUBIC )
@@ -80,9 +80,24 @@ def ConvertMaskedFace (predictor_func, predictor_input_shape, cfg, frame_info, i
m = cv2.getAffineTransform(b, fanseg_rect_corner_pts)
FAN_dst_face_mask_a_0 = cv2.warpAffine(dst_face_fanseg_mask, m, (cfg.fanseg_input_size,)*2, flags=cv2.INTER_CUBIC )
FAN_dst_face_mask_a_0 = cv2.resize (FAN_dst_face_mask_a_0, (output_size,output_size), cv2.INTER_CUBIC)
#else:
# raise ValueError ("cfg.face_type unsupported")
"""
if cfg.mask_mode == 8: #FANCHQ-dst
full_face_fanchq_mat = LandmarksProcessor.get_transform_mat (img_face_landmarks, cfg.fanchq_input_size, face_type=FaceType.FULL)
dst_face_fanchq_bgr = cv2.warpAffine(img_bgr, full_face_fanchq_mat, (cfg.fanchq_input_size,)*2, flags=cv2.INTER_CUBIC )
dst_face_fanchq_mask = cfg.fanchq_extract_func( FaceType.FULL, dst_face_fanchq_bgr )
if cfg.face_type == FaceType.FULL:
FANCHQ_dst_face_mask_a_0 = cv2.resize (dst_face_fanchq_mask, (output_size,output_size), cv2.INTER_CUBIC)
else:
face_fanchq_mat = LandmarksProcessor.get_transform_mat (img_face_landmarks, cfg.fanchq_input_size, face_type=cfg.face_type)
fanchq_rect_corner_pts = np.array ( [ [0,0], [cfg.fanchq_input_size-1,0], [0,cfg.fanchq_input_size-1] ], dtype=np.float32 )
a = LandmarksProcessor.transform_points (fanchq_rect_corner_pts, face_fanchq_mat, invert=True )
b = LandmarksProcessor.transform_points (a, full_face_fanchq_mat )
m = cv2.getAffineTransform(b, fanchq_rect_corner_pts)
FAN_dst_face_mask_a_0 = cv2.warpAffine(dst_face_fanchq_mask, m, (cfg.fanchq_input_size,)*2, flags=cv2.INTER_CUBIC )
FAN_dst_face_mask_a_0 = cv2.resize (FAN_dst_face_mask_a_0, (output_size,output_size), cv2.INTER_CUBIC)
"""
if cfg.mask_mode == 3: #FAN-prd
prd_face_mask_a_0 = FAN_prd_face_mask_a_0
elif cfg.mask_mode == 4: #FAN-dst
@@ -93,7 +108,9 @@ def ConvertMaskedFace (predictor_func, predictor_input_shape, cfg, frame_info, i
prd_face_mask_a_0 = prd_face_mask_a_0 * FAN_prd_face_mask_a_0 * FAN_dst_face_mask_a_0
elif cfg.mask_mode == 7:
prd_face_mask_a_0 = prd_face_mask_a_0 * FAN_dst_face_mask_a_0

#elif cfg.mask_mode == 8: #FANCHQ-dst
# prd_face_mask_a_0 = FANCHQ_dst_face_mask_a_0

prd_face_mask_a_0[ prd_face_mask_a_0 < 0.001 ] = 0.0

prd_face_mask_a = prd_face_mask_a_0[...,np.newaxis]
@@ -21,6 +21,9 @@ def __init__(self, type=0):
self.blursharpen_func = None
self.fanseg_input_size = None
self.fanseg_extract_func = None

self.fanchq_input_size = None
self.fanchq_extract_func = None
self.ebs_ct_func = None

self.super_res_dict = {0:"None", 1:'RankSRGAN'}
@@ -3,5 +3,4 @@
from .MTCExtractor import MTCExtractor
from .S3FDExtractor import S3FDExtractor
from .FANExtractor import FANExtractor
from .FANSegmentator import FANSegmentator
from .PoseEstimator import PoseEstimator
10 main.py
@@ -78,6 +78,16 @@ def process_dev_extract_umd_csv(arguments):
p.add_argument('--multi-gpu', action="store_true", dest="multi_gpu", default=False, help="Enables multi GPU.")
p.add_argument('--cpu-only', action="store_true", dest="cpu_only", default=False, help="Extract on CPU.")
p.set_defaults (func=process_dev_extract_umd_csv)


def process_dev_apply_celebamaskhq(arguments):
os_utils.set_process_lowest_prio()
from mainscripts import dev_misc
dev_misc.apply_celebamaskhq( arguments.input_dir )

p = subparsers.add_parser( "dev_apply_celebamaskhq", help="")
p.add_argument('--input-dir', required=True, action=fixPathAction, dest="input_dir")
p.set_defaults (func=process_dev_apply_celebamaskhq)
"""
def process_extract_fanseg(arguments):
os_utils.set_process_lowest_prio()
@@ -16,7 +16,9 @@
import imagelib
from converters import (ConverterConfig, ConvertFaceAvatar, ConvertMasked,
FrameInfo)
from facelib import FaceType, FANSegmentator, LandmarksProcessor
from facelib import FaceType, LandmarksProcessor
from nnlib import TernausNet

from interact import interact as io
from joblib import SubprocessFunctionCaller, Subprocessor
from utils import Path_utils
@@ -114,13 +116,25 @@ def blursharpen_func (img, sharpen_mode=0, kernel_size=3, amount=100):
def fanseg_extract(face_type, *args, **kwargs):
fanseg = self.fanseg_by_face_type.get(face_type, None)
if self.fanseg_by_face_type.get(face_type, None) is None:
fanseg = FANSegmentator( self.fanseg_input_size , FaceType.toString( face_type ) )
fanseg = TernausNet("FANSeg", self.fanseg_input_size , FaceType.toString( face_type ) )
self.fanseg_by_face_type[face_type] = fanseg

return fanseg.extract(*args, **kwargs)

self.fanseg_extract_func = fanseg_extract

self.fanchq_by_face_type = {}
self.fanchq_input_size = 256
def fanchq_extract(face_type, *args, **kwargs):
fanchq = self.fanchq_by_face_type.get(face_type, None)
if self.fanchq_by_face_type.get(face_type, None) is None:
fanchq = TernausNet("FANCHQ", self.fanchq_input_size , FaceType.toString( face_type ) )
self.fanchq_by_face_type[face_type] = fanchq

return fanchq.extract(*args, **kwargs)

self.fanchq_extract_func = fanchq_extract

import ebsynth
def ebs_ct(*args, **kwargs):
return ebsynth.color_transfer(*args, **kwargs)
@@ -161,6 +175,8 @@ def process_data(self, pf): #pf=ProcessingFrame
if cfg.type == ConverterConfig.TYPE_MASKED:
cfg.fanseg_input_size = self.fanseg_input_size
cfg.fanseg_extract_func = self.fanseg_extract_func
cfg.fanchq_input_size = self.fanchq_input_size
cfg.fanchq_extract_func = self.fanchq_extract_func

try:
final_img = ConvertMasked (self.predictor_func, self.predictor_input_shape, cfg, frame_info)
@@ -14,10 +14,10 @@
import facelib
import imagelib
import mathlib
from facelib import FaceType, FANSegmentator, LandmarksProcessor
from facelib import FaceType, LandmarksProcessor
from interact import interact as io
from joblib import Subprocessor
from nnlib import nnlib
from nnlib import TernausNet, nnlib
from utils import Path_utils
from utils.cv2_utils import *
from utils.DFLJPG import DFLJPG
@@ -95,7 +95,7 @@ def on_initialize(self, client_dict):

elif self.type == 'fanseg':
nnlib.import_all (device_config)
self.e = facelib.FANSegmentator(256, FaceType.toString(FaceType.FULL) )
self.e = TernausNet(256, FaceType.toString(FaceType.FULL) )
self.e.__enter__()

elif self.type == 'final':
@@ -279,7 +279,6 @@ def process_data(self, data):
fanseg_mask = self.e.extract( image / 255.0 )
src_dflimg.embed_and_set( filename_path_str,
fanseg_mask=fanseg_mask,
#fanseg_mask_ver=FANSegmentator.VERSION,
)

#overridable
@@ -889,4 +888,3 @@ def extract_umd_csv(input_file_csv,
io.log_info ('Images found: %d' % (images_found) )
io.log_info ('Faces detected: %d' % (faces_detected) )
io.log_info ('-------------------------')

@@ -1,9 +1,20 @@
from . import Extractor
from . import Sorter
from pathlib import Path
from utils import Path_utils
import multiprocessing
import shutil
from pathlib import Path

import cv2
import numpy as np

from facelib import LandmarksProcessor
from interact import interact as io
from joblib import Subprocessor
from utils import Path_utils
from utils.cv2_utils import *
from utils.DFLJPG import DFLJPG
from utils.DFLPNG import DFLPNG

from . import Extractor, Sorter


def extract_vggface2_dataset(input_dir, device_args={} ):
multi_gpu = device_args.get('multi_gpu', False)
@@ -47,4 +58,169 @@ def extract_vggface2_dataset(input_dir, device_args={} ):
io.log_info (f"Removing: {str(cur_input_path)} ")
shutil.rmtree(cur_input_path)
except:
io.log_info (f"unable to remove: {str(cur_input_path)} ")
io.log_info (f"unable to remove: {str(cur_input_path)} ")



class CelebAMASKHQSubprocessor(Subprocessor):
class Cli(Subprocessor.Cli):
#override
def on_initialize(self, client_dict):
self.masks_files_paths = client_dict['masks_files_paths']
return None

#override
def process_data(self, data):
filename = data[0]
filepath = Path(filename)
if filepath.suffix == '.png':
dflimg = DFLPNG.load( str(filepath) )
elif filepath.suffix == '.jpg':
dflimg = DFLJPG.load ( str(filepath) )
else:
dflimg = None

image_to_face_mat = dflimg.get_image_to_face_mat()
src_filename = dflimg.get_source_filename()

img = cv2_imread(filename)
h,w,c = img.shape

fanseg_mask = LandmarksProcessor.get_image_hull_mask(img.shape, dflimg.get_landmarks() )

idx_name = '%.5d' % int(src_filename.split('.')[0])
idx_files = [ x for x in self.masks_files_paths if idx_name in x ]

skin_files = [ x for x in idx_files if 'skin' in x ]
eye_glass_files = [ x for x in idx_files if 'eye_g' in x ]

for files, is_invert in [ (skin_files,False),
(eye_glass_files,True) ]:
if len(files) > 0:
mask = cv2_imread(files[0])
mask = mask[...,0]
mask[mask == 255] = 1
mask = mask.astype(np.float32)
mask = cv2.resize(mask, (1024,1024) )
mask = cv2.warpAffine(mask, image_to_face_mat, (w, h), cv2.INTER_LANCZOS4)

if not is_invert:
fanseg_mask *= mask[...,None]
else:
fanseg_mask *= (1-mask[...,None])

dflimg.embed_and_set (filename, fanseg_mask=fanseg_mask)
return 1

#override
def get_data_name (self, data):
#return string identificator of your data
return data[0]

#override
def __init__(self, image_paths, masks_files_paths ):
self.image_paths = image_paths
self.masks_files_paths = masks_files_paths

self.result = []
super().__init__('CelebAMASKHQSubprocessor', CelebAMASKHQSubprocessor.Cli, 60)

#override
def process_info_generator(self):
for i in range(min(multiprocessing.cpu_count(), 8)):
yield 'CPU%d' % (i), {}, {'masks_files_paths' : self.masks_files_paths }

#override
def on_clients_initialized(self):
io.progress_bar ("Processing", len (self.image_paths))

#override
def on_clients_finalized(self):
io.progress_bar_close()

#override
def get_data(self, host_dict):
if len (self.image_paths) > 0:
return [self.image_paths.pop(0)]
return None

#override
def on_data_return (self, host_dict, data):
self.image_paths.insert(0, data[0])

#override
def on_result (self, host_dict, data, result):
io.progress_bar_inc(1)

#override
def get_result(self):
return self.result

#unused in end user workflow
def apply_celebamaskhq(input_dir ):

input_path = Path(input_dir)

img_path = input_path / 'aligned'
mask_path = input_path / 'mask'

if not img_path.exists():
raise ValueError(f'{str(img_path)} directory not found. Please ensure it exists.')

CelebAMASKHQSubprocessor(Path_utils.get_image_paths(img_path),
Path_utils.get_image_paths(mask_path, subdirs=True) ).run()

return

paths_to_extract = []
for filename in io.progress_bar_generator(Path_utils.get_image_paths(img_path), desc="Processing"):
filepath = Path(filename)
if filepath.suffix == '.png':
dflimg = DFLPNG.load( str(filepath) )
elif filepath.suffix == '.jpg':
dflimg = DFLJPG.load ( str(filepath) )
else:
dflimg = None

if dflimg is not None:
paths_to_extract.append (filepath)

image_to_face_mat = dflimg.get_image_to_face_mat()
src_filename = dflimg.get_source_filename()

#img = cv2_imread(filename)
h,w,c = dflimg.get_shape()

fanseg_mask = LandmarksProcessor.get_image_hull_mask( (h,w,c), dflimg.get_landmarks() )

idx_name = '%.5d' % int(src_filename.split('.')[0])
idx_files = [ x for x in masks_files if idx_name in x ]

skin_files = [ x for x in idx_files if 'skin' in x ]
eye_glass_files = [ x for x in idx_files if 'eye_g' in x ]

for files, is_invert in [ (skin_files,False),
(eye_glass_files,True) ]:

if len(files) > 0:
mask = cv2_imread(files[0])
mask = mask[...,0]
mask[mask == 255] = 1
mask = mask.astype(np.float32)
mask = cv2.resize(mask, (1024,1024) )
mask = cv2.warpAffine(mask, image_to_face_mat, (w, h), cv2.INTER_LANCZOS4)

if not is_invert:
fanseg_mask *= mask[...,None]
else:
fanseg_mask *= (1-mask[...,None])

#cv2.imshow("", (fanseg_mask*255).astype(np.uint8) )
#cv2.waitKey(0)


dflimg.embed_and_set (filename, fanseg_mask=fanseg_mask)


#import code
#code.interact(local=dict(globals(), **locals()))

0 comments on commit e013cb0

Please sign in to comment.
You can’t perform that action at this time.