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
| import numpy as np
| import torch
| from torch.utils.serialization import load_lua
| import os
| import scipy.io as sio
| import cv2
| import math
| from math import cos, sin
|
| def plot_pose_cube(img, pose_params, size=150.):
| # pose_params: (pitch, yaw, roll, tdx, tdy)
| # Where (tdx, tdy) is the translation of the face.
| # For pose we have [pitch yaw roll tdx tdy tdz scale_factor]
|
| pitch, yaw, roll, tdx, tdy = pose_params
|
| p = pitch
| y = -yaw
| r = roll
|
| # TODO: probably useless
| # Translation of box depends on head pointing to right or left.
| if y < 0:
| face_x = tdx - 0.50 * size
| face_y = tdy - 0.50 * size
| else:
| face_x = tdx - 0.50 * size
| face_y = tdy - 0.50 * size
|
| x1 = size * (cos(y) * cos(r)) + face_x
| y1 = size * (cos(p) * sin(r) + cos(r) * sin(p) * sin(y)) + face_y
| x2 = size * (-cos(y) * sin(r)) + face_x
| y2 = size * (cos(p) * cos(r) - sin(p) * sin(y) * sin(r)) + face_y
| x3 = size * (sin(y)) + face_x
| y3 = size * (-cos(y) * sin(p)) + face_y
|
| # Draw base in red
| cv2.line(img, (int(face_x), int(face_y)), (int(x1),int(y1)),(255,0,0),3)
| cv2.line(img, (int(face_x), int(face_y)), (int(x2),int(y2)),(255,0,0),3)
| cv2.line(img, (int(x2), int(y2)), (int(x2+x1-face_x),int(y2+y1-face_y)),(255,0,0),3)
| cv2.line(img, (int(x1), int(y1)), (int(x1+x2-face_x),int(y1+y2-face_y)),(255,0,0),3)
| # Draw pillars in blue
| cv2.line(img, (int(face_x), int(face_y)), (int(x3),int(y3)),(0,0,255),2)
| cv2.line(img, (int(x1), int(y1)), (int(x1+x3-face_x),int(y1+y3-face_y)),(0,0,255),2)
| cv2.line(img, (int(x2), int(y2)), (int(x2+x3-face_x),int(y2+y3-face_y)),(0,0,255),2)
| cv2.line(img, (int(x2+x1-face_x),int(y2+y1-face_y)), (int(x3+x1+x2-2*face_x),int(y3+y2+y1-2*face_y)),(0,0,255),2)
| # Draw top in green
| cv2.line(img, (int(x3+x1-face_x),int(y3+y1-face_y)), (int(x3+x1+x2-2*face_x),int(y3+y2+y1-2*face_y)),(0,255,0),2)
| cv2.line(img, (int(x2+x3-face_x),int(y2+y3-face_y)), (int(x3+x1+x2-2*face_x),int(y3+y2+y1-2*face_y)),(0,255,0),2)
| cv2.line(img, (int(x3), int(y3)), (int(x3+x1-face_x),int(y3+y1-face_y)),(0,255,0),2)
| cv2.line(img, (int(x3), int(y3)), (int(x3+x2-face_x),int(y3+y2-face_y)),(0,255,0),2)
|
| return img
|
| def get_pose_params_from_mat(mat_path):
| # This functions gets the pose parameters from the .mat
| # Annotations that come with the 300W_LP dataset.
| mat = sio.loadmat(mat_path)
| # [pitch yaw roll tdx tdy tdz scale_factor]
| pre_pose_params = mat['Pose_Para'][0]
| # Get [pitch, yaw, roll, tdx, tdy]
| pose_params = pre_pose_params[:5]
| return pose_params
|
| def get_ypr_from_mat(mat_path):
| # Get yaw, pitch, roll from .mat annotation.
| # They are in radians
| mat = sio.loadmat(mat_path)
| # [pitch yaw roll tdx tdy tdz scale_factor]
| pre_pose_params = mat['Pose_Para'][0]
| # Get [pitch, yaw, roll]
| pose_params = pre_pose_params[:3]
| return pose_params
|
| def mse_loss(input, target):
| return torch.sum(torch.abs(input.data - target.data) ** 2)
|
|