Skip to content

Commit

Permalink
InteractionRecorder
Browse files Browse the repository at this point in the history
  • Loading branch information
arturoc committed Mar 10, 2012
1 parent 9f42d5a commit 73847ce
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 14 deletions.
15 changes: 11 additions & 4 deletions FaceSubstitution/src/FaceLoader.cpp
Expand Up @@ -101,7 +101,9 @@ void FaceLoader::threadedFunction(){
while(isThreadRunning()) {
loadNew.wait(mutex);
cout << "loading" << faces.getPath(currentFace) << endl;
loadFace(faces.getPath(currentFace));
string facePath = faces.getPath(currentFace);
loadFace(facePath);
ofNotifyEvent(newFaceLoadedE,facePath);
}
}

Expand All @@ -121,6 +123,10 @@ void FaceLoader::loadFace(string face){
}
}

string FaceLoader::getCurrentFacePath(){
return faces.getPath(currentFace);
}

ofImage & FaceLoader::getCurrentImg(){
return *currentImg;
}
Expand All @@ -133,7 +139,7 @@ vector<ofVec2f> & FaceLoader::getCurrentImagePoints(){
return *currentPoints;
}

void FaceLoader::loadNext(){
string FaceLoader::loadNext(){
mutex.lock();
std::swap(nextPoints,currentPoints);
std::swap(nextImg,currentImg);
Expand All @@ -147,9 +153,10 @@ void FaceLoader::loadNext(){
currentFace %= faces.size();
}
loadNew.signal();
return faces.getPath(currentFace);
}

void FaceLoader::loadPrevious(){
string FaceLoader::loadPrevious(){
mutex.lock();
std::swap(nextPoints,currentPoints);
std::swap(nextImg,currentImg);
Expand All @@ -163,7 +170,7 @@ void FaceLoader::loadPrevious(){
currentFace %= faces.size();
}
loadNew.signal();

return faces.getPath(currentFace);
}

int FaceLoader::getTotalFaces(){
Expand Down
7 changes: 5 additions & 2 deletions FaceSubstitution/src/FaceLoader.h
Expand Up @@ -37,12 +37,15 @@ class FaceLoader: public ofThread {

void threadedFunction();

void loadNext();
void loadPrevious();
string loadNext();
string loadPrevious();

int getTotalFaces();
int getCurrentFace();

string getCurrentFacePath();

ofEvent<string> newFaceLoadedE;
private:
void resizeAndDiscardImages(string path);
void loadFace(string face);
Expand Down
67 changes: 67 additions & 0 deletions FaceSubstitution/src/InteractionRecorder.cpp
@@ -0,0 +1,67 @@
/*
* InteractionRecorder.cpp
*
* Created on: 10/03/2012
* Author: arturo
*/

#include "InteractionRecorder.h"

InteractionRecorder::InteractionRecorder() {
settings.loadFile("serversettings.xml");
ftpServer = settings.getValue("server","arturocastro.net");
user = settings.getValue("user","user");
password = settings.getValue("password","password");
serverPath = settings.getValue("interaction_recordings_path","interaction_recordings_path");
folder = "recordings_interaction";
}

InteractionRecorder::~InteractionRecorder() {
// TODO Auto-generated destructor stub
}



void InteractionRecorder::setup(string path,string currentFace, int w, int h, int fps){
cout << "start recording at " << path << " with current face " << currentFace;
recordedVideoPath = path;
recorder.setup("recordings_interaction/"+recordedVideoPath,w,h,fps);
framesRecorded = 0;
ofFile recordedVideoMeta(ofFilePath::join(folder,recordedVideoPath)+".meta",ofFile::WriteOnly);
recordedVideoMeta << currentFace << " " << framesRecorded;
if(!isThreadRunning()) startThread(true,false);
}

void InteractionRecorder::addFrame(ofPixels & frame){
recorder.addFrame(frame);
}

void InteractionRecorder::changeFace(string face){
cout << "changed face to " << face;
ofFile recordedVideoMeta(ofFilePath::join(folder,recordedVideoPath)+".meta",ofFile::Append);
recordedVideoMeta << face << " " << framesRecorded;
}

void InteractionRecorder::close(){
recorder.close();
lock();
uploadQueue.push(recordedVideoPath);
unlock();
upload.signal();
}

void InteractionRecorder::threadedFunction(){
while(isThreadRunning()){
upload.wait(mutex);
while(!uploadQueue.empty()){
string nextUpload = uploadQueue.front();
uploadQueue.pop();
unlock();
string ftpCommand = "curl -u " + user+ ":" + password + " -T " + ofToDataPath(ofFilePath::join(folder,nextUpload)) + " ftp://" + ofFilePath::join(ftpServer , serverPath) + nextUpload;
system(ftpCommand.c_str());
ftpCommand = "curl -u " + user+ ":" + password + " -T " + ofToDataPath(ofFilePath::join(folder,nextUpload + ".meta")) + " ftp://" + ofFilePath::join(ftpServer , serverPath) + nextUpload + ".meta";
system(ftpCommand.c_str());
lock();
}
}
}
44 changes: 44 additions & 0 deletions FaceSubstitution/src/InteractionRecorder.h
@@ -0,0 +1,44 @@
/*
* InteractionRecorder.h
*
* Created on: 10/03/2012
* Author: arturo
*/

#ifndef INTERACTIONRECORDER_H_
#define INTERACTIONRECORDER_H_

#include <queue>
#include "ofConstants.h"
#include "ofThread.h"
#include "ofxVideoRecorder.h"
#include "ofxXmlSettings.h"

class InteractionRecorder: public ofThread {
public:
InteractionRecorder();
virtual ~InteractionRecorder();

void setup(string path,string currentFace,int w, int h, int fps=30);
void addFrame(ofPixels & frame);
void changeFace(string face);
void close();

protected:
void threadedFunction();

private:
ofxVideoRecorder recorder;
string recordedVideoPath;
int framesRecorded;
queue<string> uploadQueue;
Poco::Condition upload;
ofxXmlSettings settings;
string ftpServer;
string user;
string password;
string serverPath;
string folder;
};

#endif /* INTERACTIONRECORDER_H_ */
2 changes: 1 addition & 1 deletion FaceSubstitution/src/SnapshotSaver.cpp
Expand Up @@ -25,7 +25,7 @@ void SnapshotSaver::setup(string _folder){
ofDirectory dir(_folder);
if(!dir.exists()) dir.create(true);
folder = _folder;
settings.loadFile("settings.xml");
settings.loadFile("serversettings.xml");
ftpServer = settings.getValue("server","arturocastro.net");
user = settings.getValue("user","user");
password = settings.getValue("password","password");
Expand Down
31 changes: 25 additions & 6 deletions FaceSubstitution/src/testApp.cpp
Expand Up @@ -80,6 +80,7 @@ void testApp::setup() {
ofAddListener(blinkTrigger.longBlinkE,this,&testApp::longBlinkTriggered);

ofAddListener(blinkRecorder.recordedE,this,&testApp::videoRecorded);
ofAddListener(faceLoader.newFaceLoadedE,this,&testApp::newFaceLoaded);


// init other utils classes
Expand All @@ -96,6 +97,8 @@ void testApp::setup() {


showVideosChanged(gui.showVideos);
oneSec = ofGetElapsedTimef();
fps = 30;
}

void testApp::showVideosChanged(bool & v){
Expand All @@ -110,8 +113,10 @@ void testApp::recording(bool & rec){
}

void testApp::videoRecorded(bool & r){
//recordVideo = true;
//recorder.setup("recordings_interaction/"+ofGetTimestampString()+".mov",w,h,blinkRecorder.getFps());
}

void testApp::newFaceLoaded(string & face){
interactionRecorder.changeFace(face);
}

void testApp::update() {
Expand All @@ -130,8 +135,13 @@ void testApp::update() {
cloneReady = false;
}

if(!prevFound && cloneReady && frameProcessed){
recordVideo = true;
interactionRecorder.setup(ofGetTimestampString()+".mov",faceLoader.getCurrentFacePath(),video->getWidth(), video->getHeight(), fps);
}

if(!cloneReady && recordVideo){
recorder.encodeVideo();
interactionRecorder.close();
recordVideo = false;
}

Expand Down Expand Up @@ -167,14 +177,12 @@ void testApp::update() {

bool takeSnapshot = takeSnapshotFrom>0 && ofGetElapsedTimef()-takeSnapshotFrom>1.5;

if(takeSnapshot || recordVideo){
if(takeSnapshot){
clone.readToPixels(snapshot);

if(takeSnapshot){
snapshotSaver.save(snapshot);
takeSnapshotFrom = 0;
}else{
recorder.addFrame(snapshot);
}
}

Expand All @@ -187,6 +195,15 @@ void testApp::update() {

video->update();
if(video->isFrameNew()) {

float t = ofGetElapsedTimef();
framesOneSec++;
if(t-oneSec >= 1){
fps = float(framesOneSec)/(t-oneSec);
framesOneSec=0;
oneSec = t;
}

if(numInputRotation90!=0 && numInputRotation90!=2){
if(video->getWidth()!=rotatedInput.getHeight()){
rotatedInput.allocate(video->getHeight(),video->getWidth(),3);
Expand All @@ -211,6 +228,8 @@ void testApp::update() {
camTracker.update(toCv(*video));
}
if(gui.showVideos) blinkRecorder.update(video->getPixelsRef());
if(recordVideo)
interactionRecorder.addFrame(video->getPixelsRef());
}

if(blinkRecorder.isRecording()){
Expand Down
10 changes: 9 additions & 1 deletion FaceSubstitution/src/testApp.h
Expand Up @@ -7,6 +7,7 @@
#include "FaceLoader.h"
#include "FaceBlinkRecorder.h"
#include "VideoFader.h"
#include "InteractionRecorder.h"

//#define USE_GST_VIRTUAL_CAMERA

Expand All @@ -31,6 +32,8 @@ class testApp : public ofBaseApp {
void showVideosChanged(bool & v);
void videoRecorded(bool & r);

void newFaceLoaded(string & face);

ofxFaceTrackerThreaded camTracker;
ofVideoGrabber cam;
ofVideoPlayer vid;
Expand Down Expand Up @@ -67,7 +70,12 @@ class testApp : public ofBaseApp {

AutoExposure autoExposure;
bool adjustExposure;

bool recordVideo;
ofxVideoRecorder recorder;
InteractionRecorder interactionRecorder;


float oneSec;
int framesOneSec;
float fps;
};

0 comments on commit 73847ce

Please sign in to comment.