kylemcdonald / ofxFaceTracker

CLM face tracking addon for openFrameworks based on Jason Saragih's FaceTracker.
http://facetracker.net/
Other
1.39k stars 371 forks source link

using FaceTracker on iOS #65

Open daijimachine opened 10 years ago

daijimachine commented 10 years ago

Hello,

I am trying to use FaceTracker on iOS. But texture is not displayed. Black mesh is displayed. Why does this happen?

I also referred to this article. https://github.com/kylemcdonald/ofxCv/issues/14

Regards, daijima

capture

kylemcdonald commented 10 years ago

can you post your code? or, which example are you using?

daijimachine commented 10 years ago

Hi and thanks for the reply.

Here's my code.

testApp.h

#pragma once

#include "ofMain.h"
#include "ofxiOS.h"
#include "ofxiOSExtras.h"
#include "ofxOpenCv.h"
#include "ofxFaceTracker.h"

class testApp : public ofxiOSApp{

    public:
        void setup();
        void update();
        void draw();
        void exit();

        void touchDown(ofTouchEventArgs & touch);
        void touchMoved(ofTouchEventArgs & touch);
        void touchUp(ofTouchEventArgs & touch);
        void touchDoubleTap(ofTouchEventArgs & touch);
        void touchCancelled(ofTouchEventArgs & touch);

        void lostFocus();
        void gotFocus();
        void gotMemoryWarning();
        void deviceOrientationChanged(int newOrientation);
        ofVideoGrabber cam;
        ofxFaceTracker tracker;
        ofxFaceTracker imgTracker;
        ofImage faceImage;
};

testApp.mm

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){  
    ofSetFrameRate(30);
    ofSetVerticalSync(true);
    ofEnableAlphaBlending();
    cam.setDeviceID(1);
    cam.initGrabber(640, 1136);
    faceImage.allocate(640, 1136, OF_IMAGE_COLOR);
    faceImage.loadImage("matsumoto.jpg");
    tracker.setup();
    imgTracker.setup();
}

//--------------------------------------------------------------
void testApp::update(){

    cam.update();

    if(cam.isFrameNew()) {
        imgTracker.update(ofxCv::toCv(faceImage));
        tracker.update(ofxCv::toCv(cam));
    }
}

//--------------------------------------------------------------
void testApp::draw(){
    ofSetColor(255);
    cam.draw(0, 0);
    ofDrawBitmapString(ofToString((int) ofGetFrameRate()), 10, 20);
    if(tracker.getFound()) {
        ofMesh objectMesh = tracker.getObjectMesh();
        ofMesh imgMesh = imgTracker.getObjectMesh();
        for(int i = 0; i < objectMesh.getNumVertices();i++){
            ofVec3f vertex = objectMesh.getVertex(i);
            imgMesh.setVertex(i, vertex);
        }

        ofVec2f position = tracker.getPosition();
        float scale = tracker.getScale();
        ofVec3f orientation = tracker.getOrientation();
        ofPushMatrix();
        ofTranslate(position.x, position.y);
        ofScale(scale, scale, scale);
        ofRotateX(orientation.x*45.0f);
        ofRotateY(orientation.y*45.0f);
        ofRotateZ(orientation.z*45.0f);
        ofSetColor(255,255,127,255);
        faceImage.getTextureReference().bind();
        imgMesh.draw();
        faceImage.getTextureReference().unbind();
        ofPopMatrix();
        faceImage.draw(0, 0, 100, 100);
    }
}

//--------------------------------------------------------------
void testApp::exit(){

}

//--------------------------------------------------------------
void testApp::touchDown(ofTouchEventArgs & touch){

}

//--------------------------------------------------------------
void testApp::touchMoved(ofTouchEventArgs & touch){

}

//--------------------------------------------------------------
void testApp::touchUp(ofTouchEventArgs & touch){

}

//--------------------------------------------------------------
void testApp::touchDoubleTap(ofTouchEventArgs & touch){

}

//--------------------------------------------------------------
void testApp::touchCancelled(ofTouchEventArgs & touch){

}

//--------------------------------------------------------------
void testApp::lostFocus(){

}

//--------------------------------------------------------------
void testApp::gotFocus(){

}

//--------------------------------------------------------------
void testApp::gotMemoryWarning(){

}

//--------------------------------------------------------------
void testApp::deviceOrientationChanged(int newOrientation){

}   

ofiPhoneVideoGrabber.mm

ofPixelsRef ofiPhoneVideoGrabber::getPixelsRef(){
    ofPixels pixels;
    pixels.setFromExternalPixels(getPixels(), getWidth(), getHeight(), 3);
    return pixels;
}
danoli3 commented 10 years ago

Hey, this is a ARB Texture Coordinates issue.

@daijimachine Try this out:

void testApp::draw(){
    ofSetColor(255);
    cam.draw(0, 0);
    ofDrawBitmapString(ofToString((int) ofGetFrameRate()), 10, 20);
    if(tracker.getFound()) {
        ofMesh objectMesh = tracker.getObjectMesh();
        for(int i=0; i< objectMesh.getTexCoords().size(); i++) {
                ofVec2f & texCoord = objectMesh.getTexCoords()[i];
                texCoord.x /= ofNextPow2(cam.getWidth());
                texCoord.y /= ofNextPow2(cam.getHeight());
        }
        ofMesh imgMesh = imgTracker.getObjectMesh();
        for(int i=0; i< imgMesh.getTexCoords().size(); i++) {
                ofVec2f & texCoord = imgMesh.getTexCoords()[i];
                texCoord.x /= ofNextPow2(faceImage.getWidth());
                texCoord.y /= ofNextPow2(faceImage.getHeight());
        }
        for(int i = 0; i < objectMesh.getNumVertices();i++){
            ofVec3f vertex = objectMesh.getVertex(i);
            imgMesh.setVertex(i, vertex);
        }

        ofVec2f position = tracker.getPosition();
        float scale = tracker.getScale();
        ofVec3f orientation = tracker.getOrientation();
        ofPushMatrix();
        ofTranslate(position.x, position.y);
        ofScale(scale, scale, scale);
        ofRotateX(orientation.x*45.0f);
        ofRotateY(orientation.y*45.0f);
        ofRotateZ(orientation.z*45.0f);
        ofSetColor(255,255,127,255);
        faceImage.getTextureReference().bind();
        imgMesh.draw();
        faceImage.getTextureReference().unbind();
        ofPopMatrix();
        faceImage.draw(0, 0, 100, 100);
    }
}

Excerpt from above... here we are changing the texture coordinates to be between 0 to 1 of the total texture width of the image. (if the texture was (320x480) the texture would increase to 512x512, so this compensates for that increase)

ofMesh objectMesh = tracker.getObjectMesh();
        for(int i=0; i< objectMesh.getTexCoords().size(); i++) {
                ofVec2f & texCoord = objectMesh.getTexCoords()[i];
                texCoord.x /= ofNextPow2(cam.getWidth());
                texCoord.y /= ofNextPow2(cam.getHeight());
}