-
Notifications
You must be signed in to change notification settings - Fork 0
/
F.py
1251 lines (1114 loc) · 43.7 KB
/
F.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import dfl
from typing import Any, Callable
import numpy as np
from interact import interact as io
def print_stack():
import traceback
for line in traceback.format_stack():
print(line.strip())
def mid_point(points):
x = sum(p[0] for p in points) / len(points)
y = sum(p[1] for p in points) / len(points)
return x, y
def mid_point_by_range(points):
xmin = min(p[0] for p in points)
xmax = max(p[0] for p in points)
ymin = min(p[1] for p in points)
ymax = max(p[1] for p in points)
x = (xmin + xmax) / 2
y = (ymin + ymax) / 2
return x, y
def poly_area(points):
x = [p[0] for p in points]
y = [p[1] for p in points]
return 0.5 * np.abs(np.dot(x, np.roll(y, 1)) - np.dot(y, np.roll(x, 1)))
def angle_between(v1, v2):
def unit_vector(vector):
return vector / np.linalg.norm(vector)
v1_u = unit_vector(v1)
v2_u = unit_vector(v2)
return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))
def skip_no_face(data_dst_dir):
import os
import shutil
data_dst_aligned_dir = os.path.join(data_dst_dir, "aligned")
aligend = set([f.split('_')[0] for f in os.listdir(data_dst_aligned_dir)])
merged_dir = os.path.join(data_dst_dir, "merged")
merged_trash_dir = os.path.join(data_dst_dir, "merged_trash")
if os.path.exists(merged_trash_dir):
# raise Exception("Merge Dir Bak Exists")
shutil.rmtree(merged_trash_dir)
shutil.move(merged_dir, merged_trash_dir)
os.mkdir(merged_dir)
idx = 0
for f in io.progress_bar_generator(os.listdir(merged_trash_dir), "Skip No Face"):
name = os.path.splitext(f)[0]
ext = os.path.splitext(f)[-1]
if name in aligend:
idx += 1
src = os.path.join(merged_trash_dir, f)
dst = os.path.join(merged_dir, "%05d" % idx + ext)
shutil.copy(src, dst)
def cpu_count():
from multiprocessing import cpu_count as cs
return cs()
def get_root_path():
return dfl.get_root_path()
def get_time_str():
import time
return time.strftime('%Y_%m_%d_%H_%M_%S', time.localtime(time.time()))
def backup_model(model_name, model_path):
import os
import shutil
backup_path = os.path.join(model_path, "backup")
if not os.path.exists(backup_path):
os.mkdir(backup_path)
for file in os.listdir(model_path):
if os.path.isdir(os.path.join(model_path, file)):
continue
if file.startswith(model_name):
src = os.path.join(model_path, file)
dst = os.path.join(backup_path, file)
shutil.copy(src, dst)
def backup_model_move(model_name, model_path):
import os
import shutil
backup_path = os.path.join(model_path, "backup")
if not os.path.exists(backup_path):
return
move_path = os.path.join(model_path, "backup_move")
if os.path.exists(move_path):
shutil.rmtree(move_path)
os.mkdir(move_path)
for file in os.listdir(backup_path):
if os.path.isdir(os.path.join(model_path, file)):
continue
if file.startswith(model_name):
src = os.path.join(backup_path, file)
dst = os.path.join(move_path, file)
shutil.move(src, dst)
def has_backup(model_name, model_path):
import os
backup_path = os.path.join(model_path, "backup")
if not os.path.exists(backup_path):
return False
for file in os.listdir(model_path):
if os.path.isdir(os.path.join(model_path, file)):
continue
if file.startswith(model_name):
return True
return False
def restore_model(model_name, model_path):
import os
import shutil
backup_path = os.path.join(model_path, "backup")
if not os.path.exists(backup_path):
return
for file in os.listdir(backup_path):
if os.path.isdir(os.path.join(model_path, file)):
continue
if file.startswith(model_name):
src = os.path.join(backup_path, file)
dst = os.path.join(model_path, file)
shutil.copy(src, dst)
def extract():
import os
import shutil
from mainscripts import VideoEd
from mainscripts import Extractor
from interact import interact as io
root_dir = get_root_path()
extract_workspace = os.path.join(root_dir, "extract_workspace")
target_dir = os.path.join(extract_workspace, "aligned_")
valid_exts = [".mp4", ".avi", ".wmv", ".mkv", ".ts"]
fps = io.input_int("Enter FPS ( ?:help skip:fullfps ) : ", 0,
help_message="How many frames of every second of the video will be extracted.")
min_pixel = io.input_int("Enter Min Pixel ( ?:help skip: 512) : ", 512,
help_message="Min Pixel")
def file_filter(file):
if os.path.isdir(os.path.join(extract_workspace, file)):
return False
ext = os.path.splitext(file)[-1]
if ext not in valid_exts:
return False
return True
files = list(filter(file_filter, os.listdir(extract_workspace)))
files.sort()
pos = 0
for file in files:
pos += 1
io.log_info("@@@@@ Start Process %s, %d / %d" % (file, pos, len(files)))
# 提取图片
input_file = os.path.join(extract_workspace, file)
output_dir = os.path.join(extract_workspace, "extract_images")
if not os.path.exists(output_dir):
os.mkdir(output_dir)
for f in os.listdir(output_dir):
os.remove(os.path.join(output_dir, f))
VideoEd.extract_video(input_file, output_dir, output_ext="png", fps=fps)
io.log_info("@@@@@ Start Extract %s, %d / %d" % (file, pos, len(files)))
# 提取人脸
input_dir = output_dir
output_dir = os.path.join(extract_workspace, "_current")
debug_dir = os.path.join(extract_workspace, "debug")
Extractor.main(input_dir, output_dir, debug_dir, "s3fd", min_pixel=min_pixel)
# fanseg
io.log_info("@@@@@ Start FanSeg %s, %d / %d" % (file, pos, len(files)))
Extractor.extract_fanseg(output_dir)
# 复制到结果集
io.log_info("@@@@@ Start Move %s, %d / %d" % (file, pos, len(files)))
if not os.path.exists(target_dir):
os.mkdir(target_dir)
ts = get_time_str()
for f in os.listdir(output_dir):
src = os.path.join(output_dir, f)
dst = os.path.join(target_dir, "%s_%s" % (ts, f))
shutil.move(src, dst)
# 全部做完,删除该文件
io.log_info("@@@@@ Finish %s, %d / %d" % (file, pos, len(files)))
os.remove(os.path.join(extract_workspace, file))
os.rmdir(output_dir)
def extract_dst_image(workspace):
import os
from mainscripts import Extractor
# 提取人脸
input_dir = os.path.join(workspace, "data_dst")
output_dir = os.path.join(workspace, "data_dst/aligned")
debug_dir = os.path.join(workspace, "data_dst/aligned_debug")
Extractor.main(input_dir, output_dir, debug_dir, "s3fd", manual_fix=True)
# fanseg
Extractor.extract_fanseg(output_dir)
# noinspection PyUnresolvedReferences
def get_pitch_yaw_roll(input_path, r=0.05):
import os
import numpy as np
import cv2
from shutil import copyfile
from pathlib import Path
from utils import Path_utils
from utils.DFLPNG import DFLPNG
from utils.DFLJPG import DFLJPG
from facelib import LandmarksProcessor
from joblib import Subprocessor
import multiprocessing
from interact import interact as io
from imagelib import estimate_sharpness
io.log_info("Sorting by face yaw...")
img_list = []
trash_img_list = []
for filepath in io.progress_bar_generator(Path_utils.get_image_paths(input_path), "Loading"):
filepath = Path(filepath)
if filepath.suffix == '.png':
dflimg = DFLPNG.load(str(filepath))
elif filepath.suffix == '.jpg':
dflimg = DFLJPG.load(str(filepath))
else:
dflimg = None
if dflimg is None:
io.log_err("%s is not a dfl image file" % (filepath.name))
trash_img_list.append([str(filepath)])
continue
pitch, yaw, roll = LandmarksProcessor.estimate_pitch_yaw_roll(dflimg.get_landmarks())
img_list.append([str(filepath), pitch, yaw, roll])
img_list.sort(key=lambda item: item[1])
with open(os.path.join(input_path, "_pitch_yaw_roll.csv"), "w") as f:
for i in img_list:
f.write("%s,%f,%f,%f\n" % (os.path.basename(i[0]), i[1], i[2], i[3]))
import cv
width = 800
img = cv.cv_new((width, width))
xs = [i[1] for i in img_list]
ys = [i[2] for i in img_list]
cs = [(128, 128, 128)] * len(xs)
rs = [int(r * width / 2)] * len(xs)
cv.cv_scatter(img, xs, ys, [-1, 1], [-1, 1], cs, rs)
cs = [(0xcc, 0x66, 0x33)] * len(xs)
rs = [2] * len(xs)
cv.cv_scatter(img, xs, ys, [-1, 1], [-1, 1], cs, rs)
cv.cv_save(img, os.path.join(input_path, "_pitch_yaw_roll.bmp"))
return img_list
def get_image_var(img_path):
import cv2
image = cv2.imread(img_path)
img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image_var = cv2.Laplacian(img_gray, cv2.CV_64F).var()
return image_var
def show_landmarks(path):
import cv
from utils.DFLJPG import DFLJPG
jpg = DFLJPG.load(path)
img = cv.cv_load(path)
lm = jpg.get_landmarks()
for (x, y) in lm:
cv.cv_point(img, (x, y), (255, 0, 0))
cv.cv_show(img)
def skip_spec_pitch(input_path):
import os
img_list = get_pitch_yaw_roll(input_path)
for [path, pitch, _, _] in img_list:
if pitch > 0.2:
print(path)
os.remove(path)
def pick_spec_pitch(input_path, output_path):
import shutil
import os
if not os.path.exists(output_path):
os.makedirs(output_path)
img_list = get_pitch_yaw_roll(input_path)
for [path, pitch, _yaw, _roll] in img_list:
if pitch > 0:
print(path)
shutil.copy(path, output_path)
def get_desktop_path():
return "C:/users/yml/desktop"
def csv_name():
return "_pitch_yaw_roll.csv"
def skip_by_pitch(src_path, dst_path):
import os
import shutil
import cv
size = 800
r = 20
src_img_list = get_pitch_yaw_roll(src_path)
dst_img_list = get_pitch_yaw_roll(dst_path)
trash_path = dst_path + "_trash"
if not os.path.exists(trash_path):
os.makedirs(trash_path)
img = cv.cv_new((size + 1, size + 1))
trans: Callable[[Any], int] = lambda v: int((v + 1) * size / 2)
count = 0
for [_, pitch, yaw, _] in src_img_list:
x = trans(pitch)
y = trans(yaw)
cv.cv_point(img, (x, y), (128, 128, 128), r)
# cv.cv_show(img)
xys = []
for [path, pitch, yaw, _] in dst_img_list:
x = trans(pitch)
y = trans(yaw)
c = img[y, x]
c_ = img[-y, x]
if sum(c) == 255 * 3 and sum(c_) == 255 * 3:
xys.append((x, y, (0, 0, 0xff)))
if not os.path.exists(path) or not os.path.exists(trash_path):
continue
count += 1
shutil.move(path, trash_path)
else:
xys.append((x, y, (0xcc, 0x66, 0x33)))
for (x, y, color) in xys:
cv.cv_point(img, (x, y), color, 2)
# cv.cv_show(img)
io.log_info("Out Of Pitch, %d / %d" % (count, len(dst_img_list)))
save_path = os.path.join(dst_path, "_skip_by_pitch.bmp")
cv.cv_save(img, save_path)
def split(input_path, target_path, batch=3000):
import os
import shutil
count = 0
if not os.path.exists(target_path):
os.mkdir(target_path)
dst_dir = os.path.join(target_path, "split_%03d" % int(count / batch))
for f in io.progress_bar_generator(os.listdir(input_path), "Process"):
if not f.endswith(".jpg") and not f.endswith(".png"):
continue
if count % batch == 0:
dst_dir = os.path.join(target_path, "split_%03d" % int(count / batch))
os.mkdir(dst_dir)
src = os.path.join(input_path, f)
shutil.move(src, dst_dir)
count += 1
def merge(input_path, target_path):
import os
import shutil
for f in os.listdir(input_path):
sub_path = os.path.join(input_path, f)
if os.path.abspath(sub_path) == os.path.abspath(target_path):
continue
if os.path.isdir(sub_path):
time_str = get_time_str()
for img in io.progress_bar_generator(os.listdir(sub_path), f):
if img.endswith(".png") or img.endswith(".jpg"):
img_path = os.path.join(sub_path, img)
dst_path = os.path.join(target_path, "%s_%s" % (time_str, img))
shutil.move(img_path, dst_path)
def match_by_pitch(data_src_path, data_dst_path):
r = 0.05
mn = 1
mx = 3
import cv
import shutil
# 准备各种路径
src_aligned_store = os.path.join(data_src_path, "aligned_store")
if not os.path.exists(src_aligned_store):
raise Exception("No Src Aligned Store")
src_aligned = os.path.join(data_dst_path, "src")
if os.path.exists(src_aligned):
shutil.rmtree(src_aligned)
os.mkdir(src_aligned)
dst_aligned = os.path.join(data_dst_path, "aligned")
dst_aligned_trash = os.path.join(data_dst_path, "aligned_trash")
if not os.path.exists(dst_aligned_trash):
os.mkdir(dst_aligned_trash)
# 读取角度信息
src_img_list = get_pitch_yaw_roll(src_aligned_store)
dst_img_list = get_pitch_yaw_roll(dst_aligned)
src_pitch = list([i[1] for i in src_img_list])
src_yaw = list([i[2] for i in src_img_list])
dst_pitch = list([i[1] for i in dst_img_list])
dst_yaw = list([i[2] for i in dst_img_list])
src_ps = np.array(list(zip(src_pitch, src_yaw)), "float")
dst_ps = np.array(list(zip(dst_pitch, dst_yaw)), "float")
# 计算最近的n个点
src_match = set()
dst_match = set()
for p, i in io.progress_bar_generator(zip(dst_ps, range(len(dst_ps))), "Calculating"):
ds = np.linalg.norm(src_ps - p, axis=1, keepdims=True)
idxs = np.argsort(ds, axis=0)
min_idx = idxs[mn - 1][0]
# 极端情况所有距离都不满足半径范围
if ds[min_idx] > r:
continue
# 至少有一个满足半径条件了,dst_point可以留下
dst_match.add(i)
# 所有满足条件的加入到src_match
for idx in idxs[:mx]:
idx = idx[0]
if ds[idx] > r:
break
src_match.add(idx)
io.log_info("%s, %s, %s, %s" % ("Src Match", len(src_match), "Src All", len(src_img_list)))
io.log_info("%s, %s, %s, %s" % ("Dst Match", len(dst_match), "Dst All", len(dst_img_list)))
# 画图
width = 800
xycr = []
for idx in range(len(src_img_list)):
t = src_img_list[idx]
if idx in src_match:
xycr.append([t[1], t[2], (128, 128, 128), int(r * width / 2)]) # 蓝色,匹配到的
shutil.copy(t[0], src_aligned)
else:
xycr.append([t[1], t[2], (128, 128, 128), 2]) # 灰色,没匹配到
for idx in range(len(dst_img_list)):
t = dst_img_list[idx]
if idx in dst_match:
xycr.append([t[1], t[2], (0, 255, 0), 2]) # 绿色,保留
else:
xycr.append([t[1], t[2], (0, 0, 255), 2]) # 红色,删除
shutil.move(t[0], dst_aligned_trash)
img = cv.cv_new((width, width))
xs = [i[0] for i in xycr]
ys = [i[1] for i in xycr]
cs = [i[2] for i in xycr]
rs = [i[3] for i in xycr]
cv.cv_scatter(img, xs, ys, [-1, 1], [-1, 1], cs, rs)
cv.cv_save(img, os.path.join(dst_aligned, "_match_by_pitch.bmp"))
# 加入base
base_dir = os.path.join(data_src_path, "aligned_base")
if os.path.exists(base_dir):
for img in os.listdir(base_dir):
if img.endswith(".jpg") or img.endswith(".png"):
img_path = os.path.join(base_dir, img)
shutil.copy(img_path, src_aligned)
# noinspection PyUnresolvedReferences
def recover_filename(input_path):
from mainscripts import Util
Util.recover_original_aligned_filename(input_path)
def recover_filename_if_nessesary(input_path):
# 恢复排序
need_recover = True
for img in os.listdir(input_path):
if img.endswith("_0.jpg") or img.endswith("_0.png"):
need_recover = False
break
if need_recover:
recover_filename(input_path)
def low_prio():
from utils import os_utils
os_utils.set_process_lowest_prio()
def manual_select(input_path, src_path=None):
import cv
import colorsys
import cv2
img_list = []
src_img_list = []
width = 800
ratio = 0.8
for f in io.progress_bar_generator(os.listdir(input_path), "Loading"):
if f.endswith(".jpg") or f.endswith(".png"):
fpath = os.path.join(input_path, f)
dfl_img = dfl.dfl_load_img(fpath)
p, y, _ = dfl.dfl_estimate_pitch_yaw_roll(dfl_img)
fno = int(f.split(".")[0])
img_list.append([fno, p, y])
# for i in range(10000):
# img_list.append([i,
# random.random() * 2 - 1,
# random.random() * 2 - 1])
src_img_list = []
src_cur_list = []
img_list = np.array(img_list, "float")
cur_list = img_list
src_r = width / 100 * 2.5
redius = width / 100 * 2
trans_pitch_to_x = cv.trans_fn(-1, 1, 0, width)
trans_yaw_to_y = cv.trans_fn(-1, 1, 0, width)
trans_x_to_pitch = cv.trans_fn(0, width, -1, 1)
trans_y_to_yaw = cv.trans_fn(0, width, -1, 1)
trans_r = cv.trans_fn(0, width, 0, 2)
cur_pitch_yaw = img_list[:, 1:3]
img = cv.cv_new((width, width))
cur_w = 2
cur_mid = (0, 0)
def reload_src():
nonlocal src_img_list
nonlocal src_cur_list
src_img_list = []
if src_path:
for f in io.progress_bar_generator(os.listdir(src_path), "Loading"):
if f.endswith(".jpg") or f.endswith(".png"):
fpath = os.path.join(src_path, f)
dfl_img = dfl.dfl_load_img(fpath)
p, y, _ = dfl.dfl_estimate_pitch_yaw_roll(dfl_img)
src_img_list.append([fno, p, y])
src_img_list.append([fno, p, -y])
src_img_list = np.array(src_img_list, "float")
src_cur_list = src_img_list
def repaint():
nonlocal trans_pitch_to_x
nonlocal trans_yaw_to_y
nonlocal trans_x_to_pitch
nonlocal trans_y_to_yaw
nonlocal trans_r
nonlocal src_cur_list
nonlocal cur_list
nonlocal cur_pitch_yaw
nonlocal img
nonlocal src_path
mid = cur_mid
w = cur_w
sx = mid[0] - w / 2
sy = mid[1] - w / 2
ex = mid[0] + w / 2
ey = mid[1] + w / 2
idxs = (img_list[:, 1] >= sx) & \
(img_list[:, 2] >= sy) & \
(img_list[:, 1] <= ex) & \
(img_list[:, 2] <= ey)
cur_list = img_list[idxs]
cur_pitch_yaw = cur_list[:, 1:3]
if len(src_img_list) == 0:
src_cur_list = []
elif src_path:
idxs = (src_img_list[:, 1] >= sx) & \
(src_img_list[:, 2] >= sy) & \
(src_img_list[:, 1] <= ex) & \
(src_img_list[:, 2] <= ey)
src_cur_list = src_img_list[idxs]
trans_pitch_to_x = cv.trans_fn(sx, ex, 0, width)
trans_yaw_to_y = cv.trans_fn(sy, ey, 0, width)
trans_x_to_pitch = cv.trans_fn(0, width, sx, ex)
trans_y_to_yaw = cv.trans_fn(0, width, sy, ey)
trans_r = cv.trans_fn(0, width, 0, w)
img = cv.cv_new((width, width))
min_fno = int(cur_list[0][0])
max_fno = int(cur_list[-1][0])
trans_color = cv.trans_fn(min_fno, max_fno, 0, 1)
for _, p, y in src_cur_list:
cv.cv_point(img, (trans_pitch_to_x(p), trans_yaw_to_y(y)), (192, 192, 192), src_r * 2 / cur_w)
for f, p, y in cur_list:
fno = int(f)
h = trans_color(fno)
s = 1
v = 1
r, g, b = colorsys.hsv_to_rgb(h, s, v)
cv.cv_point(img, (trans_pitch_to_x(p), trans_yaw_to_y(y)), (b * 255, g * 255, r * 255), 2)
cv2.imshow("select", img)
def mouse_callback(event, x, y, flags, param):
nonlocal cur_mid
nonlocal cur_w
x = trans_x_to_pitch(x)
y = trans_y_to_yaw(y)
if event == cv2.EVENT_LBUTTONDOWN:
tr = trans_r(redius)
point = np.array([[x, y]] * len(cur_pitch_yaw), "float")
dist = np.linalg.norm(cur_pitch_yaw - point, axis=1)
idxs = dist <= tr
for f, _, _ in cur_list[idxs]:
print(f)
pass
print("-----------------------------------------")
elif event == cv2.EVENT_RBUTTONDOWN:
cur_mid = (x, y)
cur_w = cur_w * ratio
repaint()
elif event == cv2.EVENT_MBUTTONDOWN:
cur_w = cur_w / ratio
if cur_w >= 2:
cur_w = 2
cur_mid = (0, 0)
repaint()
reload_src()
cv2.namedWindow("select")
cv2.setMouseCallback("select", mouse_callback)
while True:
repaint()
key = cv2.waitKey()
if key == 13 or key == -1:
break
elif key == 114:
reload_src()
def prepare(workspace, detector="s3fd", manual_fix=False):
import os
import shutil
from mainscripts import Extractor
from mainscripts import VideoEd
for f in os.listdir(workspace):
ext = os.path.splitext(f)[-1]
if ext not in ['.mp4', '.avi']:
continue
if f.startswith("result"):
continue
# 获取所有的data_dst文件
tmp_dir = os.path.join(workspace, "_tmp")
tmp_aligned = os.path.join(tmp_dir, "aligned")
tmp_video_dir = os.path.join(tmp_dir, "video")
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
if not os.path.exists(tmp_dir):
os.mkdir(tmp_dir)
os.mkdir(tmp_video_dir)
video = os.path.join(workspace, f)
# 提取帧
VideoEd.extract_video(video, tmp_dir, "png", 0)
# 提取人脸
if detector == "manual":
import winsound
winsound.Beep(300, 500)
Extractor.main(tmp_dir, tmp_aligned, detector=detector, manual_fix=manual_fix)
# fanseg
Extractor.extract_fanseg(tmp_aligned)
if detector != "manual":
# # 两组人脸匹配
# skip_by_pitch(os.path.join(workspace, "data_src", "aligned"), os.path.join(tmp_dir, "aligned"))
# 排序
dfl.dfl_sort_by_hist(tmp_aligned)
# 保存video
shutil.copy(video, tmp_video_dir)
# 重命名
fname = f.replace(ext, "")
dst_dir = os.path.join(workspace, "data_dst_%s_%s" % (get_time_str(), fname))
shutil.move(tmp_dir, dst_dir)
# 移动video
data_trash = os.path.join(workspace, "../trash_workspace")
if not os.path.exists(data_trash):
os.mkdir(data_trash)
shutil.move(video, data_trash)
import winsound
winsound.Beep(300, 500)
def train(workspace, model="SAE"):
import os
model_dir = os.path.join(workspace, "model")
for f in os.listdir(workspace):
if not os.path.isdir(os.path.join(workspace, f)) or not f.startswith("data_dst_"):
continue
io.log_info(f)
data_dst = os.path.join(workspace, f)
data_src_aligned = os.path.join(data_dst, "src")
if not os.path.exists(data_src_aligned):
data_src_aligned = os.path.join(workspace, "data_src", "aligned")
data_dst_aligned = os.path.join(data_dst, "aligned")
# 训练
dfl.dfl_train(data_src_aligned, data_dst_aligned, model_dir, model)
return
def train_dst(workspace, model="SAE"):
import os
model_dir = os.path.join(workspace, "model")
data_src_aligned = os.path.join(workspace, "data_src", "aligned")
data_dst_aligned = os.path.join(workspace, "data_dst", "aligned")
# 训练
dfl.dfl_train(data_src_aligned, data_dst_aligned, model_dir, model=model)
def convert(workspace, skip=False, model="SAE"):
import os
for f in os.listdir(workspace):
if not os.path.isdir(os.path.join(workspace, f)) or not f.startswith("data_dst_"):
continue
io.log_info(f)
model_dir = os.path.join(workspace, "model")
self_model_dir = os.path.join(workspace, f, "model")
if os.path.exists(self_model_dir):
io.log_info("Use Self Model")
model_dir = self_model_dir
data_dst = os.path.join(workspace, f)
data_dst_merged = os.path.join(data_dst, "merged")
data_dst_aligned = os.path.join(data_dst, "aligned")
data_dst_video = os.path.join(data_dst, "video")
refer_path = None
for v in os.listdir(data_dst_video):
if v.split(".")[-1] in ["mp4", "avi", "wmv", "mkv"]:
refer_path = os.path.join(data_dst_video, v)
break
if not refer_path:
io.log_err("No Refer File In " + data_dst_video)
return
# 恢复排序
need_recover = True
for img in os.listdir(data_dst_aligned):
if img.endswith("_0.jpg") or img.endswith("_0.png"):
need_recover = False
break
if need_recover:
recover_filename(data_dst_aligned)
# 如果data_dst里没有脸则extract
has_img = False
for img in os.listdir(data_dst):
if img.endswith(".jpg") or img.endswith(".png"):
has_img = True
break
if not has_img:
dfl.dfl_extract_video(refer_path, data_dst)
# 转换
dfl.dfl_convert(data_dst, data_dst_merged, data_dst_aligned, model_dir, model)
# ConverterMasked.enable_predef = enable_predef
# 去掉没有脸的
# if skip:
# skip_no_face(data_dst)
# 转mp4
refer_name = ".".join(os.path.basename(refer_path).split(".")[:-1])
result_path = os.path.join(workspace, "result_%s_%s.mp4" % (get_time_str(), refer_name))
dfl.dfl_video_from_sequence(data_dst_merged, result_path, refer_path)
# 移动到trash
trash_dir = os.path.join(workspace, "../trash_workspace")
import shutil
shutil.move(data_dst, trash_dir)
def convert_dst(workspace, model="SAE"):
import os
model_dir = os.path.join(workspace, "model")
data_dst = os.path.join(workspace, "data_dst")
data_dst_merged = os.path.join(data_dst, "merged")
data_dst_aligned = os.path.join(data_dst, "aligned")
# 转换
dfl.dfl_convert(data_dst, data_dst_merged, data_dst_aligned, model_dir, model=model)
def edit_mask(workspace):
dst = get_workspace_dst(workspace)
dst_aligned = os.path.join(dst, "aligned")
_, confirmed, _ = dfl.dfl_edit_mask(dst_aligned)
import shutil
for f in os.listdir(confirmed):
shutil.move(os.path.join(confirmed, f), dst_aligned)
def edit_mask_dst(workspace):
dst = os.path.join(workspace, "data_dst")
dst_aligned = os.path.join(dst, "aligned")
_, confirmed, _ = dfl.dfl_edit_mask(dst_aligned)
import shutil
for f in os.listdir(confirmed):
shutil.move(os.path.join(confirmed, f), dst_aligned)
def refix(workspace):
dst = get_workspace_dst(workspace)
dst_aligned = os.path.join(dst, "aligned")
recover_filename_if_nessesary(dst_aligned)
extract_imgs = [f if f.endswith(".jpg") or f.endswith(".png") else "" for f in os.listdir(dst)]
max_img_no = int(max(extract_imgs).split(".")[0])
ext = extract_imgs[0].split(".")[1]
aligned_imgs = list(sorted(filter(lambda x: x is not None,
[f if f.endswith(".jpg") or f.endswith(".png") else None for f in
os.listdir(dst_aligned)])))
need_fix_no = []
i = 0 # 当前文件下标
j = 1 # 期望文件名
while i <= max_img_no and j <= max_img_no and i < len(aligned_imgs):
if aligned_imgs[i].startswith("%05d" % j):
i += 1
j += 1
else:
# print(aligned_imgs[i], j)
need_fix_no.append(j)
j += 1
for k in range(j, max_img_no + 1):
need_fix_no.append(k)
# print(k)
if len(need_fix_no) == 0:
return
fix_workspace = os.path.join(dst, "fix")
import shutil
if os.path.exists(fix_workspace):
shutil.rmtree(fix_workspace)
os.mkdir(fix_workspace)
for no in need_fix_no:
f = os.path.join(dst, "%05d.%s" % (no, ext))
io.log_info(f)
shutil.copy(f, fix_workspace)
fix_workspace_aligned = os.path.join(fix_workspace, "aligned")
from mainscripts import Extractor
Extractor.main(fix_workspace, fix_workspace_aligned, detector="manual", manual_fix=False)
Extractor.extract_fanseg(fix_workspace_aligned)
for f in os.listdir(fix_workspace_aligned):
f = os.path.join(fix_workspace_aligned, f)
io.log_info(f)
shutil.move(f, dst_aligned)
shutil.rmtree(fix_workspace)
def mp4(workspace, skip=False):
import os
for f in os.listdir(workspace):
if not os.path.isdir(os.path.join(workspace, f)) or not f.startswith("data_dst_"):
continue
io.log_info(f)
data_dst = os.path.join(workspace, f)
data_dst_merged = os.path.join(data_dst, "merged")
data_dst_aligned = os.path.join(data_dst, "aligned")
data_dst_video = os.path.join(data_dst, "video")
refer_path = None
for v in os.listdir(data_dst_video):
if v.split(".")[-1] in ["mp4", "avi", "wmv", "mkv"]:
refer_path = os.path.join(data_dst_video, v)
break
if not refer_path:
io.log_err("No Refer File In " + data_dst_video)
return
io.log_info("Refer File " + refer_path)
# 恢复排序
need_recover = True
for img in os.listdir(data_dst_aligned):
if img.endswith("_0.jpg") or img.endswith("_0.png"):
need_recover = False
if need_recover:
recover_filename(data_dst_aligned)
# 如果data_dst里没有脸则extract
has_img = False
for img in os.listdir(data_dst):
if img.endswith(".jpg") or img.endswith(".png"):
has_img = True
break
if not has_img:
dfl.dfl_extract_video(refer_path, data_dst)
# 去掉没有脸的
# if skip:
# skip_no_face(data_dst)
# 转mp4
refer_name = ".".join(os.path.basename(refer_path).split(".")[:-1])
result_path = os.path.join(workspace, "result_%s_%s.mp4" % (get_time_str(), refer_name))
dfl.dfl_video_from_sequence(data_dst_merged, result_path, refer_path)
# 移动到trash
trash_dir = os.path.join(workspace, "../trash_workspace")
import shutil
shutil.move(data_dst, trash_dir)
def step(workspace):
import shutil
for f in os.listdir(workspace):
if os.path.isdir(os.path.join(workspace, f)) and f.startswith("data_dst_"):
model = os.path.join(workspace, "model")
model_dst = os.path.join(workspace, f, "model")
if not os.path.exists(model_dst):
io.log_info("Move Model Files To %s" % f)
os.mkdir(model_dst)
for m in os.listdir(model):
mf = os.path.join(model, m)
if os.path.isfile(mf):
shutil.copy(os.path.join(model, m), model_dst)
src = os.path.join(workspace, f)
dst = os.path.join(workspace, "../trash_workspace")
io.log_info("Move %s To %s" % (src, dst))
shutil.move(src, dst)
return
def auto(workspace):
import subprocess
for f in os.listdir(workspace):
if os.path.isdir(os.path.join(get_root_path(), "workspace", f)) and f.startswith("data_dst_"):
train_bat = os.path.join(get_root_path(), "auto_train.bat")
convert_bat = os.path.join(get_root_path(), "auto_convert.bat")
step_bat = os.path.join(get_root_path(), "auto_step.bat")
subprocess.call([train_bat])
subprocess.call([convert_bat])
subprocess.call([step_bat])
io.log_info("Finish " + f)
def select(exists_path, pool_path, div=200):
# 先计算output_path的已有图像
import cv
import dfl
import random
width = 800
trans = cv.trans_fn(-1, 1, 0, width)
img = cv.cv_new((width, width))
for f in io.progress_bar_generator(os.listdir(exists_path), "Existing Imgs"):
if f.endswith(".png") or f.endswith("jpg"):
img_path = os.path.join(exists_path, f)
dfl_img = dfl.dfl_load_img(img_path)
pitch, yaw, _ = dfl.dfl_estimate_pitch_yaw_roll(dfl_img)
pitch = trans(pitch)
yaw = trans(yaw)
cv.cv_circle(img, (pitch, yaw), (128, 128, 128), width / div, -1)
time_str = get_time_str()
import shutil
pool_files = list(os.listdir(pool_path))
# random.shuffle(pool_files)
count = 0
for f in io.progress_bar_generator(pool_files, os.path.basename(pool_path)):
if f.endswith(".png") or f.endswith(".jpg"):
img_path = os.path.join(pool_path, f)
dfl_img = dfl.dfl_load_img(img_path)
pitch, yaw, _ = dfl.dfl_estimate_pitch_yaw_roll(dfl_img)
pitch = trans(pitch)
yaw = trans(yaw)
if sum(img[yaw][pitch]) == 255 * 3:
dst = os.path.join(exists_path, "%s_%s" % (time_str, f))
shutil.copy(img_path, dst)
count += 1
cv.cv_circle(img, (pitch, yaw), (0xcc, 0x66, 0x33), width / div, -1)
cv.cv_save(img, os.path.join(exists_path, "_select.bmp"))
io.log_info("Copy %d, Total %d" % (count, len(pool_files)))
def sync_trash(trash_path, pool_path):
import shutil
count = 0
for f in io.progress_bar_generator(os.listdir(trash_path), "Trash Files"):
if f.endswith(".jpg") or f.endswith(".png"):
img_name = f.split("_")[-1]
img_path = os.path.join(pool_path, img_name)
dst_path = os.path.join(trash_path, "_origin")
if os.path.exists(img_path):
shutil.move(img_path, dst_path)
count += 1
io.log_info("Trash %d" % count)
def get_first_dst(workspace):
for f in os.listdir(workspace):
if f.startswith("data_dst_"):
return os.path.join(workspace, f)
def auto_skip_by_pitch():
workspace = os.path.join(get_root_path(), "workspace")
for f in os.listdir(workspace):
if f.startswith("data_dst_"):
dst = os.path.join(workspace, f, "aligned")
src = os.path.join(workspace, "data_src/aligned")
skip_by_pitch(src, dst)
break
def auto_extract_to_img():
workspace = os.path.join(get_root_path(), "workspace")
data_dst = None