Skip to content

Commit

Permalink
real-time OpenFace with configuration file for ZeroMQ
Browse files Browse the repository at this point in the history
  • Loading branch information
NumesSanguis committed Apr 18, 2018
1 parent b6fea8b commit 482800d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 29 deletions.
104 changes: 75 additions & 29 deletions openface/MainWindow.xaml.cs
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
using NetMQ.Sockets;
using Newtonsoft.Json;
using System.Net;
using System.IO;
using System.Xml;

namespace OpenFaceOffline
{
Expand All @@ -78,24 +80,30 @@ public struct JsonData
public double confidence;
public struct Pose
{
public double x;
public double y;
public double z;
public double pitch;
public double roll;
public double yaw;
public double pose_Tx;
public double pose_Ty;
public double pose_Tz;
public double pose_Rx;
public double pose_Ry;
public double pose_Rz;
}
public Pose pose;
public struct Gaze
{
public double x;
public double y;
//public Point
public double gaze_angle_x;
public double gaze_angle_y;
public double gaze_0_x;
public double gaze_0_y;
public double gaze_0_z;
public double gaze_1_x;
public double gaze_1_y;
public double gaze_1_z;
}
public Gaze gaze;
public Dictionary<string, double> au_c;
public Dictionary<string, double> au_r;
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
Expand All @@ -104,6 +112,8 @@ public partial class MainWindow : Window
// By Huang
PublisherSocket pubSocket = null;
long frame_no = 0;
string topic = "openface";
//Mode running_mode = Mode.standalone;

// Timing for measuring FPS
#region High-Resolution Timing
Expand Down Expand Up @@ -184,11 +194,44 @@ public static DateTime CurrentTime

public MainWindow()
{
String hostName = Dns.GetHostName();
bool bPush = false;

string configfile_name = "config.xml";

FileStream stream = new FileStream(configfile_name, FileMode.Open);
XmlDocument document = new XmlDocument();
document.Load(stream);

string serveraddress = "localhost";
int port = 5570;


XmlNodeList list = document.GetElementsByTagName("Mode");
if (list.Count > 0 && ((XmlElement)list[0]).InnerText.ToLower().Equals("push"))
bPush = true;
list = document.GetElementsByTagName("IP");
if(list.Count > 0)
{
serveraddress = ((XmlElement)list[0]).InnerText;
}
list = document.GetElementsByTagName("Port");
if(list.Count > 0)
{
port = Int32.Parse(((XmlElement)list[0]).InnerText);
}
list = document.GetElementsByTagName("Topic");
if(list.Count > 0)
{
topic = ((XmlElement)list[0]).InnerText;
}


String hostName = Dns.GetHostName();
IPAddress[] addresses = Dns.GetHostAddresses(hostName);

string myaddress = "localhost";


foreach (IPAddress address in addresses)
{
if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
Expand Down Expand Up @@ -216,7 +259,10 @@ public MainWindow()
// Added by Huang
pubSocket = new PublisherSocket();
pubSocket.Options.SendHighWatermark = 1000;
pubSocket.Connect("tcp://" + myaddress + ":5570"); //bind
if(bPush)
pubSocket.Connect("tcp://" + serveraddress + ":" + port);
else
pubSocket.Bind("tcp://" + myaddress + ":" + port);

}

Expand Down Expand Up @@ -467,15 +513,14 @@ private void RecordObservation(RecorderOpenFace recorder, RawImage vis_image, bo
// added by Huang
private void SendZeroMQMessage(bool success, float fx, float fy, float cx, float cy, double openface_timestamp)
{
//List<Tuple<double, double>> eye_landmarks = null;
Tuple<double, double> gaze_angle = new Tuple<double, double>(0, 0);
List<double> pose = new List<double>();
List<double> non_rigid_params = landmark_detector.GetNonRigidParams();



NetMQMessage output_message = new NetMQMessage();
output_message.Append("openface");
output_message.Append(topic);

JsonData json_data = new JsonData();

Expand All @@ -496,27 +541,28 @@ private void SendZeroMQMessage(bool success, float fx, float fy, float cx, float

pose = new List<double>();
landmark_detector.GetPose(pose, fx, fy, cx, cy);
//json_data.pose.yaw = pose[4] * 180 / Math.PI + 0.5;
//json_data.pose.roll = pose[5] * 180 / Math.PI + 0.5;
//json_data.pose.pitch = pose[3] * 180 / Math.PI + 0.5;
json_data.pose.yaw = pose[4];
json_data.pose.roll = pose[5];
json_data.pose.pitch = pose[3];
json_data.pose.x = pose[0];
json_data.pose.y = pose[1];
json_data.pose.z = pose[2];

gaze_angle = gaze_analyser.GetGazeAngle();
//json_data.gaze.x = gaze_angle.Item1 * (180.0 / Math.PI);
//json_data.gaze.y = gaze_angle.Item2 * (180.0 / Math.PI);
json_data.gaze.x = gaze_angle.Item1;
json_data.gaze.y = gaze_angle.Item2;
json_data.pose.pose_Tx = pose[0];
json_data.pose.pose_Ty = pose[1];
json_data.pose.pose_Tz = pose[2];
json_data.pose.pose_Rx = pose[3];
json_data.pose.pose_Ry = pose[4];
json_data.pose.pose_Rz = pose[5];

json_data.au_c = face_analyser.GetCurrentAUsClass();
gaze_angle = gaze_analyser.GetGazeAngle();
var gaze = gaze_analyser.GetGazeCamera();

json_data.gaze.gaze_angle_x = gaze_angle.Item1;
json_data.gaze.gaze_angle_y = gaze_angle.Item2;
json_data.gaze.gaze_0_x = gaze.Item1.Item1;
json_data.gaze.gaze_0_y = gaze.Item1.Item2;
json_data.gaze.gaze_0_z = gaze.Item1.Item3;
json_data.gaze.gaze_1_x = gaze.Item2.Item1;
json_data.gaze.gaze_1_y = gaze.Item2.Item2;
json_data.gaze.gaze_1_z = gaze.Item2.Item3;

json_data.au_c = face_analyser.GetCurrentAUsClass();

//var au_regs_scaled = new Dictionary<String, double>();
Dictionary<string, double> au_regs = face_analyser.GetCurrentAUsReg();
json_data.au_r = new Dictionary<string, double>();
foreach (KeyValuePair<string, double> au_reg in au_regs)
Expand Down
9 changes: 9 additions & 0 deletions openface/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Config>
<!-- push or pull, push uses connect function, pull uses bind function -->
<Mode>push</Mode>
<!-- <IP>192.168.11.3</IP> -->
<!-- <IP>127.0.0.1</IP> -->
<IP>127.0.0.1</IP>
<Port>5570</Port>
<Topic>openface</Topic>
</Config>

0 comments on commit 482800d

Please sign in to comment.