From 73847ceed11ca90764860bbffc60bba4a98785cf Mon Sep 17 00:00:00 2001 From: arturoc Date: Sat, 10 Mar 2012 10:46:06 +0100 Subject: [PATCH] InteractionRecorder --- FaceSubstitution/src/FaceLoader.cpp | 15 +++-- FaceSubstitution/src/FaceLoader.h | 7 +- FaceSubstitution/src/InteractionRecorder.cpp | 67 ++++++++++++++++++++ FaceSubstitution/src/InteractionRecorder.h | 44 +++++++++++++ FaceSubstitution/src/SnapshotSaver.cpp | 2 +- FaceSubstitution/src/testApp.cpp | 31 +++++++-- FaceSubstitution/src/testApp.h | 10 ++- 7 files changed, 162 insertions(+), 14 deletions(-) create mode 100644 FaceSubstitution/src/InteractionRecorder.cpp create mode 100644 FaceSubstitution/src/InteractionRecorder.h diff --git a/FaceSubstitution/src/FaceLoader.cpp b/FaceSubstitution/src/FaceLoader.cpp index a98a88e..76a3524 100644 --- a/FaceSubstitution/src/FaceLoader.cpp +++ b/FaceSubstitution/src/FaceLoader.cpp @@ -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); } } @@ -121,6 +123,10 @@ void FaceLoader::loadFace(string face){ } } +string FaceLoader::getCurrentFacePath(){ + return faces.getPath(currentFace); +} + ofImage & FaceLoader::getCurrentImg(){ return *currentImg; } @@ -133,7 +139,7 @@ vector & FaceLoader::getCurrentImagePoints(){ return *currentPoints; } -void FaceLoader::loadNext(){ +string FaceLoader::loadNext(){ mutex.lock(); std::swap(nextPoints,currentPoints); std::swap(nextImg,currentImg); @@ -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); @@ -163,7 +170,7 @@ void FaceLoader::loadPrevious(){ currentFace %= faces.size(); } loadNew.signal(); - + return faces.getPath(currentFace); } int FaceLoader::getTotalFaces(){ diff --git a/FaceSubstitution/src/FaceLoader.h b/FaceSubstitution/src/FaceLoader.h index 1c06db0..5879408 100644 --- a/FaceSubstitution/src/FaceLoader.h +++ b/FaceSubstitution/src/FaceLoader.h @@ -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 newFaceLoadedE; private: void resizeAndDiscardImages(string path); void loadFace(string face); diff --git a/FaceSubstitution/src/InteractionRecorder.cpp b/FaceSubstitution/src/InteractionRecorder.cpp new file mode 100644 index 0000000..e062ffb --- /dev/null +++ b/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(); + } + } +} diff --git a/FaceSubstitution/src/InteractionRecorder.h b/FaceSubstitution/src/InteractionRecorder.h new file mode 100644 index 0000000..f2c632a --- /dev/null +++ b/FaceSubstitution/src/InteractionRecorder.h @@ -0,0 +1,44 @@ +/* + * InteractionRecorder.h + * + * Created on: 10/03/2012 + * Author: arturo + */ + +#ifndef INTERACTIONRECORDER_H_ +#define INTERACTIONRECORDER_H_ + +#include +#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 uploadQueue; + Poco::Condition upload; + ofxXmlSettings settings; + string ftpServer; + string user; + string password; + string serverPath; + string folder; +}; + +#endif /* INTERACTIONRECORDER_H_ */ diff --git a/FaceSubstitution/src/SnapshotSaver.cpp b/FaceSubstitution/src/SnapshotSaver.cpp index d77816c..f8797d7 100644 --- a/FaceSubstitution/src/SnapshotSaver.cpp +++ b/FaceSubstitution/src/SnapshotSaver.cpp @@ -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"); diff --git a/FaceSubstitution/src/testApp.cpp b/FaceSubstitution/src/testApp.cpp index f6f28be..c964752 100644 --- a/FaceSubstitution/src/testApp.cpp +++ b/FaceSubstitution/src/testApp.cpp @@ -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 @@ -96,6 +97,8 @@ void testApp::setup() { showVideosChanged(gui.showVideos); + oneSec = ofGetElapsedTimef(); + fps = 30; } void testApp::showVideosChanged(bool & v){ @@ -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() { @@ -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; } @@ -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); } } @@ -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); @@ -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()){ diff --git a/FaceSubstitution/src/testApp.h b/FaceSubstitution/src/testApp.h index 630170c..af2be19 100644 --- a/FaceSubstitution/src/testApp.h +++ b/FaceSubstitution/src/testApp.h @@ -7,6 +7,7 @@ #include "FaceLoader.h" #include "FaceBlinkRecorder.h" #include "VideoFader.h" +#include "InteractionRecorder.h" //#define USE_GST_VIRTUAL_CAMERA @@ -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; @@ -67,7 +70,12 @@ class testApp : public ofBaseApp { AutoExposure autoExposure; bool adjustExposure; + bool recordVideo; - ofxVideoRecorder recorder; + InteractionRecorder interactionRecorder; + + float oneSec; + int framesOneSec; + float fps; };