I’ve got two apps that run simultaneously. One is a face tracker, just watching for faces. Anytime it gets one it sends a “face” message over UDP (or “noface” if not). The second app receives those messages and plays a particular video based on the message.
However, there is a bit of a lag in the response time of the second app in relation to the face tracking of the first. I used to have these two apps written into one so have that as a point of comparison (it was faster). (I separated them to get the opencv into its own thread so it could have a whole CPU core to work on without affecting the playback rate of the video).
My abridged code is below. Am I approaching this in the wrong way somehow? Missing something? Or maybe this is just the price of the network overhead? The apps are on the same machine so I was expecting it to be near instantaneous. The lag is maybe a half second?
Here’s the code:
// ----------------------------
// RECEIVER APP (abbreviated)
// ----------------------------
void testApp::setup() {
gotFace = FALSE;
//create the socket and bind to port 11999
udpConnection.Create();
udpConnection.Bind(11999);
udpConnection.SetNonBlocking(TRUE);
// setup some movies to play
}
void testApp::update() {
char udpMessage[100000];
udpConnection.Receive(udpMessage,100000);
while(TRUE) {
string message = udpMessage;
if(message == "face") { gotFace = TRUE; }
else if(message == "noface") { gotFace = FALSE; }
else if(message == "") { break; }
udpConnection.Receive(udpMessage,100000);
}
// make some choices based on the message...
}
void testApp::draw() {
// draw the appropriate movie
}
// ----------------------------
// UDP SENDER APP (abbreviated)
// ----------------------------
#define RECONNECT_TIME 400
void testApp::setup(){
//create the socket and set to send to 127.0.0.1:11999
udpConnection.Create();
udpConnection.Connect("127.0.0.1",11999);
udpConnection.SetNonBlocking(TRUE);
// setup vidgrabber
// setup vision
}
void testApp::update(){
string message;
if(finder.blobs.size()) { message = "face"; }
else { message = "noface"; }
int sent = udpConnection.Send(message.c_str(),message.length());
// get new frames from vidgrabber for opencv
}
void testApp::draw() { }