-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathface_processor.py
388 lines (313 loc) · 13.4 KB
/
face_processor.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
"""
Core processing module for face reconstruction and editing.
Handles the main pipeline from input image to 3D reconstruction.
"""
import time
import torch
import numpy as np
import dlib
from PIL import Image
import torchvision.transforms as transforms
from dataclasses import dataclass
from typing import Optional, Tuple, List, Dict, Any
from pathlib import Path
import logging
# For Debugging
from utils.common import tensor2im
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@dataclass
class ProcessingOptions:
pose_range: Tuple[int, int]
center_pose: int
show_generated: bool
show_multipose: bool
show_3d: bool
class FaceProcessor:
def __init__(self, config):
"""
Initialize the face processor with configuration.
Args:
config: Configuration object containing all necessary paths and parameters
"""
self.config = config
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
logger.info(f"Using device: {self.device}")
self._initialize_models()
self._setup_transforms()
def _setup_transforms(self):
"""Setup image transformations for the model."""
self.transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
def _initialize_models(self):
"""Initialize all required models and components."""
try:
from models.stylegan3.model import GeneratorType
from FaceBoxes.FaceBoxes_ONNX import FaceBoxes_ONNX
from TDDFA.TDDFA_ONNX import TDDFA_ONNX
from editing.interfacegan.face_editor import FaceEditor
from utils.inference_utils import load_encoder, get_average_image
logger.info("Initializing face detection and alignment models...")
self.face_detector = dlib.get_frontal_face_detector()
self.shape_predictor = dlib.shape_predictor(str(self.config.shape_predictor_path))
logger.info("Initializing 3DDFA models...")
self.face_boxes = FaceBoxes_ONNX()
self.tddfa = TDDFA_ONNX(**self.config.threeddfa_config)
logger.info("Loading encoder model...")
self.encoder, self.opts = load_encoder(
checkpoint_path=str(self.config.e4e_model_path)
)
self.encoder = self.encoder.to(self.device)
self.encoder.eval()
logger.info("Initializing face editor...")
self.face_editor = FaceEditor(
stylegan_generator=self.encoder.decoder,
generator_type=GeneratorType.ALIGNED
)
self.avg_image = get_average_image(self.encoder)
except Exception as e:
raise RuntimeError(f"Failed to initialize models: {str(e)}")
def process_image(self, input_path: str, options: ProcessingOptions) -> Dict[str, Path]:
"""
Process input image through the complete pipeline.
Args:
input_path: Path to input image
options: Processing options including pose range and visualization flags
Returns:
Dictionary containing paths to generated files
"""
try:
logger.info(f"Processing image: {input_path}")
# Load and preprocess image
original_image = Image.open(input_path).convert("RGB")
aligned_image = self._align_face(original_image)
cropped_image = self._crop_face(original_image)
# Get transform matrix
transform_matrix = self._compute_transform(aligned_image, cropped_image)
# Generate inversions and edits
result_batch, result_latents = self._generate_inversions(aligned_image)
edit_images = self._generate_pose_edits(
result_latents,
transform_matrix,
options.pose_range
)
# Generate 3D reconstruction
frontal_face = edit_images[options.center_pose]
output_paths = self._generate_3d_reconstruction(frontal_face)
# Save pose images
output_paths['pose'] = self._save_pose_images(edit_images, concatenate=True)
if options.show_3d:
self._visualize_3d(output_paths['obj'])
return output_paths
except Exception as e:
raise RuntimeError(f"Failed to process image: {str(e)}")
def _align_face(self, image: Image.Image) -> Image.Image:
"""
Align face in image using facial landmarks.
Args:
image: Input PIL Image
Returns:
Aligned PIL Image
"""
from utils.alignment_utils import align_face
logger.info("Aligning face...")
try:
aligned_image = align_face(
image,
self.face_detector,
self.shape_predictor
)
return aligned_image
except Exception as e:
raise RuntimeError(f"Face alignment failed: {str(e)}")
def _crop_face(self, image: Image.Image) -> Image.Image:
"""
Crop face from image using facial landmarks.
Args:
image: Input PIL Image
Returns:
Cropped PIL Image
"""
from utils.alignment_utils import crop_face
logger.info("Cropping face...")
try:
cropped_image = crop_face(
image,
self.face_detector,
self.shape_predictor
)
return cropped_image
except Exception as e:
raise RuntimeError(f"Face cropping failed: {str(e)}")
def _compute_transform(self, aligned_image: Image.Image, cropped_image: Image.Image) -> np.ndarray:
"""
Compute transformation matrix between aligned and cropped images.
Args:
aligned_image: Aligned face image
cropped_image: Cropped face image
Returns:
Transformation matrix
"""
from utils.alignment_utils import get_stylegan_transform
logger.info("Computing transformation matrix...")
try:
res = get_stylegan_transform(
cropped_image,
aligned_image,
self.face_detector,
self.shape_predictor
)
if res is None:
raise RuntimeError("Failed to compute transformation matrix")
rotation_angle, translation, transform, inverse_transform = res
return inverse_transform
except Exception as e:
raise RuntimeError(f"Transform computation failed: {str(e)}")
def _generate_inversions(self, image: Image.Image) -> Tuple[torch.Tensor, np.ndarray]:
"""
Generate StyleGAN inversions for the input image.
Args:
image: Input PIL Image
Returns:
Tuple of (result_batch, result_latents)
"""
from utils.inference_utils import run_on_batch
logger.info("Generating inversions...")
try:
transformed_image = self.transform(image)
transformed_image = transformed_image.unsqueeze(0).to(self.device).float()
self.opts.n_iters_per_batch = 3
self.opts.resize_outputs = False
with torch.no_grad():
tic = time.time()
result_batch, result_latents = run_on_batch(
inputs=transformed_image,
net=self.encoder,
opts=self.opts,
avg_image=self.avg_image
)
toc = time.time()
logger.info(f'Inference took {toc - tic:.4f} seconds')
#Debugging
# result_tensors = result_batch[0]
# inversed_img = tensor2im(result_tensors[-1])
# inversed_img.save("wakao.png")
return result_batch, result_latents
except Exception as e:
raise RuntimeError(f"Inversion generation failed: {str(e)}")
def _generate_pose_edits(
self,
latents: np.ndarray,
transform_matrix: np.ndarray,
pose_range: Tuple[int, int]
) -> List[Image.Image]:
"""
Generate pose variations using the face editor.
Args:
latents: Input latent codes
transform_matrix: Transformation matrix
pose_range: Tuple of (min_pose, max_pose)
Returns:
List of edited images
"""
logger.info("Generating pose variations...")
try:
input_latent = torch.from_numpy(latents[0][-1]).unsqueeze(0).to(self.device)
# print(type(transform_matrix))
edit_images, edit_latents = self.face_editor.edit(
latents=input_latent,
direction='pose',
factor_range=pose_range,
user_transforms=transform_matrix,
apply_user_transformations=True
)
return [image[0] for image in edit_images]
except Exception as e:
raise RuntimeError(f"Pose editing failed: {str(e)}")
def _generate_3d_reconstruction(self, image: Image.Image) -> Dict[str, Path]:
"""
Generate 3D reconstruction using 3DDFA.
Args:
image: Input frontal face image
Returns:
Dictionary containing paths to generated files
"""
from ThreeDDFA_utils.uv import uv_tex
from ThreeDDFA_utils.serialization import ser_to_obj
logger.info("Generating 3D reconstruction...")
try:
# Convert to BGR for 3DDFA
image_np = np.array(image)
image_bgr = image_np[:, :, ::-1]
# Detect face and estimate parameters
boxes = self.face_boxes(image_bgr)
param_lst, roi_box_lst = self.tddfa(image_bgr, boxes)
ver_lst = self.tddfa.recon_vers(param_lst, roi_box_lst, dense_flag=True)
# Generate output paths
output_paths = self.config.get_output_paths()
# Generate UV texture and OBJ file
uv_tex(image_bgr, ver_lst, self.tddfa.tri, wfp=str(output_paths['uv_tex']))
ser_to_obj(
image_bgr,
ver_lst,
self.tddfa.tri,
height=1024,
wfp=str(output_paths['obj'])
)
return output_paths
except Exception as e:
raise RuntimeError(f"3D reconstruction failed: {str(e)}")
def _save_pose_images(self, images: List[Image.Image], concatenate: bool = True) -> List[Path]:
"""
Save multi-pose visualization, either as a concatenated image or individual files.
Args:
images: List of pose variations
concatenate: If True, concatenate images horizontally. If False, save individually.
Returns:
Path to saved concatenated image if concatenate=True, or list of paths to individual images if concatenate=False
"""
logger.info("Saving pose variations...")
try:
output_paths = self.config.get_output_paths()
base_path = output_paths['pose']
if concatenate:
# Create concatenated image
res = np.array(images[0].resize((512, 512)))
for image in images[1:]:
res = np.concatenate([res, image.resize((512, 512))], axis=1)
# Save concatenated result
pose_img = Image.fromarray(res).convert("RGB")
pose_img.save(str(base_path))
return base_path
else:
# Save individual images
saved_paths = []
stem = base_path.stem
suffix = base_path.suffix
for idx, image in enumerate(images):
individual_path = base_path.parent / f"{stem}_{idx}{suffix}"
resized_img = image.resize((512, 512))
resized_img.save(str(individual_path))
saved_paths.append(individual_path)
return saved_paths
except Exception as e:
raise RuntimeError(f"Failed to save pose images: {str(e)}")
def _visualize_3d(self, obj_path: Path):
"""
Visualize 3D reconstruction using Open3D.
Args:
obj_path: Path to OBJ file
"""
try:
import open3d as o3d
logger.info("Visualizing 3D reconstruction...")
mesh = o3d.io.read_triangle_mesh(str(obj_path))
o3d.visualization.draw_geometries([mesh])
except ImportError:
logger.warning("Open3D not available. Skipping 3D visualization.")
except Exception as e:
logger.error(f"3D visualization failed: {str(e)}")