<filename>python/test.py
import eos
import numpy as np
import os
import socket
import json
import ast
import time


dic = {"1":"356","2":"323","3":"288","4":"397","5":"365","6":"379","7":"378","8":"400","9":"152","10":"176",
       "11":"149","12":"150","13":"136","14":"172","15":"58","16":"93","17":"127","18":"260","19":"259","20":"257",
       "21":"258","22":"286","23":"56","24":"28","25":"27","26":"29","27":"30","28":"6","29":"197","30":"195",
       "31":"4","32":"305","33":"326","34":"2","35":"97","36":"75","37":"263","38":"387","39":"385","40":"398",
       "41":"380","42":"373","43":"173","44":"158","45":"160","46":"246","47":"144","48":"153","49":"375","50":"270",
       "51":"267","52":"0","53":"37","54":"40","55":"185","56":"91","57":"84","58":"17","59":"314","60":"321",
       "61":"415","62":"311","63":"13","64":"81","65":"191","66":"178","67":"14","68":"402"}


def main():
    """Demo for running the eos fitting from Python."""
    image_width = 720 # Make sure to adjust these when using your own images!
    image_height = 1280

    model = eos.morphablemodel.load_model("D:\\eos\\share\\sfm_shape_3448.bin")
    blendshapes = eos.morphablemodel.load_blendshapes("D:\\eos\\share\\expression_blendshapes_3448.bin")
    # Create a MorphableModel with expressions from the loaded neutral model and blendshapes:
    morphablemodel_with_expressions = eos.morphablemodel.MorphableModel(model.get_shape_model(), blendshapes,
                                                                        color_model=eos.morphablemodel.PcaModel(),
                                                                        vertex_definitions=None,
                                                                        texture_coordinates=model.get_texture_coordinates())
    landmark_mapper = eos.core.LandmarkMapper('D:\\eos\\share\\ibug_to_sfm.txt')
    edge_topology = eos.morphablemodel.load_edge_topology('D:\\eos\\share\\sfm_3448_edge_topology.json')
    contour_landmarks = eos.fitting.ContourLandmarks.load('D:\\eos\\share\\ibug_to_sfm.txt')
    model_contour = eos.fitting.ModelContour.load('D:\\eos\\share\\sfm_model_contours.json')
    
    udp_client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server_addr = ("192.168.0.115", 8001)
    #tcp_socket.connect(server_addr)
    data = bytes()
    
    while new_client_socket:
        while True:
            recv_data = new_client_socket.recv(2048)
            #print(recv_data)
            if b'{' in recv_data:
                data += recv_data
            elif b'}' in recv_data:
                data += recv_data
                if b'}{' in data:
                    data = bytes()
                    break
                else:
                    eos_dic = ast.literal_eval(data.decode('UTF-8'))
                    if "face_landmark[0]" in eos_dic:
                        eos_landmarks = get_eos_face_landmarks(eos_dic)
                        (mesh, pose, shape_coeffs, blendshape_coeffs) = eos.fitting.fit_shape_and_pose(morphablemodel_with_expressions,
        eos_landmarks, landmark_mapper, image_width, image_height, edge_topology, contour_landmarks, model_contour)
                        print(blendshape_coeffs)

                        #blendshape_data = json.dumps({"anger": blendshape_coeffs[0], "disgust": blendshape_coeffs[1], "fear": blendshape_coeffs[2], "happiness": blendshape_coeffs[3], "sadness": blendshape_coeffs[4], "surprise": blendshape_coeffs[5], "rotation_w": pose.get_rotation()[3], "rotation_x":pose.get_rotation()[0], "rotation_y": pose.get_rotation()[1], "rotation_z": pose.get_rotation()[2]})
                        
                        #print(blendshape_data)
                        #tcp_socket.send(str(blendshape_coeffs).encode("UTF-8"))
                        #udp_client.sendto(str(blendshape_data).encode("UTF-8"), server_addr)
                data = bytes()
            else:
                data += recv_data
                
            

def get_eos_face_landmarks(mediapipe_face_dic):
    landmarks = []
    for l in dic:
        index = "face_landmark[" + dic.get(l) + "]"
        x = eval(mediapipe_face_dic.get(index))[0] * 720
        y = eval(mediapipe_face_dic.get(index))[1] * 1280
        landmarks.append(eos.core.Landmark(l, [x, y]))
    return landmarks



def read_pts(filename):
    """A helper function to read the 68 ibug landmarks from a .pts file."""
    lines = open(filename).read().splitlines()
    lines = lines[3:71]

    landmarks = []
    ibug_index = 1  # count from 1 to 68 for all ibug landmarks
    for l in lines:
        coords = l.split()
        landmarks.append(eos.core.Landmark(str(ibug_index), [float(coords[0]), float(coords[1])]))
        ibug_index = ibug_index + 1

    return landmarks


if __name__ == "__main__":
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcp_server_socket.bind(("", 5567))
    tcp_server_socket.listen(128)
    print("wait for client")
    new_client_socket, client_addr = tcp_server_socket.accept()
    print("new client connected")
    main()
    new_client_socket.close()
    tcp_server_socket.close()

<filename>python/test.py
import eos
import numpy as np
import os
import socket
import json
import ast
import time


dic = {"1":"356","2":"323","3":"288","4":"397","5":"365","6":"379","7":"378","8":"400","9":"152","10":"176",
       "11":"149","12":"150","13":"136","14":"172","15":"58","16":"93","17":"127","18":"260","19":"259","20":"257",
       "21":"258","22":"286","23":"56","24":"28","25":"27","26":"29","27":"30","28":"6","29":"197","30":"195",
       "31":"4","32":"305","33":"326","34":"2","35":"97","36":"75","37":"263","38":"387","39":"385","40":"398",
       "41":"380","42":"373","43":"173","44":"158","45":"160","46":"246","47":"144","48":"153","49":"375","50":"270",
       "51":"267","52":"0","53":"37","54":"40","55":"185","56":"91","57":"84","58":"17","59":"314","60":"321",
       "61":"415","62":"311","63":"13","64":"81","65":"191","66":"178","67":"14","68":"402"}


def main():
    """Demo for running the eos fitting from Python."""
    image_width = 720 # Make sure to adjust these when using your own images!
    image_height = 1280

    model = eos.morphablemodel.load_model("D:\\eos\\share\\sfm_shape_3448.bin")
    blendshapes = eos.morphablemodel.load_blendshapes("D:\\eos\\share\\expression_blendshapes_3448.bin")
    # Create a MorphableModel with expressions from the loaded neutral model and blendshapes:
    morphablemodel_with_expressions = eos.morphablemodel.MorphableModel(model.get_shape_model(), blendshapes,
                                                                        color_model=eos.morphablemodel.PcaModel(),
                                                                        vertex_definitions=None,
                                                                        texture_coordinates=model.get_texture_coordinates())
    landmark_mapper = eos.core.LandmarkMapper('D:\\eos\\share\\ibug_to_sfm.txt')
    edge_topology = eos.morphablemodel.load_edge_topology('D:\\eos\\share\\sfm_3448_edge_topology.json')
    contour_landmarks = eos.fitting.ContourLandmarks.load('D:\\eos\\share\\ibug_to_sfm.txt')
    model_contour = eos.fitting.ModelContour.load('D:\\eos\\share\\sfm_model_contours.json')
    
    udp_client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server_addr = ("192.168.0.115", 8001)
    #tcp_socket.connect(server_addr)
    data = bytes()
    
    while new_client_socket:
        while True:
            recv_data = new_client_socket.recv(2048)
            #print(recv_data)
            if b'{' in recv_data:
                data += recv_data
            elif b'}' in recv_data:
                data += recv_data
                if b'}{' in data:
                    data = bytes()
                    break
                else:
                    eos_dic = ast.literal_eval(data.decode('UTF-8'))
                    if "face_landmark[0]" in eos_dic:
                        eos_landmarks = get_eos_face_landmarks(eos_dic)
                        (mesh, pose, shape_coeffs, blendshape_coeffs) = eos.fitting.fit_shape_and_pose(morphablemodel_with_expressions,
        eos_landmarks, landmark_mapper, image_width, image_height, edge_topology, contour_landmarks, model_contour)
                        print(blendshape_coeffs)

                        #blendshape_data = json.dumps({"anger": blendshape_coeffs[0], "disgust": blendshape_coeffs[1], "fear": blendshape_coeffs[2], "happiness": blendshape_coeffs[3], "sadness": blendshape_coeffs[4], "surprise": blendshape_coeffs[5], "rotation_w": pose.get_rotation()[3], "rotation_x":pose.get_rotation()[0], "rotation_y": pose.get_rotation()[1], "rotation_z": pose.get_rotation()[2]})
                        
                        #print(blendshape_data)
                        #tcp_socket.send(str(blendshape_coeffs).encode("UTF-8"))
                        #udp_client.sendto(str(blendshape_data).encode("UTF-8"), server_addr)
                data = bytes()
            else:
                data += recv_data
                
            

def get_eos_face_landmarks(mediapipe_face_dic):
    landmarks = []
    for l in dic:
        index = "face_landmark[" + dic.get(l) + "]"
        x = eval(mediapipe_face_dic.get(index))[0] * 720
        y = eval(mediapipe_face_dic.get(index))[1] * 1280
        landmarks.append(eos.core.Landmark(l, [x, y]))
    return landmarks



def read_pts(filename):
    """A helper function to read the 68 ibug landmarks from a .pts file."""
    lines = open(filename).read().splitlines()
    lines = lines[3:71]

    landmarks = []
    ibug_index = 1  # count from 1 to 68 for all ibug landmarks
    for l in lines:
        coords = l.split()
        landmarks.append(eos.core.Landmark(str(ibug_index), [float(coords[0]), float(coords[1])]))
        ibug_index = ibug_index + 1

    return landmarks


if __name__ == "__main__":
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcp_server_socket.bind(("", 5567))
    tcp_server_socket.listen(128)
    print("wait for client")
    new_client_socket, client_addr = tcp_server_socket.accept()
    print("new client connected")
    main()
    new_client_socket.close()
    tcp_server_socket.close()

Demo for running the eos fitting from Python. # Make sure to adjust these when using your own images! # Create a MorphableModel with expressions from the loaded neutral model and blendshapes: #tcp_socket.connect(server_addr) #print(recv_data) #blendshape_data = json.dumps({"anger": blendshape_coeffs[0], "disgust": blendshape_coeffs[1], "fear": blendshape_coeffs[2], "happiness": blendshape_coeffs[3], "sadness": blendshape_coeffs[4], "surprise": blendshape_coeffs[5], "rotation_w": pose.get_rotation()[3], "rotation_x":pose.get_rotation()[0], "rotation_y": pose.get_rotation()[1], "rotation_z": pose.get_rotation()[2]}) #print(blendshape_data) #tcp_socket.send(str(blendshape_coeffs).encode("UTF-8")) #udp_client.sendto(str(blendshape_data).encode("UTF-8"), server_addr) A helper function to read the 68 ibug landmarks from a .pts file. # count from 1 to 68 for all ibug landmarks