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
|
- import multiprocessing
- import operator
- import pickle
- import traceback
- from pathlib import Path
- import samplelib.PackedFaceset
- from core import pathex
- from core.interact import interact as io
- from core.joblib import Subprocessor
- from DFLIMG import *
- from facelib import FaceType, LandmarksProcessor
- from .Sample import Sample, SampleType
- class SampleLoader:
- samples_cache = dict()
- @staticmethod
- def get_person_id_max_count(samples_path):
- samples = None
- try:
- samples = samplelib.PackedFaceset.load(samples_path)
- except:
- io.log_err(f"Error occured while loading samplelib.PackedFaceset.load {str(samples_dat_path)}, {traceback.format_exc()}")
- if samples is None:
- raise ValueError("packed faceset not found.")
- persons_name_idxs = {}
- for sample in samples:
- persons_name_idxs[sample.person_name] = 0
- return len(list(persons_name_idxs.keys()))
- @staticmethod
- def load(sample_type, samples_path, subdirs=False):
- samples_cache = SampleLoader.samples_cache
- if str(samples_path) not in samples_cache.keys():
- samples_cache[str(samples_path)] = [None]*SampleType.QTY
- samples = samples_cache[str(samples_path)]
- if sample_type == SampleType.IMAGE:
- if samples[sample_type] is None:
- samples[sample_type] = [ Sample(filename=filename) for filename in io.progress_bar_generator( pathex.get_image_paths(samples_path, subdirs=subdirs), "Loading") ]
- elif sample_type == SampleType.FACE:
- if samples[sample_type] is None:
- try:
- result = samplelib.PackedFaceset.load(samples_path)
- except:
- io.log_err(f"Error occured while loading samplelib.PackedFaceset.load {str(samples_dat_path)}, {traceback.format_exc()}")
- if result is not None:
- io.log_info (f"Loaded {len(result)} packed faces from {samples_path}")
- if result is None:
- result = SampleLoader.load_face_samples( pathex.get_image_paths(samples_path, subdirs=subdirs) )
- samples[sample_type] = result
- elif sample_type == SampleType.FACE_TEMPORAL_SORTED:
- result = SampleLoader.load (SampleType.FACE, samples_path)
- result = SampleLoader.upgradeToFaceTemporalSortedSamples(result)
- samples[sample_type] = result
- return samples[sample_type]
- @staticmethod
- def load_face_samples ( image_paths):
- result = FaceSamplesLoaderSubprocessor(image_paths).run()
- sample_list = []
- for filename, \
- ( face_type,
- shape,
- landmarks,
- ie_polys,
- seg_ie_polys,
- eyebrows_expand_mod,
- source_filename,
- ) in result:
- sample_list.append( Sample(filename=filename,
- sample_type=SampleType.FACE,
- face_type=FaceType.fromString (face_type),
- shape=shape,
- landmarks=landmarks,
- ie_polys=ie_polys,
- seg_ie_polys=seg_ie_polys,
- eyebrows_expand_mod=eyebrows_expand_mod,
- source_filename=source_filename,
- ))
- return sample_list
- """
- @staticmethod
- def load_face_samples ( image_paths):
- sample_list = []
- for filename in io.progress_bar_generator (image_paths, desc="Loading"):
- dflimg = DFLIMG.load (Path(filename))
- if dflimg is None:
- io.log_err (f"{filename} is not a dfl image file.")
- else:
- sample_list.append( Sample(filename=filename,
- sample_type=SampleType.FACE,
- face_type=FaceType.fromString ( dflimg.get_face_type() ),
- shape=dflimg.get_shape(),
- landmarks=dflimg.get_landmarks(),
- ie_polys=dflimg.get_ie_polys(),
- eyebrows_expand_mod=dflimg.get_eyebrows_expand_mod(),
- source_filename=dflimg.get_source_filename(),
- ))
- return sample_list
- """
- @staticmethod
- def upgradeToFaceTemporalSortedSamples( samples ):
- new_s = [ (s, s.source_filename) for s in samples]
- new_s = sorted(new_s, key=operator.itemgetter(1))
- return [ s[0] for s in new_s]
- class FaceSamplesLoaderSubprocessor(Subprocessor):
- #override
- def __init__(self, image_paths ):
- self.image_paths = image_paths
- self.image_paths_len = len(image_paths)
- self.idxs = [*range(self.image_paths_len)]
- self.result = [None]*self.image_paths_len
- super().__init__('FaceSamplesLoader', FaceSamplesLoaderSubprocessor.Cli, 60)
- #override
- def on_clients_initialized(self):
- io.progress_bar ("Loading samples", len (self.image_paths))
- #override
- def on_clients_finalized(self):
- io.progress_bar_close()
- #override
- def process_info_generator(self):
- for i in range(min(multiprocessing.cpu_count(), 8) ):
- yield 'CPU%d' % (i), {}, {}
- #override
- def get_data(self, host_dict):
- if len (self.idxs) > 0:
- idx = self.idxs.pop(0)
- return idx, self.image_paths[idx]
- return None
- #override
- def on_data_return (self, host_dict, data):
- self.idxs.insert(0, data[0])
- #override
- def on_result (self, host_dict, data, result):
- idx, dflimg = result
- self.result[idx] = (self.image_paths[idx], dflimg)
- io.progress_bar_inc(1)
- #override
- def get_result(self):
- return self.result
- class Cli(Subprocessor.Cli):
- #override
- def process_data(self, data):
- idx, filename = data
- dflimg = DFLIMG.load (Path(filename))
- if dflimg is None:
- self.log_err (f"FaceSamplesLoader: {filename} is not a dfl image file.")
- data = None
- else:
- data = (dflimg.get_face_type(),
- dflimg.get_shape(),
- dflimg.get_landmarks(),
- dflimg.get_ie_polys(),
- dflimg.get_seg_ie_polys(),
- dflimg.get_eyebrows_expand_mod(),
- dflimg.get_source_filename() )
- return idx, data
- #override
- def get_data_name (self, data):
- #return string identificator of your data
- return data[1]
|