Advertisement
GeeckoDev

FaceSubstitution

Feb 9th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include "testApp.h"
  2.  
  3. using namespace ofxCv;
  4.  
  5. void testApp::setup() {
  6. #ifdef TARGET_OSX
  7.     //ofSetDataPathRoot("../data/");
  8. #endif
  9.     ofSetVerticalSync(true);
  10.     cloneReady = false;
  11.     cam.initGrabber(1280, 720);
  12.     clone.setup(cam.getWidth(), cam.getHeight());
  13.     ofFbo::Settings settings;
  14.     settings.width = cam.getWidth();
  15.     settings.height = cam.getHeight();
  16.     maskFbo.allocate(settings);
  17.     srcFbo.allocate(settings);
  18.     camTracker.setup();
  19.     srcTracker.setup();
  20.     srcTracker.setIterations(25);
  21.     srcTracker.setAttempts(4);
  22.  
  23.     faces.allowExt("jpg");
  24.     faces.allowExt("png");
  25.     faces.listDir("faces");
  26.     currentFace = 0;
  27.     if(faces.size()!=0){
  28.         loadFace(faces.getPath(currentFace));
  29.     }
  30. }
  31.  
  32. void testApp::update() {
  33.     cam.update();
  34.     if(cam.isFrameNew()) {
  35.         camTracker.update(toCv(cam));
  36.        
  37.         cloneReady = camTracker.getFound();
  38.         if(cloneReady) {
  39.             ofMesh camMesh = camTracker.getImageMesh();
  40.             camMesh.clearTexCoords();
  41.             camMesh.addTexCoords(srcPoints);
  42.            
  43.             maskFbo.begin();
  44.             ofClear(0, 255);
  45.             camMesh.draw();
  46.             maskFbo.end();
  47.            
  48.             srcFbo.begin();
  49.             ofClear(0, 255);
  50.             src.bind();
  51.             camMesh.draw();
  52.             src.unbind();
  53.             srcFbo.end();
  54.            
  55.             clone.setStrength(16);
  56.             clone.update(srcFbo.getTextureReference(), cam.getTextureReference(), maskFbo.getTextureReference());
  57.         }
  58.     }
  59. }
  60.  
  61. void testApp::draw() {
  62.     static std::clock_t lastFound;
  63.     static bool faceChanged = true;
  64.  
  65.     ofSetColor(255);
  66.    
  67.     if(src.getWidth() > 0 && cloneReady) {
  68.         clone.draw(0, 0);
  69.     } else {
  70.         cam.draw(0, 0);
  71.     }
  72.    
  73.     if(!camTracker.getFound()) {
  74.         drawHighlightString("camera face not found", 10, 10);
  75.  
  76.         if(((std::clock() - lastFound) / CLOCKS_PER_SEC)>(3*60) && !faceChanged) {
  77.             std::cout << "next face!" << std::endl;
  78.             faceChanged = true;
  79.  
  80.             currentFace++;
  81.             if (currentFace>=faces.size()) currentFace = 0;
  82.  
  83.             if(faces.size()!=0){
  84.                 loadFace(faces.getPath(currentFace));
  85.             }
  86.         }
  87.     }
  88.     else {
  89.         lastFound = std::clock();
  90.         faceChanged = false;
  91.     }
  92.  
  93.     if(src.getWidth() == 0) {
  94.         drawHighlightString("drag an image here", 10, 30);
  95.     } else if(!srcTracker.getFound()) {
  96.         drawHighlightString("image face not found", 10, 30);
  97.     }
  98. }
  99.  
  100. void testApp::loadFace(string face){
  101.     src.loadImage(face);
  102.     if(src.getWidth() > 0) {
  103.         srcTracker.update(toCv(src));
  104.         srcPoints = srcTracker.getImagePoints();
  105.     }
  106. }
  107.  
  108. void testApp::dragEvent(ofDragInfo dragInfo) {
  109.     loadFace(dragInfo.files[0]);
  110. }
  111.  
  112. void testApp::keyPressed(int key){
  113.     switch(key){
  114.     case OF_KEY_UP:
  115.         currentFace++;
  116.         break;
  117.     case OF_KEY_DOWN:
  118.         currentFace--;
  119.         break;
  120.     }
  121.     if (currentFace<0) currentFace = faces.size()-1;
  122.     else if (currentFace>=faces.size()) currentFace = 0;
  123.  
  124.     if(faces.size()!=0){
  125.         loadFace(faces.getPath(currentFace));
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement