Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

ofxFaceTracker from base64 image

$
0
0

@caiodv wrote:

Hey everyone!
I'm loading an image received as a base64 string from a socket client into openframeworks, the image is loading fine, it is a cam feed from a website I'm running in localhost.
The problem is that I just can't get ofxFaceTracker to work with this image, there are no errors but tracker.getFound() is always returning 0.

Guess the code will explain itself.

ofApp.h

#pragma once
#include "ofMain.h"
#include "ofxCv.h"
#include "ofxSocketIO.h"
#include "ofxSocketIOData.h"
using namespace ofxCv;
using namespace cv;
#include "ofxFaceTracker.h"
class ofApp : public ofBaseApp{
    public:
	void setup();
	void update();
	void draw();
    void onConnection();
    void bindEvents();
    void gotEvent(std::string& name);
    void onImageEvent(ofxSocketIOData& data);

ofxFaceTracker tracker;
ofImage img;
bool hasImg;

ofxSocketIO socketIO;
bool isConnected;

ofEvent<ofxSocketIOData&> serverEvent;
ofEvent<ofxSocketIOData&> imageEvent;

std::string address;
std::string status;
std::string base64;
};

ofApp.cpp

#include "ofApp.h"
#include "Poco/Base64Decoder.h"
using namespace ofxCv;
using namespace cv;
//--------------------------------------------------------------
void ofApp::setup(){
    img.allocate(320,240, OF_IMAGE_GRAYSCALE);
    tracker.setup();
    hasImg = false;
    isConnected = false;
    address = "http://localhost:3000";
    status = "not connected";
    base64 = "";
    socketIO.setup(address);
    ofAddListener(socketIO.notifyEvent, this, &ofApp::gotEvent);
    ofAddListener(socketIO.connectionEvent, this, &ofApp::onConnection);
}
void ofApp::onConnection () {
    isConnected = true;
    bindEvents();
}
void ofApp::bindEvents () {
    std::string imageEventName = "image";
    socketIO.bindEvent(imageEvent, imageEventName);
    ofAddListener(imageEvent, this, &ofApp::onImageEvent);
}
void ofApp::onImageEvent (ofxSocketIOData& data) {
    base64 = data.getStringValue("buffer");
    if(base64.length() > 100){
        hasImg = true;
    }
}
void ofApp::gotEvent(string& name) {
    status = name;
}
//--------------------------------------------------------------
void ofApp::update(){
//not really sure if this part is correct
    if(img.isUsingTexture()){
        tracker.update(toCv(img));
        cout << ofToString(tracker.getFound()) << endl;
    }
    if(hasImg){
        stringstream ss;
        ss << base64;
        Poco::Base64Decoder b64in(ss);
        ofBuffer buffer;
        b64in >> buffer;
        img.load(buffer);
        hasImg = false;
    }
}
//--------------------------------------------------------------
void ofApp::draw(){
    ofColor(255);
    img.draw(0,0);
    if(tracker.getFound()) {
        ofSetLineWidth(1);
        tracker.draw();
    }
}

Any ideas?

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles