-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
380 lines (310 loc) · 14.6 KB
/
main.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
import cv2
import mediapipe as mp
import numpy as np
import os
from custom.objloader_simple import *
from custom.utils import *
from custom.videosource import WebcamSource
from custom.face_geometry import (
PCF,
get_metric_landmarks,
procrustes_landmark_basis,
)
# Create mediapipe variables
mp_drawing = mp.solutions.drawing_utils
mp_face_mesh = mp.solutions.face_mesh
mp_face_mesh_connections = mp.solutions.face_mesh_connections
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=3)
points_idx = [33, 263, 61, 291, 199]
points_idx = points_idx + [key for (key, val) in procrustes_landmark_basis]
points_idx = list(set(points_idx))
points_idx.sort()
frame_height, frame_width, channels = (360, 720, 3)
# Pseudo camera internals
focal_length = frame_width
center = (frame_width / 2, frame_height / 2)
camera_matrix = np.array(
[[focal_length, 0, center[0]], [0, focal_length, center[1]], [0, 0, 1]],
dtype="double",
)
# Distortion coefficients
dist_coeff = np.zeros((4, 1))
def render_lower(img, faces, vertices, camera_parameters, texture, base_point, head_up, pass_factor, left_oriented):
"""Render the loaded obj model into the current video frame for lower parts
Args:
img (np.array): Input image
faces (list): Faces of the object
vertices (list): Vertices of the object
camera_parameters (dict): Camera parameters
texture (np.array): Texture image
base_point (tuple): Reference point for render
head_up (bool): Boolean for head position
pass_factor (int): Constant for render
left_oriented (bool): Boolean for head left position
Returns:
np.array: Output image
"""
texture_height = texture.shape[0] - 6
texture_width = texture.shape[1] - 1
src_points = np.array([[0, 0], [0, texture_height], [texture_width, texture_height], [texture_width, 0]], dtype=np.float32)
src_points_triangle = np.array([[0, 0], [texture_height, 0], [0, texture_width]], dtype=np.float32)
# Using scale matrix to upscale the model
scale_matrix = np.eye(3) * 1
# Downscale the image
down_width = 320 #1280
down_height = 180 #720
down_points = (down_width, down_height)
img = cv2.resize(img, down_points, interpolation= cv2.INTER_LINEAR)
for idx, face in enumerate(faces):
face_vertices = face[0]
points = np.array([vertices[vertex - 1] for vertex in face_vertices])
points = np.dot(points, scale_matrix)
# Render model in the landmarks. To do so, model points must be displaced
points = np.array([[p[0], p[1], p[2] - 6] for p in points])
# Pass rendering if the point is behind the reference point
pass_render = False
for point in points:
if point[2] < base_point[2] - pass_factor:
pass_render = True
if pass_render:
if head_up:
continue
# Get projected points mp_rotation_vector, mp_translation_vector, camera_matrix, dist_coeff
(pointer2D, jacobian) = cv2.projectPoints(
np.array([points]),
camera_parameters['mp_rotation_vector'],
camera_parameters['mp_translation_vector'],
camera_parameters['camera_matrix'],
camera_parameters['dist_coeff'],
)
point_2d = pointer2D.squeeze().astype(np.float32)
if head_up:
if left_oriented:
point_2d[:,0] += 5
else:
point_2d[:,0] -= 5
point_2d = point_2d / 2
cv2.fillConvexPoly(img, point_2d.astype(int), (1.0, 1.0, 1.0), 16, 0)
# Add texture with perspective transform and warp
if len(point_2d) > 3:
pers = cv2.getPerspectiveTransform(src_points.astype(np.float32), point_2d.astype(np.float32))
im_temp = cv2.warpPerspective(texture, pers, (img.shape[1], img.shape[0]))
img = img + im_temp
elif len(point_2d) == 3:
# Add texture with affine transform and warp
affine = cv2.getAffineTransform(src_points_triangle.astype(np.float32), point_2d.astype(np.float32))
im_temp = cv2.warpAffine(texture, affine, (img.shape[1], img.shape[0]))
img = img + im_temp
# Upscale the image using new width and height
up_width = 640
up_height = 360
up_points = (up_width, up_height)
img = cv2.resize(img, up_points, interpolation= cv2.INTER_LINEAR)
return img
def render_upper(img, faces, vertices, camera_parameters, texture, base_point, head_up, pass_factor):
"""Render the loaded obj model into the current video frame for upper parts
Args:
img (np.array): Input image
faces (list): Faces of the object
vertices (list): Vertices of the object
camera_parameters (dict): Camera parameters
texture (np.array): Texture image
base_point (tuple): Reference point for render
head_up (bool): Boolean for head position
pass_factor (int): Constant for render
left_oriented (bool): Boolean for head left position
Returns:
np.array: Output image
"""
texture_height = texture.shape[0] - 6
texture_width = texture.shape[1] - 1
src_points = np.array([[0, 0], [0, texture_height], [texture_width, texture_height], [texture_width, 0]], dtype=np.float32)
src_points_triangle = np.array([[0, 0], [texture_height, 0], [0, texture_width]], dtype=np.float32)
# Using scale matrix to upscale the model
scale_matrix = np.eye(3) * 1
# Downscale the image
down_width = 320 #1280
down_height = 180 #720
down_points = (down_width, down_height)
img = cv2.resize(img, down_points, interpolation= cv2.INTER_LINEAR)
for idx, face in enumerate(faces):
face_vertices = face[0]
points = np.array([vertices[vertex - 1] for vertex in face_vertices])
points = np.dot(points, scale_matrix)
# Render model in the landmarks. To do so, model points must be displaced
points = np.array([[p[0], p[1], p[2] - 6] for p in points])
# Pass rendering if the point is behind the reference point
pass_render = False
for point in points:
if point[2] < base_point[2] - pass_factor:
pass_render = True
if pass_render:
continue
# Get projected points
(pointer2D, jacobian) = cv2.projectPoints(
np.array([points]),
camera_parameters['mp_rotation_vector'],
camera_parameters['mp_translation_vector'],
camera_parameters['camera_matrix'],
camera_parameters['dist_coeff'],
)
point_2d = pointer2D.squeeze().astype(np.float32)
point_2d = point_2d / 2
cv2.fillConvexPoly(img, point_2d.astype(int), (1.0, 1.0, 1.0), 16, 0)
# Add texture with perspective transform and warp
if len(point_2d) > 3:
pers = cv2.getPerspectiveTransform(src_points.astype(np.float32), point_2d.astype(np.float32))
im_temp = cv2.warpPerspective(texture, pers, (img.shape[1], img.shape[0]))
img = img + im_temp
elif len(point_2d) == 3:
# Add texture with affine transform and warp
affine = cv2.getAffineTransform(src_points_triangle.astype(np.float32), point_2d.astype(np.float32))
im_temp = cv2.warpAffine(texture, affine, (img.shape[1], img.shape[0]))
img = img + im_temp
# Upscale the image using new width and height
up_width = 640
up_height = 360
up_points = (up_width, up_height)
img = cv2.resize(img, up_points, interpolation= cv2.INTER_LINEAR)
return img
def render(img, faces, vertices, camera_parameters, texture, base_point, head_up):
"""Render the loaded obj model into the current video frame
Args:
img (np.array): Input image
faces (list): Faces of the object
vertices (list): Vertices of the object
camera_parameters (dict): Camera parameters
texture (np.array): Texture image
base_point (tuple): Reference point for render
head_up (bool): Boolean for head position
Returns:
np.array: Output image
"""
texture_height = texture.shape[0] - 6
texture_width = texture.shape[1] - 1
src_points = np.array([[0, 0], [0, texture_height], [texture_width, texture_height], [texture_width, 0]], dtype=np.float32)
src_points_triangle = np.array([[0, 0], [texture_height, 0], [0, texture_width]], dtype=np.float32)
# Using scale matrix to upscale the model
scale_matrix = np.eye(3) * 1
# Downscale the image
down_width = 320 #1280
down_height = 180 #720
down_points = (down_width, down_height)
img = cv2.resize(img, down_points, interpolation= cv2.INTER_LINEAR)
for idx, face in enumerate(faces):
face_vertices = face[0]
points = np.array([vertices[vertex - 1] for vertex in face_vertices])
points = np.dot(points, scale_matrix)
# Render model in the landmarks. To do so, model points must be displaced
points = np.array([[p[0], p[1], p[2] - 6] for p in points])
# Pass rendering if the point is behind the reference point
pass_render = False
for point in points:
if point[2] < base_point[2] - 2:
pass_render = True
if pass_render:
if not (0 < idx < 64):
continue
else:
if head_up:
continue
# Get projected points
(pointer2D, jacobian) = cv2.projectPoints(
np.array([points]),
camera_parameters['mp_rotation_vector'],
camera_parameters['mp_translation_vector'],
camera_parameters['camera_matrix'],
camera_parameters['dist_coeff'],
)
point_2d = pointer2D.squeeze().astype(np.float32)
point_2d = point_2d / 2
cv2.fillConvexPoly(img, point_2d.astype(int), (1.0, 1.0, 1.0), 16, 0)
# Add texture with perspective transform and warp
if len(point_2d) > 3:
pers = cv2.getPerspectiveTransform(src_points.astype(np.float32), point_2d.astype(np.float32))
im_temp = cv2.warpPerspective(texture, pers, (img.shape[1], img.shape[0]))
img = img + im_temp
elif len(point_2d) == 3:
# Add texture with affine transform and warp
affine = cv2.getAffineTransform(src_points_triangle.astype(np.float32), point_2d.astype(np.float32))
im_temp = cv2.warpAffine(texture, affine, (img.shape[1], img.shape[0]))
img = img + im_temp
# Upscale the image using new width and height
up_width = 640
up_height = 360
up_points = (up_width, up_height)
img = cv2.resize(img, up_points, interpolation= cv2.INTER_LINEAR)
return img
def main():
source = WebcamSource(flip=True)
refine_landmarks = True
dir_name = os.getcwd()
# Load 3D model from OBJ file
obj = OBJ(os.path.join(dir_name, 'models/anime_hat.obj'), swapyz=False)
# Load texture image
texture = cv2.imread(os.path.join(dir_name, 'models/texture_small.jpg'), cv2.IMREAD_UNCHANGED)
# Reversing the faces because we need to change the render order
faces = obj.faces
faces.reverse()
# Divide faces for different rendering scenarios
hat_lower = obj.faces[:64]
hat_upper = obj.faces[65:]
vertices = obj.vertices
# These are taken from mediapipe (PCF)
pcf = PCF(
near=1,
far=10000,
frame_height=frame_height,
frame_width=frame_width,
fy=camera_matrix[1, 1],
)
with mp_face_mesh.FaceMesh(
static_image_mode=False,
refine_landmarks=refine_landmarks,
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
) as face_mesh:
for idx, (frame, frame_rgb) in enumerate(source):
results = face_mesh.process(frame_rgb)
multi_face_landmarks = results.multi_face_landmarks
if multi_face_landmarks:
face_landmarks = multi_face_landmarks[0]
landmarks = np.array(
[(lm.x, lm.y, lm.z) for lm in face_landmarks.landmark]
)
chin = landmarks[152]
forehead = landmarks[10]
left_ear = landmarks[234]
right_ear = landmarks[454]
landmarks = landmarks.T
if refine_landmarks:
landmarks = landmarks[:, :468]
_, pose_transform_mat = get_metric_landmarks(
landmarks.copy(), pcf
)
# Calculate extrinsic values
pose_transform_mat[1:3, :] = -pose_transform_mat[1:3, :]
mp_rotation_vector, _ = cv2.Rodrigues(pose_transform_mat[:3, :3])
mp_translation_vector = pose_transform_mat[:3, 3, None]
camera_parameters = {
'mp_rotation_vector': mp_rotation_vector,
'mp_translation_vector': mp_translation_vector,
'camera_matrix': camera_matrix,
'dist_coeff': dist_coeff
}
# To render only required part, using this point as reference
base_point = (forehead[0], forehead[1], forehead[2] - 8)
# This condition checks the head position whether it is up or down
if chin[2] < forehead[2]:
# This condition checks the head whether it is turned left o right
if left_ear[2] < right_ear[2]:
frame = render_upper(frame, hat_upper, vertices, camera_parameters, texture=texture, base_point=base_point, head_up=True, pass_factor=-2)
frame = render_lower(frame, hat_lower, vertices, camera_parameters, texture=texture, base_point=base_point, head_up=True, pass_factor=2, left_oriented=True)
else:
frame = render_upper(frame, hat_upper, vertices, camera_parameters, texture=texture, base_point=base_point, head_up=True, pass_factor=-2)
frame = render_lower(frame, hat_lower, vertices, camera_parameters, texture=texture, base_point=base_point, head_up=True, pass_factor=2, left_oriented=False)
else:
frame = render(frame, faces, vertices, camera_parameters, texture=texture, base_point=base_point, head_up=False)
source.show(frame)
if __name__ == "__main__":
main()