Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

SampleLoader.py 6.9 KB

You have to be logged in to leave a comment. Sign In
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
  1. import multiprocessing
  2. import operator
  3. import pickle
  4. import traceback
  5. from pathlib import Path
  6. import samplelib.PackedFaceset
  7. from core import pathex
  8. from core.interact import interact as io
  9. from core.joblib import Subprocessor
  10. from DFLIMG import *
  11. from facelib import FaceType, LandmarksProcessor
  12. from .Sample import Sample, SampleType
  13. class SampleLoader:
  14. samples_cache = dict()
  15. @staticmethod
  16. def get_person_id_max_count(samples_path):
  17. samples = None
  18. try:
  19. samples = samplelib.PackedFaceset.load(samples_path)
  20. except:
  21. io.log_err(f"Error occured while loading samplelib.PackedFaceset.load {str(samples_dat_path)}, {traceback.format_exc()}")
  22. if samples is None:
  23. raise ValueError("packed faceset not found.")
  24. persons_name_idxs = {}
  25. for sample in samples:
  26. persons_name_idxs[sample.person_name] = 0
  27. return len(list(persons_name_idxs.keys()))
  28. @staticmethod
  29. def load(sample_type, samples_path, subdirs=False):
  30. samples_cache = SampleLoader.samples_cache
  31. if str(samples_path) not in samples_cache.keys():
  32. samples_cache[str(samples_path)] = [None]*SampleType.QTY
  33. samples = samples_cache[str(samples_path)]
  34. if sample_type == SampleType.IMAGE:
  35. if samples[sample_type] is None:
  36. samples[sample_type] = [ Sample(filename=filename) for filename in io.progress_bar_generator( pathex.get_image_paths(samples_path, subdirs=subdirs), "Loading") ]
  37. elif sample_type == SampleType.FACE:
  38. if samples[sample_type] is None:
  39. try:
  40. result = samplelib.PackedFaceset.load(samples_path)
  41. except:
  42. io.log_err(f"Error occured while loading samplelib.PackedFaceset.load {str(samples_dat_path)}, {traceback.format_exc()}")
  43. if result is not None:
  44. io.log_info (f"Loaded {len(result)} packed faces from {samples_path}")
  45. if result is None:
  46. result = SampleLoader.load_face_samples( pathex.get_image_paths(samples_path, subdirs=subdirs) )
  47. samples[sample_type] = result
  48. elif sample_type == SampleType.FACE_TEMPORAL_SORTED:
  49. result = SampleLoader.load (SampleType.FACE, samples_path)
  50. result = SampleLoader.upgradeToFaceTemporalSortedSamples(result)
  51. samples[sample_type] = result
  52. return samples[sample_type]
  53. @staticmethod
  54. def load_face_samples ( image_paths):
  55. result = FaceSamplesLoaderSubprocessor(image_paths).run()
  56. sample_list = []
  57. for filename, \
  58. ( face_type,
  59. shape,
  60. landmarks,
  61. ie_polys,
  62. seg_ie_polys,
  63. eyebrows_expand_mod,
  64. source_filename,
  65. ) in result:
  66. sample_list.append( Sample(filename=filename,
  67. sample_type=SampleType.FACE,
  68. face_type=FaceType.fromString (face_type),
  69. shape=shape,
  70. landmarks=landmarks,
  71. ie_polys=ie_polys,
  72. seg_ie_polys=seg_ie_polys,
  73. eyebrows_expand_mod=eyebrows_expand_mod,
  74. source_filename=source_filename,
  75. ))
  76. return sample_list
  77. """
  78. @staticmethod
  79. def load_face_samples ( image_paths):
  80. sample_list = []
  81. for filename in io.progress_bar_generator (image_paths, desc="Loading"):
  82. dflimg = DFLIMG.load (Path(filename))
  83. if dflimg is None:
  84. io.log_err (f"{filename} is not a dfl image file.")
  85. else:
  86. sample_list.append( Sample(filename=filename,
  87. sample_type=SampleType.FACE,
  88. face_type=FaceType.fromString ( dflimg.get_face_type() ),
  89. shape=dflimg.get_shape(),
  90. landmarks=dflimg.get_landmarks(),
  91. ie_polys=dflimg.get_ie_polys(),
  92. eyebrows_expand_mod=dflimg.get_eyebrows_expand_mod(),
  93. source_filename=dflimg.get_source_filename(),
  94. ))
  95. return sample_list
  96. """
  97. @staticmethod
  98. def upgradeToFaceTemporalSortedSamples( samples ):
  99. new_s = [ (s, s.source_filename) for s in samples]
  100. new_s = sorted(new_s, key=operator.itemgetter(1))
  101. return [ s[0] for s in new_s]
  102. class FaceSamplesLoaderSubprocessor(Subprocessor):
  103. #override
  104. def __init__(self, image_paths ):
  105. self.image_paths = image_paths
  106. self.image_paths_len = len(image_paths)
  107. self.idxs = [*range(self.image_paths_len)]
  108. self.result = [None]*self.image_paths_len
  109. super().__init__('FaceSamplesLoader', FaceSamplesLoaderSubprocessor.Cli, 60)
  110. #override
  111. def on_clients_initialized(self):
  112. io.progress_bar ("Loading samples", len (self.image_paths))
  113. #override
  114. def on_clients_finalized(self):
  115. io.progress_bar_close()
  116. #override
  117. def process_info_generator(self):
  118. for i in range(min(multiprocessing.cpu_count(), 8) ):
  119. yield 'CPU%d' % (i), {}, {}
  120. #override
  121. def get_data(self, host_dict):
  122. if len (self.idxs) > 0:
  123. idx = self.idxs.pop(0)
  124. return idx, self.image_paths[idx]
  125. return None
  126. #override
  127. def on_data_return (self, host_dict, data):
  128. self.idxs.insert(0, data[0])
  129. #override
  130. def on_result (self, host_dict, data, result):
  131. idx, dflimg = result
  132. self.result[idx] = (self.image_paths[idx], dflimg)
  133. io.progress_bar_inc(1)
  134. #override
  135. def get_result(self):
  136. return self.result
  137. class Cli(Subprocessor.Cli):
  138. #override
  139. def process_data(self, data):
  140. idx, filename = data
  141. dflimg = DFLIMG.load (Path(filename))
  142. if dflimg is None:
  143. self.log_err (f"FaceSamplesLoader: {filename} is not a dfl image file.")
  144. data = None
  145. else:
  146. data = (dflimg.get_face_type(),
  147. dflimg.get_shape(),
  148. dflimg.get_landmarks(),
  149. dflimg.get_ie_polys(),
  150. dflimg.get_seg_ie_polys(),
  151. dflimg.get_eyebrows_expand_mod(),
  152. dflimg.get_source_filename() )
  153. return idx, data
  154. #override
  155. def get_data_name (self, data):
  156. #return string identificator of your data
  157. return data[1]
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...