Skeletal Animation Multithread Face
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Face Class Reference

This class runs the client that handles the vertices coming from the python server face_mesh_server.py to render the face landmarking. More...

#include <face.h>

Public Member Functions

 Face ()
 Constructor. More...
 
void update (void)
 
Eigen::MatrixXd get_vertices (void)
 Get face vertices. More...
 
Eigen::MatrixXi get_surface_indices (void)
 Get the surface indices of the face mesh. More...
 
Eigen::MatrixXd translation_matrix (const Eigen::Vector3d &offset, size_t vert_num)
 This function offset a set of vertices by a given offset. More...
 

Private Member Functions

void setup_tcp_communication (void)
 Setup communication with python server. More...
 
Eigen::MatrixXd incoming_data_callback (void)
 Read incoming data. More...
 

Private Attributes

std::filesystem::path m_vertices_connections_abs_path
 Absolute name of vertices connection files. More...
 
std::string m_vertices_connections_rel_filename
 Relative name of vertices connection files. More...
 
Eigen::MatrixXi m_faces
 Face landmarking indices matrix. More...
 
std::string m_local_ip_address = "127.0.1.1"
 Start tcp client at this ip adress (localhost). More...
 
const int m_tcp_port = 5052
 
int m_socket = 0
 Socket handle. More...
 
const int m_buffer_size = 40000
 
const int m_vertices_num = 468
 Number of vertices provided by the mediapipe. This is constant. More...
 
Eigen::MatrixXi m_surf_indices
 The surface element indices of the incoming mesh. More...
 
std::future< Eigen::MatrixXd > m_future_fun
 Future function handle. More...
 
bool m_return_value = 0
 Termination flag for callback function. More...
 
Eigen::MatrixXd m_vol_mesh_vertices
 Volume mesh data. More...
 
Eigen::Matrix3d m_rot_mat = Eigen::Matrix3d::Identity()
 Face rotation matrix. More...
 
double m_face_mesh_scale = 6.0
 Face scale matrix. More...
 
Eigen::Vector3d m_face_offset = Eigen::Vector3d({0.0, -2.0, 0.0})
 
Eigen::Matrix3d m_scale_mat
 

Detailed Description

This class runs the client that handles the vertices coming from the python server face_mesh_server.py to render the face landmarking.

Definition at line 25 of file face.h.

Constructor & Destructor Documentation

◆ Face()

Face::Face ( )

Constructor.

Construct a new Face:: Face object It initializes the tcp communcations and the asychronous functionalities. The indices of the face mesh are stored in the "vertices_connections.csv" and they are converted to an Eigen::MatrixXi.

Definition at line 9 of file face.cpp.

10{
11 // Get absolute file path
12 std::filesystem::path absolute_path = std::filesystem::current_path();
13
14 // Define absolute path of hand configuration file
17
18 // Read indices matrix
19 Eigen::MatrixXd tmp_faces =
21 m_surf_indices = tmp_faces.cast<int>();
22
23 // Setup communication with python server
25
26 // Initialize asychronous function
27 m_future_fun = std::async(&Face::incoming_data_callback, this);
28
29 // Initialize volume vertices
30 m_vol_mesh_vertices = Eigen::MatrixXd::Zero(m_vertices_num, 3);
31
32 // Set rotation matrix
33 m_rot_mat = EulerRotations::rotation(0, M_PI, M_PI);
34
35}
static Eigen::Matrix3d rotation(double phi, double theta, double psi)
Euler rotation matrix z-y'-x''.
Eigen::MatrixXd incoming_data_callback(void)
Read incoming data.
Definition: face.cpp:63
const int m_vertices_num
Number of vertices provided by the mediapipe. This is constant.
Definition: face.h:82
Eigen::MatrixXi m_surf_indices
The surface element indices of the incoming mesh.
Definition: face.h:85
Eigen::Matrix3d m_rot_mat
Face rotation matrix.
Definition: face.h:101
std::string m_vertices_connections_rel_filename
Relative name of vertices connection files.
Definition: face.h:55
Eigen::MatrixXd m_vol_mesh_vertices
Volume mesh data.
Definition: face.h:98
void setup_tcp_communication(void)
Setup communication with python server.
Definition: face.cpp:143
std::future< Eigen::MatrixXd > m_future_fun
Future function handle.
Definition: face.h:89
std::filesystem::path m_vertices_connections_abs_path
Absolute name of vertices connection files.
Definition: face.h:52
static Eigen::MatrixXd load_matrix_data(std::string filename)
Load Eigen double matrix from csv file.
Definition: utils.cpp:59
Here is the call graph for this function:

Member Function Documentation

◆ get_surface_indices()

Eigen::MatrixXi Face::get_surface_indices ( void  )
inline

Get the surface indices of the face mesh.

Returns
Eigen::MatrixXi The surface indices matrix.

Definition at line 43 of file face.h.

43{ return m_surf_indices; }

◆ get_vertices()

Eigen::MatrixXd Face::get_vertices ( void  )

Get face vertices.

It is the point of entry that feeds the animation loop with the face vertices data. It also manages the asynchronous threading.

Returns
Eigen::MatrixXd The face vertices.

Definition at line 41 of file face.cpp.

42{
43 // Set termination flag for asychronous function
45
46 // Get vertices
47 Eigen::MatrixXd vertices = m_future_fun.get();
48
49 // Reinitialize async function
51 m_future_fun = std::async(&Face::incoming_data_callback, this);
52
53 return vertices;
54}
bool m_return_value
Termination flag for callback function.
Definition: face.h:92
Here is the call graph for this function:

◆ incoming_data_callback()

Eigen::MatrixXd Face::incoming_data_callback ( void  )
private

Read incoming data.

This is the callback function for reading asynchronously the incoming data (vertices) from the server. The data is sent as a json file, so a parser is also used for reading the x, y and z coordinates of each vertex of the face mesh.

Returns
Eigen::MatrixXd The vertex pointcloud

Definition at line 63 of file face.cpp.

64{
65 while(!m_return_value)
66 {
67
68 // Initialize buffer
69 char buffer[m_buffer_size] = {0};
70
71 // Read value
72 int val_read = read(m_socket, buffer, m_buffer_size);
73
74 // Correct parsing flag
75 bool correct_parsing;
76
77 // Initialzie volume mesh
78 Eigen::MatrixXd vol_mesh_vertices;
79
80 // Initialize point cloud
81 nlohmann::basic_json<>::value_type x_pc, y_pc, z_pc;
82
83 try
84 {
85 // Parse json file
86 nlohmann::json incoming_data = nlohmann::json::parse(buffer);
87
88 // Pointcloud x
89 x_pc = incoming_data["x"];
90 y_pc = incoming_data["y"];
91 z_pc = incoming_data["z"];
92
93 vol_mesh_vertices.resize(x_pc.size(), 3);
94
95 // Check if the parsing is correct
96 correct_parsing = (x_pc.size() == m_vertices_num);
97 }
98 catch(nlohmann::json::parse_error& e)
99 {
100 // Parsing incorrect
101 correct_parsing = 0;
102 }
103
104 if (correct_parsing)
105 {
106 for (size_t i = 0; i < x_pc.size(); i++)
107 {
108 vol_mesh_vertices(i, 0) = x_pc[i];
109 vol_mesh_vertices(i, 1) = y_pc[i];
110 vol_mesh_vertices(i, 2) = z_pc[i];
111 }
112
113 // Rotate and scale vertices
115 vol_mesh_vertices.transpose()).transpose();
116
117 // Find centre of geometry
118 double x_cg = m_vol_mesh_vertices.col(0).sum() / m_vertices_num;
119 double y_cg = m_vol_mesh_vertices.col(1).sum() / m_vertices_num;
120 double z_cg = m_vol_mesh_vertices.col(2).sum() / m_vertices_num;
121
122 // Reset face offset
123 Eigen::Vector3d face_offset = m_face_offset +
124 Eigen::Vector3d({x_cg, y_cg, z_cg});
125
128 }
129 }
130
131 return m_vol_mesh_vertices;
132}
const int m_buffer_size
Definition: face.h:79
int m_socket
Socket handle.
Definition: face.h:73
Eigen::Matrix3d m_scale_mat
Definition: face.h:106
Eigen::Vector3d m_face_offset
Definition: face.h:105
Eigen::MatrixXd translation_matrix(const Eigen::Vector3d &offset, size_t vert_num)
This function offset a set of vertices by a given offset.
Definition: face.cpp:176
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setup_tcp_communication()

void Face::setup_tcp_communication ( void  )
private

Setup communication with python server.

Since the libraries for face landmarking are all written in Python, we establish an Inter-process communication (IPC), to get all the data in our C++ program. In this case the IPC is implemented with the help of sockets. The python side sets up a server and it transmits all the data to our C++ client. This method sets the tcp communication and initializes the socket and the client that listens to incoming data.

Definition at line 143 of file face.cpp.

144{
145 // Sock addr struct
146 struct sockaddr_in serv_addr;
147
148 if ((m_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
149 {
150 printf("\n Socket creation error \n");
151 }
152
153 serv_addr.sin_family = AF_INET;
154 serv_addr.sin_port = htons(m_tcp_port);
155
156 // Convert IPv4 and IPv6 addresses from text to binary form
157 if(inet_pton(AF_INET, m_local_ip_address.c_str(), &serv_addr.sin_addr)<=0)
158 {
159 printf("\nInvalid address/ Address not supported \n");
160 }
161
162 if (connect(m_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
163 {
164 printf("\nConnection Failed \n");
165 }
166}
std::string m_local_ip_address
Start tcp client at this ip adress (localhost).
Definition: face.h:66
const int m_tcp_port
Definition: face.h:70
Here is the caller graph for this function:

◆ translation_matrix()

Eigen::MatrixXd Face::translation_matrix ( const Eigen::Vector3d &  offset,
size_t  vert_num 
)

This function offset a set of vertices by a given offset.

This is not a homogenous transformation matrix. Instead it is used for shifting a matrix of vertices (as defined by libigl) by a given offset.

Parameters
offsetThe offset to shift the matrix of vertices.
vert_numThe number of matrices.
Returns
Eigen::MatrixXd The output translation matrix.

Definition at line 176 of file face.cpp.

178{
179 // Initialize matrix
180 Eigen::MatrixXd t_mat = Eigen::MatrixXd(vert_num, offset.rows());
181
182 // Ones vector
183 Eigen::VectorXd ones_vec = Eigen::VectorXd::Ones(vert_num);
184
185 for(size_t i = 0; i < offset.rows(); i++)
186 {
187 t_mat.col(i) = offset(i) * ones_vec;
188 }
189
190 return t_mat;
191}
Here is the caller graph for this function:

◆ update()

void Face::update ( void  )

Member Data Documentation

◆ m_buffer_size

const int Face::m_buffer_size = 40000
private

Buffer size for reading the vertices message. The number of vertices is constant and this buffer size is enough.

Definition at line 79 of file face.h.

◆ m_face_mesh_scale

double Face::m_face_mesh_scale = 6.0
private

Face scale matrix.

Definition at line 104 of file face.h.

◆ m_face_offset

Eigen::Vector3d Face::m_face_offset = Eigen::Vector3d({0.0, -2.0, 0.0})
private

Definition at line 105 of file face.h.

◆ m_faces

Eigen::MatrixXi Face::m_faces
private

Face landmarking indices matrix.

Definition at line 59 of file face.h.

◆ m_future_fun

std::future<Eigen::MatrixXd> Face::m_future_fun
private

Future function handle.

Definition at line 89 of file face.h.

◆ m_local_ip_address

std::string Face::m_local_ip_address = "127.0.1.1"
private

Start tcp client at this ip adress (localhost).

Definition at line 66 of file face.h.

◆ m_return_value

bool Face::m_return_value = 0
private

Termination flag for callback function.

Definition at line 92 of file face.h.

◆ m_rot_mat

Eigen::Matrix3d Face::m_rot_mat = Eigen::Matrix3d::Identity()
private

Face rotation matrix.

Definition at line 101 of file face.h.

◆ m_scale_mat

Eigen::Matrix3d Face::m_scale_mat
private
Initial value:
=
m_face_mesh_scale * Eigen::Matrix3d::Identity()
double m_face_mesh_scale
Face scale matrix.
Definition: face.h:104

Definition at line 106 of file face.h.

◆ m_socket

int Face::m_socket = 0
private

Socket handle.

Definition at line 73 of file face.h.

◆ m_surf_indices

Eigen::MatrixXi Face::m_surf_indices
private

The surface element indices of the incoming mesh.

Definition at line 85 of file face.h.

◆ m_tcp_port

const int Face::m_tcp_port = 5052
private

Tcp port for socket communication (this might need to be changed if it already occupied.)

Definition at line 70 of file face.h.

◆ m_vertices_connections_abs_path

std::filesystem::path Face::m_vertices_connections_abs_path
private

Absolute name of vertices connection files.

Definition at line 52 of file face.h.

◆ m_vertices_connections_rel_filename

std::string Face::m_vertices_connections_rel_filename
private
Initial value:
=
"share/vertices_connections.csv"

Relative name of vertices connection files.

Definition at line 55 of file face.h.

◆ m_vertices_num

const int Face::m_vertices_num = 468
private

Number of vertices provided by the mediapipe. This is constant.

Definition at line 82 of file face.h.

◆ m_vol_mesh_vertices

Eigen::MatrixXd Face::m_vol_mesh_vertices
private

Volume mesh data.

Definition at line 98 of file face.h.


The documentation for this class was generated from the following files: