mirror of
https://github.com/iperov/DeepFaceLive
synced 2025-07-07 05:22:08 -07:00
FaceMerger: added interpolation type choice
This commit is contained in:
parent
09400dce75
commit
be6ac8643f
3 changed files with 43 additions and 10 deletions
|
@ -93,6 +93,11 @@ class FaceMergerWorker(BackendWorker):
|
||||||
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_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.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)
|
||||||
|
cs.interpolation.select(state.interpolation if state.interpolation is not None else 'bilinear')
|
||||||
|
|
||||||
cs.face_opacity.enable()
|
cs.face_opacity.enable()
|
||||||
cs.face_opacity.set_config(lib_csw.Number.Config(min=0.0, max=1.0, step=0.01, decimals=2, allow_instant_update=True))
|
cs.face_opacity.set_config(lib_csw.Number.Config(min=0.0, max=1.0, step=0.01, decimals=2, allow_instant_update=True))
|
||||||
cs.face_opacity.set_number(state.face_opacity if state.face_opacity is not None else 1.0)
|
cs.face_opacity.set_number(state.face_opacity if state.face_opacity is not None else 1.0)
|
||||||
|
@ -149,6 +154,12 @@ class FaceMergerWorker(BackendWorker):
|
||||||
self.save_state()
|
self.save_state()
|
||||||
self.reemit_frame_signal.send()
|
self.reemit_frame_signal.send()
|
||||||
|
|
||||||
|
def on_cs_interpolation(self, idx, interpolation):
|
||||||
|
state, cs = self.get_state(), self.get_control_sheet()
|
||||||
|
state.interpolation = interpolation
|
||||||
|
self.save_state()
|
||||||
|
self.reemit_frame_signal.send()
|
||||||
|
|
||||||
def on_cs_face_opacity(self, face_opacity):
|
def on_cs_face_opacity(self, face_opacity):
|
||||||
state, cs = self.get_state(), self.get_control_sheet()
|
state, cs = self.get_state(), self.get_control_sheet()
|
||||||
cfg = cs.face_opacity.get_config()
|
cfg = cs.face_opacity.get_config()
|
||||||
|
@ -157,14 +168,18 @@ class FaceMergerWorker(BackendWorker):
|
||||||
self.save_state()
|
self.save_state()
|
||||||
self.reemit_frame_signal.send()
|
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 ):
|
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()
|
state = self.get_state()
|
||||||
|
|
||||||
|
interpolation = self._cpu_interp[state.interpolation]
|
||||||
|
|
||||||
frame_image = ImageProcessor(frame_image).to_ufloat32().get_image('HWC')
|
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_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')
|
face_swap_mask_img = ImageProcessor(face_swap_mask_img).to_ufloat32().get_image('HW')
|
||||||
|
|
||||||
|
|
||||||
if state.face_mask_type == FaceMaskType.SRC:
|
if state.face_mask_type == FaceMaskType.SRC:
|
||||||
face_mask = face_align_mask_img
|
face_mask = face_align_mask_img
|
||||||
elif state.face_mask_type == FaceMaskType.CELEB:
|
elif state.face_mask_type == FaceMaskType.CELEB:
|
||||||
|
@ -179,7 +194,7 @@ class FaceMergerWorker(BackendWorker):
|
||||||
frame_face_mask = face_mask_ip.get_image('HWC')
|
frame_face_mask = face_mask_ip.get_image('HWC')
|
||||||
|
|
||||||
frame_face_swap_img = ImageProcessor(face_swap_img) \
|
frame_face_swap_img = ImageProcessor(face_swap_img) \
|
||||||
.to_ufloat32().warpAffine(aligned_to_source_uni_mat, frame_width, frame_height).get_image('HWC')
|
.to_ufloat32().warpAffine(aligned_to_source_uni_mat, frame_width, frame_height, interpolation=interpolation).get_image('HWC')
|
||||||
|
|
||||||
# Combine final frame
|
# Combine final frame
|
||||||
opacity = state.face_opacity
|
opacity = state.face_opacity
|
||||||
|
@ -190,9 +205,13 @@ class FaceMergerWorker(BackendWorker):
|
||||||
|
|
||||||
return frame_final
|
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 ):
|
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()
|
state = self.get_state()
|
||||||
|
interpolation = self._gpu_interp[state.interpolation]
|
||||||
|
|
||||||
if state.face_mask_type == FaceMaskType.SRC:
|
if state.face_mask_type == FaceMaskType.SRC:
|
||||||
face_mask_t = lib_cl.Tensor.from_value(face_align_mask_img)
|
face_mask_t = lib_cl.Tensor.from_value(face_align_mask_img)
|
||||||
|
@ -212,8 +231,8 @@ class FaceMergerWorker(BackendWorker):
|
||||||
face_swap_img_t = lib_cl.Tensor.from_value(face_swap_img)
|
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)
|
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, output_size=(frame_height, frame_width) )
|
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, 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_image_t = lib_cl.Tensor.from_value(frame_image).transpose( (2,0,1) )
|
frame_image_t = lib_cl.Tensor.from_value(frame_image).transpose( (2,0,1) )
|
||||||
|
|
||||||
|
@ -300,6 +319,7 @@ class Sheet:
|
||||||
self.face_mask_scale = lib_csw.Number.Client()
|
self.face_mask_scale = lib_csw.Number.Client()
|
||||||
self.face_mask_erode = lib_csw.Number.Client()
|
self.face_mask_erode = lib_csw.Number.Client()
|
||||||
self.face_mask_blur = lib_csw.Number.Client()
|
self.face_mask_blur = lib_csw.Number.Client()
|
||||||
|
self.interpolation = lib_csw.DynamicSingleSwitch.Client()
|
||||||
self.face_opacity = lib_csw.Number.Client()
|
self.face_opacity = lib_csw.Number.Client()
|
||||||
|
|
||||||
class Worker(lib_csw.Sheet.Worker):
|
class Worker(lib_csw.Sheet.Worker):
|
||||||
|
@ -312,6 +332,7 @@ class Sheet:
|
||||||
self.face_mask_type = lib_csw.DynamicSingleSwitch.Host()
|
self.face_mask_type = lib_csw.DynamicSingleSwitch.Host()
|
||||||
self.face_mask_erode = lib_csw.Number.Host()
|
self.face_mask_erode = lib_csw.Number.Host()
|
||||||
self.face_mask_blur = lib_csw.Number.Host()
|
self.face_mask_blur = lib_csw.Number.Host()
|
||||||
|
self.interpolation = lib_csw.DynamicSingleSwitch.Host()
|
||||||
self.face_opacity = lib_csw.Number.Host()
|
self.face_opacity = lib_csw.Number.Host()
|
||||||
|
|
||||||
class WorkerState(BackendWorkerState):
|
class WorkerState(BackendWorkerState):
|
||||||
|
@ -322,5 +343,6 @@ class WorkerState(BackendWorkerState):
|
||||||
face_mask_type = None
|
face_mask_type = None
|
||||||
face_mask_erode : int = None
|
face_mask_erode : int = None
|
||||||
face_mask_blur : int = None
|
face_mask_blur : int = None
|
||||||
|
interpolation = None
|
||||||
face_opacity : float = None
|
face_opacity : float = None
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,9 @@ class QFaceMerger(QBackendPanel):
|
||||||
q_face_mask_blur_label = QLabelPopupInfo(label=L('@QFaceMerger.face_mask_blur') )
|
q_face_mask_blur_label = QLabelPopupInfo(label=L('@QFaceMerger.face_mask_blur') )
|
||||||
q_face_mask_blur = QSpinBoxCSWNumber(cs.face_mask_blur, reflect_state_widgets=[q_face_mask_blur_label])
|
q_face_mask_blur = QSpinBoxCSWNumber(cs.face_mask_blur, reflect_state_widgets=[q_face_mask_blur_label])
|
||||||
|
|
||||||
|
q_interpolation_label = QLabelPopupInfo(label=L('@QFaceMerger.interpolation') )
|
||||||
|
q_interpolation = QComboBoxCSWDynamicSingleSwitch(cs.interpolation, reflect_state_widgets=[q_interpolation_label])
|
||||||
|
|
||||||
q_face_opacity_label = QLabelPopupInfo(label=L('@QFaceMerger.face_opacity') )
|
q_face_opacity_label = QLabelPopupInfo(label=L('@QFaceMerger.face_opacity') )
|
||||||
q_face_opacity = QSliderCSWNumber(cs.face_opacity, reflect_state_widgets=[q_face_opacity_label])
|
q_face_opacity = QSliderCSWNumber(cs.face_opacity, reflect_state_widgets=[q_face_opacity_label])
|
||||||
|
|
||||||
|
@ -57,6 +60,9 @@ class QFaceMerger(QBackendPanel):
|
||||||
grid_l.addLayout(lib_qt.QXVBoxLayout([q_face_mask_erode_label,q_face_mask_blur_label]), row, 0, alignment=Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter )
|
grid_l.addLayout(lib_qt.QXVBoxLayout([q_face_mask_erode_label,q_face_mask_blur_label]), row, 0, alignment=Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter )
|
||||||
grid_l.addLayout(lib_qt.QXHBoxLayout([q_face_mask_erode,q_face_mask_blur], spacing=3), row, 1, alignment=Qt.AlignmentFlag.AlignLeft )
|
grid_l.addLayout(lib_qt.QXHBoxLayout([q_face_mask_erode,q_face_mask_blur], spacing=3), row, 1, alignment=Qt.AlignmentFlag.AlignLeft )
|
||||||
row += 1
|
row += 1
|
||||||
|
grid_l.addWidget(q_interpolation_label, row, 0, alignment=Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter )
|
||||||
|
grid_l.addWidget(q_interpolation, row, 1, alignment=Qt.AlignmentFlag.AlignLeft )
|
||||||
|
row += 1
|
||||||
grid_l.addWidget(q_face_opacity_label, row, 0, alignment=Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter )
|
grid_l.addWidget(q_face_opacity_label, row, 0, alignment=Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter )
|
||||||
grid_l.addWidget(q_face_opacity, row, 1)
|
grid_l.addWidget(q_face_opacity, row, 1)
|
||||||
row += 1
|
row += 1
|
||||||
|
|
|
@ -547,6 +547,11 @@ class Localization:
|
||||||
'ru-RU' : 'Размытие маски',
|
'ru-RU' : 'Размытие маски',
|
||||||
'zh-CN' : '遮罩边缘羽化'},
|
'zh-CN' : '遮罩边缘羽化'},
|
||||||
|
|
||||||
|
'QFaceMerger.interpolation':{
|
||||||
|
'en-US' : 'Interpolation',
|
||||||
|
'ru-RU' : 'Интерполяция',
|
||||||
|
'zh-CN' : '插值 '},
|
||||||
|
|
||||||
'QFaceMerger.face_opacity':{
|
'QFaceMerger.face_opacity':{
|
||||||
'en-US' : 'Face opacity',
|
'en-US' : 'Face opacity',
|
||||||
'ru-RU' : 'Непрозрач. лица',
|
'ru-RU' : 'Непрозрач. лица',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue