added new XSegEditor !

here new whole_face + XSeg workflow:

with XSeg model you can train your own mask segmentator for dst(and/or src) faces
that will be used by the merger for whole_face.

Instead of using a pretrained segmentator model (which does not exist),
you control which part of faces should be masked.

new scripts:
	5.XSeg) data_dst edit masks.bat
	5.XSeg) data_src edit masks.bat
	5.XSeg) train.bat

Usage:
	unpack dst faceset if packed

	run 5.XSeg) data_dst edit masks.bat

	Read tooltips on the buttons (en/ru/zn languages are supported)

	mask the face using include or exclude polygon mode.

	repeat for 50/100 faces,
		!!! you don't need to mask every frame of dst
		only frames where the face is different significantly,
		for example:
			closed eyes
			changed head direction
			changed light
		the more various faces you mask, the more quality you will get

		Start masking from the upper left area and follow the clockwise direction.
		Keep the same logic of masking for all frames, for example:
			the same approximated jaw line of the side faces, where the jaw is not visible
			the same hair line
		Mask the obstructions using exclude polygon mode.

	run XSeg) train.bat
		train the model

		Check the faces of 'XSeg dst faces' preview.

		if some faces have wrong or glitchy mask, then repeat steps:
			run edit
			find these glitchy faces and mask them
			train further or restart training from scratch

Restart training of XSeg model is only possible by deleting all 'model\XSeg_*' files.

If you want to get the mask of the predicted face (XSeg-prd mode) in merger,
you should repeat the same steps for src faceset.

New mask modes available in merger for whole_face:

XSeg-prd	  - XSeg mask of predicted face	-> faces from src faceset should be labeled
XSeg-dst	  - XSeg mask of dst face        	-> faces from dst faceset should be labeled
XSeg-prd*XSeg-dst - the smallest area of both

if workspace\model folder contains trained XSeg model, then merger will use it,
otherwise you will get transparent mask by using XSeg-* modes.

Some screenshots:
XSegEditor: https://i.imgur.com/7Bk4RRV.jpg
trainer   : https://i.imgur.com/NM1Kn3s.jpg
merger    : https://i.imgur.com/glUzFQ8.jpg

example of the fake using 13 segmented dst faces
          : https://i.imgur.com/wmvyizU.gifv
This commit is contained in:
Colombo 2020-03-24 12:15:31 +04:00
parent eddebedcf6
commit 01d81674fd
42 changed files with 2009 additions and 24 deletions

View file

@ -121,9 +121,9 @@ Unfortunately, there is no "make everything ok" button in DeepFaceLab. You shoul
||bitcoin:bc1qkhh7h0gwwhxgg6h6gpllfgstkd645fefrd5s6z| ||bitcoin:bc1qkhh7h0gwwhxgg6h6gpllfgstkd645fefrd5s6z|
|Alipay 捐款|![](doc/Alipay_donation.jpg)| |Alipay 捐款|![](doc/Alipay_donation.jpg)|
||| |||
|Last donations|10$ ( 朱 阳阳 )| |Last donations|50$ ( Tomas Hajka )|
||10$ ( 朱 阳阳 )|
||24$ ( NextFace )| ||24$ ( NextFace )|
||10$ ( Amien Phillips )|
||| |||
|Collect facesets|You can collect faceset of any celebrity that can be used in DeepFaceLab and share it [in the community](https://mrdeepfakes.com/forums/forum-celebrity-facesets)| |Collect facesets|You can collect faceset of any celebrity that can be used in DeepFaceLab and share it [in the community](https://mrdeepfakes.com/forums/forum-celebrity-facesets)|
||| |||

10
XSegEditor/QCursorDB.py Normal file
View file

@ -0,0 +1,10 @@
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QCursorDB():
@staticmethod
def initialize(cursor_path):
QCursorDB.cross_red = QCursor ( QPixmap ( str(cursor_path / 'cross_red.png') ) )
QCursorDB.cross_green = QCursor ( QPixmap ( str(cursor_path / 'cross_green.png') ) )
QCursorDB.cross_blue = QCursor ( QPixmap ( str(cursor_path / 'cross_blue.png') ) )

21
XSegEditor/QIconDB.py Normal file
View file

@ -0,0 +1,21 @@
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QIconDB():
@staticmethod
def initialize(icon_path):
QIconDB.app_icon = QIcon ( str(icon_path / 'app_icon.png') )
QIconDB.delete_poly = QIcon ( str(icon_path / 'delete_poly.png') )
QIconDB.undo_pt = QIcon ( str(icon_path / 'undo_pt.png') )
QIconDB.redo_pt = QIcon ( str(icon_path / 'redo_pt.png') )
QIconDB.poly_color_red = QIcon ( str(icon_path / 'poly_color_red.png') )
QIconDB.poly_color_green = QIcon ( str(icon_path / 'poly_color_green.png') )
QIconDB.poly_color_blue = QIcon ( str(icon_path / 'poly_color_blue.png') )
QIconDB.poly_type_include = QIcon ( str(icon_path / 'poly_type_include.png') )
QIconDB.poly_type_exclude = QIcon ( str(icon_path / 'poly_type_exclude.png') )
QIconDB.left = QIcon ( str(icon_path / 'left.png') )
QIconDB.right = QIcon ( str(icon_path / 'right.png') )
QIconDB.pt_edit_mode = QIcon ( str(icon_path / 'pt_edit_mode.png') )
QIconDB.view_baked = QIcon ( str(icon_path / 'view_baked.png') )

72
XSegEditor/QStringDB.py Normal file
View file

@ -0,0 +1,72 @@
from localization import system_language
class QStringDB():
@staticmethod
def initialize():
lang = system_language
if lang not in ['en','ru','zn']:
lang = 'en'
QStringDB.btn_poly_color_red_tip = { 'en' : 'Poly color scheme red',
'ru' : 'Красная цветовая схема полигонов',
'zn' : '多边形配色方案红色',
}[lang]
QStringDB.btn_poly_color_green_tip = { 'en' : 'Poly color scheme green',
'ru' : 'Зелёная цветовая схема полигонов',
'zn' : '多边形配色方案绿色',
}[lang]
QStringDB.btn_poly_color_blue_tip = { 'en' : 'Poly color scheme blue',
'ru' : 'Синяя цветовая схема полигонов',
'zn' : '多边形配色方案蓝色',
}[lang]
QStringDB.btn_view_baked_mask_tip = { 'en' : 'View baked mask',
'ru' : 'Посмотреть запечёную маску',
'zn' : '查看遮罩通道',
}[lang]
QStringDB.btn_poly_type_include_tip = { 'en' : 'Poly include mode',
'ru' : 'Режим полигонов - включение',
'zn' : '多边形包含模式',
}[lang]
QStringDB.btn_poly_type_exclude_tip = { 'en' : 'Poly exclude mode',
'ru' : 'Режим полигонов - исключение',
'zn' : '多边形排除方式',
}[lang]
QStringDB.btn_undo_pt_tip = { 'en' : 'Undo point',
'ru' : 'Отменить точку',
'zn' : '撤消点',
}[lang]
QStringDB.btn_redo_pt_tip = { 'en' : 'Redo point',
'ru' : 'Повторить точку',
'zn' : '重做点',
}[lang]
QStringDB.btn_delete_poly_tip = { 'en' : 'Delete poly',
'ru' : 'Удалить полигон',
'zn' : '删除多边形',
}[lang]
QStringDB.btn_pt_edit_mode_tip = { 'en' : 'Edit point mode ( HOLD CTRL )',
'ru' : 'Режим правки точек',
'zn' : '编辑点模式 ( 按住CTRL )',
}[lang]
QStringDB.btn_prev_image_tip = { 'en' : 'Save and Prev image\nHold SHIFT : accelerate\nHold CTRL : skip non masked\n',
'ru' : 'Сохранить и предыдущее изображение\nУдерживать SHIFT : ускорить\nУдерживать CTRL : пропустить неразмеченные\n',
'zn' : '保存和上一张图片\n按住SHIFT : 加快\n按住CTRL : 跳过未标记的\n',
}[lang]
QStringDB.btn_next_image_tip = { 'en' : 'Save and Next image\nHold SHIFT : accelerate\nHold CTRL : skip non masked\n',
'ru' : 'Сохранить и следующее изображение\nУдерживать SHIFT : ускорить\nУдерживать CTRL : пропустить неразмеченные\n',
'zn' : '保存并下一张图片\n按住SHIFT : 加快\n按住CTRL : 跳过未标记的\n',
}[lang]

1265
XSegEditor/XSegEditor.py Normal file

File diff suppressed because it is too large Load diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

BIN
XSegEditor/gfx/icons/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

152
core/imagelib/SegIEPolys.py Normal file
View file

@ -0,0 +1,152 @@
import numpy as np
import cv2
from enum import IntEnum
class SegIEPolyType(IntEnum):
EXCLUDE = 0
INCLUDE = 1
class SegIEPoly():
def __init__(self, type=None, pts=None, **kwargs):
self.type = type
if pts is None:
pts = np.empty( (0,2), dtype=np.float32 )
else:
pts = np.float32(pts)
self.pts = pts
self.n_max = self.n = len(pts)
def dump(self):
return {'type': int(self.type),
'pts' : self.get_pts(),
}
def identical(self, b):
if self.n != b.n:
return False
return (self.pts[0:self.n] == b.pts[0:b.n]).all()
def get_type(self):
return self.type
def add_pt(self, x, y):
self.pts = np.append(self.pts[0:self.n], [ ( float(x), float(y) ) ], axis=0).astype(np.float32)
self.n_max = self.n = self.n + 1
def undo(self):
self.n = max(0, self.n-1)
return self.n
def redo(self):
self.n = min(len(self.pts), self.n+1)
return self.n
def redo_clip(self):
self.pts = self.pts[0:self.n]
self.n_max = self.n
def insert_pt(self, n, pt):
if n < 0 or n > self.n:
raise ValueError("insert_pt out of range")
self.pts = np.concatenate( (self.pts[0:n], pt[None,...].astype(np.float32), self.pts[n:]), axis=0)
self.n_max = self.n = self.n+1
def remove_pt(self, n):
if n < 0 or n >= self.n:
raise ValueError("remove_pt out of range")
self.pts = np.concatenate( (self.pts[0:n], self.pts[n+1:]), axis=0)
self.n_max = self.n = self.n-1
def get_last_point(self):
return self.pts[self.n-1].copy()
def get_pts(self):
return self.pts[0:self.n].copy()
def get_pts_count(self):
return self.n
def set_point(self, id, pt):
self.pts[id] = pt
def set_points(self, pts):
self.pts = np.array(pts)
self.n_max = self.n = len(pts)
class SegIEPolys():
def __init__(self):
self.polys = []
def identical(self, b):
polys_len = len(self.polys)
o_polys_len = len(b.polys)
if polys_len != o_polys_len:
return False
return all ([ a_poly.identical(b_poly) for a_poly, b_poly in zip(self.polys, b.polys) ])
def add_poly(self, ie_poly_type):
poly = SegIEPoly(ie_poly_type)
self.polys.append (poly)
return poly
def remove_poly(self, poly):
if poly in self.polys:
self.polys.remove(poly)
def has_polys(self):
return len(self.polys) != 0
def get_poly(self, id):
return self.polys[id]
def get_polys(self):
return self.polys
def get_pts_count(self):
return sum([poly.get_pts_count() for poly in self.polys])
def sort(self):
poly_by_type = { SegIEPolyType.EXCLUDE : [], SegIEPolyType.INCLUDE : [] }
for poly in self.polys:
poly_by_type[poly.type].append(poly)
self.polys = poly_by_type[SegIEPolyType.INCLUDE] + poly_by_type[SegIEPolyType.EXCLUDE]
def __iter__(self):
for poly in self.polys:
yield poly
def overlay_mask(self, mask):
h,w,c = mask.shape
white = (1,)*c
black = (0,)*c
for poly in self.polys:
pts = poly.get_pts().astype(np.int32)
if len(pts) != 0:
cv2.fillPoly(mask, [pts], white if poly.type == SegIEPolyType.INCLUDE else black )
def dump(self):
return {'polys' : [ poly.dump() for poly in self.polys ] }
@staticmethod
def load(data=None):
ie_polys = SegIEPolys()
if data is not None:
if isinstance(data, list):
# Backward comp
ie_polys.polys = [ SegIEPoly(type=type, pts=pts) for (type, pts) in data ]
elif isinstance(data, dict):
ie_polys.polys = [ SegIEPoly(**poly_cfg) for poly_cfg in data['polys'] ]
ie_polys.sort()
return ie_polys

View file

@ -16,6 +16,7 @@ from .color_transfer import color_transfer, color_transfer_mix, color_transfer_s
from .common import normalize_channels, cut_odd_image, overlay_alpha_image from .common import normalize_channels, cut_odd_image, overlay_alpha_image
from .IEPolys import IEPolys from .IEPolys import IEPolys
from .SegIEPolys import *
from .blursharpen import LinearMotionBlur, blursharpen from .blursharpen import LinearMotionBlur, blursharpen

262
core/qtex/QSubprocessor.py Normal file
View file

@ -0,0 +1,262 @@
import multiprocessing
import sys
import time
import traceback
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from core.interact import interact as io
from .qtex import *
class QSubprocessor(object):
"""
"""
class Cli(object):
def __init__ ( self, client_dict ):
s2c = multiprocessing.Queue()
c2s = multiprocessing.Queue()
self.p = multiprocessing.Process(target=self._subprocess_run, args=(client_dict,s2c,c2s) )
self.s2c = s2c
self.c2s = c2s
self.p.daemon = True
self.p.start()
self.state = None
self.sent_time = None
self.sent_data = None
self.name = None
self.host_dict = None
def kill(self):
self.p.terminate()
self.p.join()
#overridable optional
def on_initialize(self, client_dict):
#initialize your subprocess here using client_dict
pass
#overridable optional
def on_finalize(self):
#finalize your subprocess here
pass
#overridable
def process_data(self, data):
#process 'data' given from host and return result
raise NotImplementedError
#overridable optional
def get_data_name (self, data):
#return string identificator of your 'data'
return "undefined"
def log_info(self, msg): self.c2s.put ( {'op': 'log_info', 'msg':msg } )
def log_err(self, msg): self.c2s.put ( {'op': 'log_err' , 'msg':msg } )
def progress_bar_inc(self, c): self.c2s.put ( {'op': 'progress_bar_inc' , 'c':c } )
def _subprocess_run(self, client_dict, s2c, c2s):
self.c2s = c2s
data = None
try:
self.on_initialize(client_dict)
c2s.put ( {'op': 'init_ok'} )
while True:
msg = s2c.get()
op = msg.get('op','')
if op == 'data':
data = msg['data']
result = self.process_data (data)
c2s.put ( {'op': 'success', 'data' : data, 'result' : result} )
data = None
elif op == 'close':
break
time.sleep(0.001)
self.on_finalize()
c2s.put ( {'op': 'finalized'} )
except Exception as e:
c2s.put ( {'op': 'error', 'data' : data} )
if data is not None:
print ('Exception while process data [%s]: %s' % (self.get_data_name(data), traceback.format_exc()) )
else:
print ('Exception: %s' % (traceback.format_exc()) )
c2s.close()
s2c.close()
self.c2s = None
# disable pickling
def __getstate__(self):
return dict()
def __setstate__(self, d):
self.__dict__.update(d)
#overridable
def __init__(self, name, SubprocessorCli_class, no_response_time_sec = 0, io_loop_sleep_time=0.005):
if not issubclass(SubprocessorCli_class, QSubprocessor.Cli):
raise ValueError("SubprocessorCli_class must be subclass of QSubprocessor.Cli")
self.name = name
self.SubprocessorCli_class = SubprocessorCli_class
self.no_response_time_sec = no_response_time_sec
self.io_loop_sleep_time = io_loop_sleep_time
self.clis = []
#getting info about name of subprocesses, host and client dicts, and spawning them
for name, host_dict, client_dict in self.process_info_generator():
try:
cli = self.SubprocessorCli_class(client_dict)
cli.state = 1
cli.sent_time = 0
cli.sent_data = None
cli.name = name
cli.host_dict = host_dict
self.clis.append (cli)
except:
raise Exception (f"Unable to start subprocess {name}. Error: {traceback.format_exc()}")
if len(self.clis) == 0:
raise Exception ("Unable to start QSubprocessor '%s' " % (self.name))
#waiting subprocesses their success(or not) initialization
while True:
for cli in self.clis[:]:
while not cli.c2s.empty():
obj = cli.c2s.get()
op = obj.get('op','')
if op == 'init_ok':
cli.state = 0
elif op == 'log_info':
io.log_info(obj['msg'])
elif op == 'log_err':
io.log_err(obj['msg'])
elif op == 'error':
cli.kill()
self.clis.remove(cli)
break
if all ([cli.state == 0 for cli in self.clis]):
break
io.process_messages(0.005)
if len(self.clis) == 0:
raise Exception ( "Unable to start subprocesses." )
#ok some processes survived, initialize host logic
self.on_clients_initialized()
self.q_timer = QTimer()
self.q_timer.timeout.connect(self.tick)
self.q_timer.start(5)
#overridable
def process_info_generator(self):
#yield per process (name, host_dict, client_dict)
for i in range(min(multiprocessing.cpu_count(), 8) ):
yield 'CPU%d' % (i), {}, {}
#overridable optional
def on_clients_initialized(self):
#logic when all subprocesses initialized and ready
pass
#overridable optional
def on_clients_finalized(self):
#logic when all subprocess finalized
pass
#overridable
def get_data(self, host_dict):
#return data for processing here
raise NotImplementedError
#overridable
def on_data_return (self, host_dict, data):
#you have to place returned 'data' back to your queue
raise NotImplementedError
#overridable
def on_result (self, host_dict, data, result):
#your logic what to do with 'result' of 'data'
raise NotImplementedError
def tick(self):
for cli in self.clis[:]:
while not cli.c2s.empty():
obj = cli.c2s.get()
op = obj.get('op','')
if op == 'success':
#success processed data, return data and result to on_result
self.on_result (cli.host_dict, obj['data'], obj['result'])
self.sent_data = None
cli.state = 0
elif op == 'error':
#some error occured while process data, returning chunk to on_data_return
if 'data' in obj.keys():
self.on_data_return (cli.host_dict, obj['data'] )
#and killing process
cli.kill()
self.clis.remove(cli)
elif op == 'log_info':
io.log_info(obj['msg'])
elif op == 'log_err':
io.log_err(obj['msg'])
elif op == 'progress_bar_inc':
io.progress_bar_inc(obj['c'])
for cli in self.clis[:]:
if cli.state == 1:
if cli.sent_time != 0 and self.no_response_time_sec != 0 and (time.time() - cli.sent_time) > self.no_response_time_sec:
#subprocess busy too long
io.log_info ( '%s doesnt response, terminating it.' % (cli.name) )
self.on_data_return (cli.host_dict, cli.sent_data )
cli.kill()
self.clis.remove(cli)
for cli in self.clis[:]:
if cli.state == 0:
#free state of subprocess, get some data from get_data
data = self.get_data(cli.host_dict)
if data is not None:
#and send it to subprocess
cli.s2c.put ( {'op': 'data', 'data' : data} )
cli.sent_time = time.time()
cli.sent_data = data
cli.state = 1
if all ([cli.state == 0 for cli in self.clis]):
#gracefully terminating subprocesses
for cli in self.clis[:]:
cli.s2c.put ( {'op': 'close'} )
cli.sent_time = time.time()
while True:
for cli in self.clis[:]:
terminate_it = False
while not cli.c2s.empty():
obj = cli.c2s.get()
obj_op = obj['op']
if obj_op == 'finalized':
terminate_it = True
break
if (time.time() - cli.sent_time) > 30:
terminate_it = True
if terminate_it:
cli.state = 2
cli.kill()
if all ([cli.state == 2 for cli in self.clis]):
break
#finalizing host logic
self.q_timer.stop()
self.q_timer = None
self.on_clients_finalized()

83
core/qtex/QXIconButton.py Normal file
View file

@ -0,0 +1,83 @@
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from localization import StringsDB
from .QXMainWindow import *
class QXIconButton(QPushButton):
"""
Custom Icon button that works through keyEvent system, without shortcut of QAction
works only with QXMainWindow as global window class
currently works only with one-key shortcut
"""
def __init__(self, icon,
tooltip=None,
shortcut=None,
click_func=None,
first_repeat_delay=300,
repeat_delay=20,
):
super().__init__(icon, "")
self.setIcon(icon)
if shortcut is not None:
tooltip = f"{tooltip} ( {StringsDB['S_HOT_KEY'] }: {shortcut} )"
self.setToolTip(tooltip)
self.seq = QKeySequence(shortcut) if shortcut is not None else None
QXMainWindow.inst.add_keyPressEvent_listener ( self.on_keyPressEvent )
QXMainWindow.inst.add_keyReleaseEvent_listener ( self.on_keyReleaseEvent )
self.click_func = click_func
self.first_repeat_delay = first_repeat_delay
self.repeat_delay = repeat_delay
self.repeat_timer = None
self.op_device = None
self.pressed.connect( lambda : self.action(is_pressed=True) )
self.released.connect( lambda : self.action(is_pressed=False) )
def action(self, is_pressed=None, op_device=None):
if self.click_func is None:
return
if is_pressed is not None:
if is_pressed:
if self.repeat_timer is None:
self.click_func()
self.repeat_timer = QTimer()
self.repeat_timer.timeout.connect(self.action)
self.repeat_timer.start(self.first_repeat_delay)
else:
if self.repeat_timer is not None:
self.repeat_timer.stop()
self.repeat_timer = None
else:
self.click_func()
if self.repeat_timer is not None:
self.repeat_timer.setInterval(self.repeat_delay)
def on_keyPressEvent(self, ev):
key = ev.key()
if ev.isAutoRepeat():
return
if self.seq is not None:
if key == self.seq[0]:
self.action(is_pressed=True)
def on_keyReleaseEvent(self, ev):
key = ev.key()
if ev.isAutoRepeat():
return
if self.seq is not None:
if key == self.seq[0]:
self.action(is_pressed=False)

34
core/qtex/QXMainWindow.py Normal file
View file

@ -0,0 +1,34 @@
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QXMainWindow(QWidget):
"""
Custom mainwindow class that provides global single instance and event listeners
"""
inst = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if QXMainWindow.inst is not None:
raise Exception("QXMainWindow can only be one.")
QXMainWindow.inst = self
self.keyPressEvent_listeners = []
self.keyReleaseEvent_listeners = []
self.setFocusPolicy(Qt.WheelFocus)
def add_keyPressEvent_listener(self, func):
self.keyPressEvent_listeners.append (func)
def add_keyReleaseEvent_listener(self, func):
self.keyReleaseEvent_listeners.append (func)
def keyPressEvent(self, ev):
super().keyPressEvent(ev)
for func in self.keyPressEvent_listeners:
func(ev)
def keyReleaseEvent(self, ev):
super().keyReleaseEvent(ev)
for func in self.keyReleaseEvent_listeners:
func(ev)

3
core/qtex/__init__.py Normal file
View file

@ -0,0 +1,3 @@
from .qtex import *
from .QSubprocessor import *
from .QXIconButton import *

79
core/qtex/qtex.py Normal file
View file

@ -0,0 +1,79 @@
import numpy as np
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from localization import StringsDB
from .QXMainWindow import *
class QActionEx(QAction):
def __init__(self, icon, text, shortcut=None, trigger_func=None, shortcut_in_tooltip=False, is_checkable=False, is_auto_repeat=False ):
super().__init__(icon, text)
if shortcut is not None:
self.setShortcut(shortcut)
if shortcut_in_tooltip:
self.setToolTip( f"{text} ( {StringsDB['S_HOT_KEY'] }: {shortcut} )")
if trigger_func is not None:
self.triggered.connect(trigger_func)
if is_checkable:
self.setCheckable(True)
self.setAutoRepeat(is_auto_repeat)
def QImage_from_np(img):
if img.dtype != np.uint8:
raise ValueError("img should be in np.uint8 format")
h,w,c = img.shape
if c == 1:
fmt = QImage.Format_Grayscale8
elif c == 3:
fmt = QImage.Format_BGR888
elif c == 4:
fmt = QImage.Format_ARGB32
else:
raise ValueError("unsupported channel count")
return QImage(img.data, w, h, c*w, fmt )
def QImage_to_np(q_img):
q_img = q_img.convertToFormat(QImage.Format_BGR888)
width = q_img.width()
height = q_img.height()
b = q_img.constBits()
b.setsize(height * width * 3)
arr = np.frombuffer(b, np.uint8).reshape((height, width, 3))
return arr#[::-1]
def QPixmap_from_np(img):
return QPixmap.fromImage(QImage_from_np(img))
def QPoint_from_np(n):
return QPoint(*n.astype(np.int))
def QPoint_to_np(q):
return np.int32( [q.x(), q.y()] )
def QSize_to_np(q):
return np.int32( [q.width(), q.height()] )
class QDarkPalette(QPalette):
def __init__(self):
super().__init__()
self.setColor(QPalette.Window, QColor(53, 53, 53))
self.setColor(QPalette.WindowText, Qt.white)
self.setColor(QPalette.Base, QColor(25, 25, 25))
self.setColor(QPalette.AlternateBase, QColor(53, 53, 53))
self.setColor(QPalette.ToolTipBase, Qt.white)
self.setColor(QPalette.ToolTipText, Qt.white)
self.setColor(QPalette.Text, Qt.white)
self.setColor(QPalette.Button, QColor(53, 53, 53))
self.setColor(QPalette.ButtonText, Qt.white)
self.setColor(QPalette.BrightText, Qt.red)
self.setColor(QPalette.Link, QColor(42, 130, 218))
self.setColor(QPalette.Highlight, QColor(42, 130, 218))
self.setColor(QPalette.HighlightedText, Qt.black)

View file

@ -1,2 +1,2 @@
from .localization import get_default_ttf_font_name from .localization import StringsDB, system_language, get_default_ttf_font_name

View file

@ -4,6 +4,8 @@ import locale
system_locale = locale.getdefaultlocale()[0] system_locale = locale.getdefaultlocale()[0]
# system_locale may be nil # system_locale may be nil
system_language = system_locale[0:2] if system_locale is not None else "en" system_language = system_locale[0:2] if system_locale is not None else "en"
if system_language not in ['en','ru','zn']:
system_language = 'en'
windows_font_name_map = { windows_font_name_map = {
'en' : 'cour', 'en' : 'cour',
@ -28,3 +30,13 @@ def get_default_ttf_font_name():
if platform[0:3] == 'win': return windows_font_name_map.get(system_language, 'cour') if platform[0:3] == 'win': return windows_font_name_map.get(system_language, 'cour')
elif platform == 'darwin': return darwin_font_name_map.get(system_language, 'cour') elif platform == 'darwin': return darwin_font_name_map.get(system_language, 'cour')
else: return linux_font_name_map.get(system_language, 'cour') else: return linux_font_name_map.get(system_language, 'cour')
SID_HOT_KEY = 1
if system_language == 'en':
StringsDB = {'S_HOT_KEY' : 'hot key'}
elif system_language == 'ru':
StringsDB = {'S_HOT_KEY' : 'горячая клавиша'}
elif system_language == 'zn':
StringsDB = {'S_HOT_KEY' : '热键'}

20
main.py
View file

@ -264,26 +264,16 @@ if __name__ == "__main__":
p.set_defaults (func=process_dev_test) p.set_defaults (func=process_dev_test)
# ========== XSeg util # ========== XSeg util
xseg_parser = subparsers.add_parser( "xseg", help="XSeg utils.").add_subparsers() p = subparsers.add_parser( "xsegeditor", help="XSegEditor.")
def process_xseg_merge(arguments): def process_xsegeditor(arguments):
osex.set_process_lowest_prio() osex.set_process_lowest_prio()
from mainscripts import XSegUtil from XSegEditor import XSegEditor
XSegUtil.merge(arguments.input_dir) XSegEditor.start (Path(arguments.input_dir))
p = xseg_parser.add_parser( "merge", help="")
p.add_argument('--input-dir', required=True, action=fixPathAction, dest="input_dir") p.add_argument('--input-dir', required=True, action=fixPathAction, dest="input_dir")
p.set_defaults (func=process_xseg_merge) p.set_defaults (func=process_xsegeditor)
def process_xseg_split(arguments):
osex.set_process_lowest_prio()
from mainscripts import XSegUtil
XSegUtil.split(arguments.input_dir)
p = xseg_parser.add_parser( "split", help="")
p.add_argument('--input-dir', required=True, action=fixPathAction, dest="input_dir")
p.set_defaults (func=process_xseg_split)
def bad_args(arguments): def bad_args(arguments):
parser.print_help() parser.print_help()

View file

@ -8,3 +8,4 @@ scipy==1.4.1
colorama colorama
labelme==4.2.9 labelme==4.2.9
tensorflow-gpu==1.13.2 tensorflow-gpu==1.13.2
pyqt5

View file

@ -7,7 +7,7 @@ import numpy as np
from core.cv2ex import * from core.cv2ex import *
from DFLIMG import * from DFLIMG import *
from facelib import LandmarksProcessor from facelib import LandmarksProcessor
from core.imagelib import IEPolys from core.imagelib import IEPolys, SegIEPolys
class SampleType(IntEnum): class SampleType(IntEnum):
IMAGE = 0 #raw image IMAGE = 0 #raw image
@ -54,7 +54,7 @@ class Sample(object):
self.shape = shape self.shape = shape
self.landmarks = np.array(landmarks) if landmarks is not None else None self.landmarks = np.array(landmarks) if landmarks is not None else None
self.ie_polys = IEPolys.load(ie_polys) self.ie_polys = IEPolys.load(ie_polys)
self.seg_ie_polys = IEPolys.load(seg_ie_polys) self.seg_ie_polys = SegIEPolys.load(seg_ie_polys)
self.eyebrows_expand_mod = eyebrows_expand_mod if eyebrows_expand_mod is not None else 1.0 self.eyebrows_expand_mod = eyebrows_expand_mod if eyebrows_expand_mod is not None else 1.0
self.source_filename = source_filename self.source_filename = source_filename
self.person_name = person_name self.person_name = person_name

View file

@ -27,7 +27,7 @@ class SampleGeneratorFaceXSeg(SampleGeneratorBase):
for path in paths: for path in paths:
samples += SampleLoader.load (SampleType.FACE, path) samples += SampleLoader.load (SampleType.FACE, path)
seg_samples = [ sample for sample in samples if sample.seg_ie_polys.get_total_points() != 0] seg_samples = [ sample for sample in samples if sample.seg_ie_polys.get_pts_count() != 0]
seg_samples_len = len(seg_samples) seg_samples_len = len(seg_samples)
if seg_samples_len == 0: if seg_samples_len == 0:
raise Exception(f"No segmented faces found.") raise Exception(f"No segmented faces found.")