Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
iperov committed Oct 7, 2021
1 parent be6ac86 commit 458816d
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions apps/DeepFaceLive/backend/FaceMerger.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def on_cs_device(self, idxs, device : lib_cl.DeviceInfo):
cs.face_mask_blur.enable()
cs.face_mask_blur.set_config(lib_csw.Number.Config(min=0, max=400, step=1, decimals=0, allow_instant_update=True))
cs.face_mask_blur.set_number(state.face_mask_blur if state.face_mask_blur is not None else 25.0)

cs.interpolation.call_on_selected(self.on_cs_interpolation)
cs.interpolation.enable()
cs.interpolation.set_choices(['bilinear','bicubic','lanczos4'], none_choice_name=None)
Expand Down Expand Up @@ -131,7 +131,7 @@ def on_cs_face_scale(self, face_scale):
cs.face_scale.set_number(face_scale)
self.save_state()
self.reemit_frame_signal.send()

def on_cs_face_mask_type(self, idx, face_mask_type):
state, cs = self.get_state(), self.get_control_sheet()
state.face_mask_type = face_mask_type
Expand All @@ -153,7 +153,7 @@ def on_cs_mask_blur(self, face_mask_blur):
cs.face_mask_blur.set_number(face_mask_blur)
self.save_state()
self.reemit_frame_signal.send()

def on_cs_interpolation(self, idx, interpolation):
state, cs = self.get_state(), self.get_control_sheet()
state.interpolation = interpolation
Expand All @@ -167,15 +167,15 @@ def on_cs_face_opacity(self, face_opacity):
cs.face_opacity.set_number(face_opacity)
self.save_state()
self.reemit_frame_signal.send()

_cpu_interp = {'bilinear' : ImageProcessor.Interpolation.LINEAR,
'bicubic' : ImageProcessor.Interpolation.CUBIC,
'lanczos4' : ImageProcessor.Interpolation.LANCZOS4}
def _merge_on_cpu(self, frame_image, face_align_mask_img, face_swap_img, face_swap_mask_img, aligned_to_source_uni_mat, frame_width, frame_height ):
state = self.get_state()

interpolation = self._cpu_interp[state.interpolation]

frame_image = ImageProcessor(frame_image).to_ufloat32().get_image('HWC')
face_align_mask_img = ImageProcessor(face_align_mask_img).to_ufloat32().get_image('HW')
face_swap_mask_img = ImageProcessor(face_swap_mask_img).to_ufloat32().get_image('HW')
Expand Down Expand Up @@ -204,11 +204,11 @@ def _merge_on_cpu(self, frame_image, face_align_mask_img, face_swap_img, face_sw
frame_final = ne.evaluate('frame_image*(1.0-frame_face_mask) + frame_image*frame_face_mask*(1.0-opacity) + frame_face_swap_img*frame_face_mask*opacity')

return frame_final

_gpu_interp = {'bilinear' : lib_cl.EInterpolation.LINEAR,
'bicubic' : lib_cl.EInterpolation.CUBIC,
'lanczos4' : lib_cl.EInterpolation.LANCZOS4}

def _merge_on_gpu(self, frame_image, face_align_mask_img, face_swap_img, face_swap_mask_img, aligned_to_source_uni_mat, frame_width, frame_height ):
state = self.get_state()
interpolation = self._gpu_interp[state.interpolation]
Expand All @@ -231,16 +231,16 @@ def _merge_on_gpu(self, frame_image, face_align_mask_img, face_swap_img, face_sw
face_swap_img_t = lib_cl.Tensor.from_value(face_swap_img)
face_swap_img_t = face_swap_img_t.transpose( (2,0,1), op_text='O = ((O_TYPE)I) / 255.0', dtype=np.float32)

frame_face_mask_t = lib_cl.remap_np_affine(face_mask_t, aligned_to_source_uni_mat, interpolation=interpolation, output_size=(frame_height, frame_width) )
frame_face_swap_img_t = lib_cl.remap_np_affine(face_swap_img_t, aligned_to_source_uni_mat, interpolation=interpolation, output_size=(frame_height, frame_width) )
frame_face_mask_t = lib_cl.remap_np_affine(face_mask_t, aligned_to_source_uni_mat, interpolation=lib_cl.EInterpolation.LINEAR, output_size=(frame_height, frame_width), post_op_text='O = (O <= (1.0/255.0) ? 0.0 : O > 1.0 ? 1.0 : O);' )
frame_face_swap_img_t = lib_cl.remap_np_affine(face_swap_img_t, aligned_to_source_uni_mat, interpolation=interpolation, output_size=(frame_height, frame_width), post_op_text='O = clamp(O, 0.0, 1.0);' )

frame_image_t = lib_cl.Tensor.from_value(frame_image).transpose( (2,0,1) )

opacity = state.face_opacity
if opacity == 1.0:
frame_final_t = lib_cl.any_wise('float I0f = (((float)I0) / 255.0); I1 = (I1 <= (1.0/255.0) ? 0.0 : I1 > 1.0 ? 1.0 : I1); O = I0f*(1.0-I1) + I2*I1', frame_image_t, frame_face_mask_t, frame_face_swap_img_t, dtype=np.float32)
frame_final_t = lib_cl.any_wise('float I0f = (((float)I0) / 255.0); O = I0f*(1.0-I1) + I2*I1', frame_image_t, frame_face_mask_t, frame_face_swap_img_t, dtype=np.float32)
else:
frame_final_t = lib_cl.any_wise('float I0f = (((float)I0) / 255.0); I1 = (I1 <= (1.0/255.0) ? 0.0 : I1 > 1.0 ? 1.0 : I1); O = I0f*(1.0-I1) + I0f*I1*(1.0-I3) + I2*I1*I3', frame_image_t, frame_face_mask_t, frame_face_swap_img_t, opacity, dtype=np.float32)
frame_final_t = lib_cl.any_wise('float I0f = (((float)I0) / 255.0); O = I0f*(1.0-I1) + I0f*I1*(1.0-I3) + I2*I1*I3', frame_image_t, frame_face_mask_t, frame_face_swap_img_t, opacity, dtype=np.float32)

return frame_final_t.transpose( (1,2,0) ).np()

Expand Down

0 comments on commit 458816d

Please sign in to comment.