facerelighther: added intensity param

This commit is contained in:
Colombo 2019-11-13 00:19:26 +04:00
parent 54786848bc
commit 006233656b
2 changed files with 38 additions and 21 deletions

View file

@ -20,7 +20,7 @@ class RelightEditor:
self.current_img_shape = None self.current_img_shape = None
self.pick_new_face() self.pick_new_face()
self.alt_azi_ar = [ [0,0] ] self.alt_azi_ar = [ [0,0,1.0] ]
self.alt_azi_cur = 0 self.alt_azi_cur = 0
self.mouse_x = self.mouse_y = 9999 self.mouse_x = self.mouse_y = 9999
@ -43,21 +43,22 @@ class RelightEditor:
return result return result
def make_screen(self): def make_screen(self):
alt,azi=self.alt_azi_ar[self.alt_azi_cur] alt,azi,inten=self.alt_azi_ar[self.alt_azi_cur]
img = self.dpr.relight (self.current_img, alt, azi, self.lighten) img = self.dpr.relight (self.current_img, alt, azi, inten, self.lighten)
h,w,c = img.shape h,w,c = img.shape
lines = ['Pick light directions for whole faceset.', lines = ['Pick light directions for whole faceset.',
'[q]-new test face', '[q]-new test face',
'[w][e]-navigate', '[w][e]-navigate',
'[a][s]-intensity',
'[r]-new [t]-delete [enter]-process', '[r]-new [t]-delete [enter]-process',
''] '']
for i, (alt,azi) in enumerate(self.alt_azi_ar): for i, (alt,azi,inten) in enumerate(self.alt_azi_ar):
s = '>:' if self.alt_azi_cur == i else ' :' s = '>:' if self.alt_azi_cur == i else ' :'
s += f'alt=[{ int(alt):03}] azi=[{ int(azi):03}]' s += f'alt=[{ int(alt):03}] azi=[{ int(azi):03}] int=[{inten:01.1f}]'
lines += [ s ] lines += [ s ]
lines_count = len(lines) lines_count = len(lines)
@ -109,10 +110,12 @@ class RelightEditor:
if is_angle_editing: if is_angle_editing:
h,w,c = self.current_img_shape h,w,c = self.current_img_shape
self.alt_azi_ar[self.alt_azi_cur] = \ alt,azi,inten = self.alt_azi_ar[self.alt_azi_cur]
[np.clip ( ( 0.5-y/w )*2.0, -1, 1)*90, \ alt = np.clip ( ( 0.5-y/w )*2.0, -1, 1)*90
np.clip ( (x / h - 0.5)*2.0, -1, 1)*90 ] azi = np.clip ( (x / h - 0.5)*2.0, -1, 1)*90
self.alt_azi_ar[self.alt_azi_cur] = (alt,azi,inten)
self.set_screen_changed() self.set_screen_changed()
@ -130,7 +133,7 @@ class RelightEditor:
self.set_screen_changed() self.set_screen_changed()
elif chr_key == 'r': elif chr_key == 'r':
#add direction #add direction
self.alt_azi_ar += [ [0,0] ] self.alt_azi_ar += [ [0,0,1.0] ]
self.alt_azi_cur +=1 self.alt_azi_cur +=1
self.set_screen_changed() self.set_screen_changed()
elif chr_key == 't': elif chr_key == 't':
@ -138,6 +141,16 @@ class RelightEditor:
self.alt_azi_ar.pop(self.alt_azi_cur) self.alt_azi_ar.pop(self.alt_azi_cur)
self.alt_azi_cur = np.clip (self.alt_azi_cur, 0, len(self.alt_azi_ar)-1) self.alt_azi_cur = np.clip (self.alt_azi_cur, 0, len(self.alt_azi_ar)-1)
self.set_screen_changed() self.set_screen_changed()
elif chr_key == 'a':
alt,azi,inten = self.alt_azi_ar[self.alt_azi_cur]
inten = np.clip ( inten-0.1, 0.0, 1.0)
self.alt_azi_ar[self.alt_azi_cur] = (alt,azi,inten)
self.set_screen_changed()
elif chr_key == 's':
alt,azi,inten = self.alt_azi_ar[self.alt_azi_cur]
inten = np.clip ( inten+0.1, 0.0, 1.0)
self.alt_azi_ar[self.alt_azi_cur] = (alt,azi,inten)
self.set_screen_changed()
elif key == 27 or chr_key == '\r' or chr_key == '\n': #esc elif key == 27 or chr_key == '\r' or chr_key == '\n': #esc
is_exit = True is_exit = True
@ -164,7 +177,7 @@ def relight(input_dir, lighten=None, random_one=None):
if not manual: if not manual:
if random_one is None: if random_one is None:
random_one = io.input_bool ("Relight the faces only with one random direction? ( y/n default:y ?:help) : ", True, help_message="Otherwise faceset will be relighted with predefined 7 light directions.") random_one = io.input_bool ("Relight the faces only with one random direction and random intensity? ( y/n default:y ?:help) : ", True, help_message="Otherwise faceset will be relighted with predefined 7 light directions but with random intensity.")
image_paths = [Path(x) for x in Path_utils.get_image_paths(input_dir)] image_paths = [Path(x) for x in Path_utils.get_image_paths(input_dir)]
filtered_image_paths = [] filtered_image_paths = []
@ -194,10 +207,7 @@ def relight(input_dir, lighten=None, random_one=None):
if manual: if manual:
alt_azi_ar = RelightEditor(image_paths, dpr, lighten).run() alt_azi_ar = RelightEditor(image_paths, dpr, lighten).run()
else:
if not random_one:
alt_azi_ar = [(60,0), (60,60), (0,60), (-60,60), (-60,0), (-60,-60), (0,-60), (60,-60)]
for filepath in io.progress_bar_generator(image_paths, "Relighting"): for filepath in io.progress_bar_generator(image_paths, "Relighting"):
try: try:
if filepath.suffix == '.png': if filepath.suffix == '.png':
@ -218,9 +228,14 @@ def relight(input_dir, lighten=None, random_one=None):
if random_one: if random_one:
alt = np.random.randint(-90,91) alt = np.random.randint(-90,91)
azi = np.random.randint(-90,91) azi = np.random.randint(-90,91)
relighted_imgs = [dpr.relight(img,alt=alt,azi=azi,lighten=lighten)] inten = np.random.random()*0.3+0.3
relighted_imgs = [dpr.relight(img,alt=alt,azi=azi,intensity=inten,lighten=lighten)]
else: else:
relighted_imgs = [dpr.relight(img,alt=alt,azi=azi,lighten=lighten) for (alt,azi) in alt_azi_ar ] if not manual and not random_one:
inten = np.random.random()*0.3+0.3
alt_azi_ar = [(60,0,inten), (60,60,inten), (0,60,inten), (-60,60,inten), (-60,0,inten), (-60,-60,inten), (0,-60,inten), (60,-60,inten)]
relighted_imgs = [dpr.relight(img,alt=alt,azi=azi,intensity=inten,lighten=lighten) for (alt,azi,inten) in alt_azi_ar ]
i = 0 i = 0
for i,relighted_img in enumerate(relighted_imgs): for i,relighted_img in enumerate(relighted_imgs):

View file

@ -45,11 +45,12 @@ class DeepPortraitRelighting(object):
return sh_basis return sh_basis
#n = [0..8] #n = [0..8]
def relight(self, img, alt, azi, lighten=False): def relight(self, img, alt, azi, intensity=1.0, lighten=False):
torch = self.torch torch = self.torch
sh = self.SH_basis (alt, azi) sh = self.SH_basis (alt, azi)
sh = (sh.reshape( (1,9,1,1) ) ).astype(np.float32) sh = (sh.reshape( (1,9,1,1) ) ).astype(np.float32)
#sh *= 0.1
sh = torch.autograd.Variable(torch.from_numpy(sh).to(self.torch_device)) sh = torch.autograd.Variable(torch.from_numpy(sh).to(self.torch_device))
row, col, _ = img.shape row, col, _ = img.shape
@ -67,9 +68,10 @@ class DeepPortraitRelighting(object):
outputImg = cv2.blur(outputImg, (3,3) ) outputImg = cv2.blur(outputImg, (3,3) )
if not lighten: if not lighten:
outputImg = inputL* outputImg outputImg = inputL*(1.0-intensity) + (inputL*outputImg)*intensity
else: else:
outputImg = outputImg*255.0 outputImg = inputL*(1.0-intensity) + (outputImg*255.0)*intensity
outputImg = np.clip(outputImg, 0,255).astype(np.uint8) outputImg = np.clip(outputImg, 0,255).astype(np.uint8)
Lab[:,:,0] = outputImg Lab[:,:,0] = outputImg