Skip to content

Instantly share code, notes, and snippets.

@HalfdanJ
Last active February 4, 2017 14:03
Show Gist options
  • Save HalfdanJ/e8b29fec9bcd845a045b96a569a8b933 to your computer and use it in GitHub Desktop.
Save HalfdanJ/e8b29fec9bcd845a045b96a569a8b933 to your computer and use it in GitHub Desktop.
Small ofApp that forwards the last layer from darknet model to osc to use with Wekinator
ofxDarknet
ofxOpenCv
ofxOsc
#include "ofMain.h"
#include "ofApp.h"
//========================================================================
int main( ){
ofSetupOpenGL(1024,768,OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new ofApp());
}
#include "ofApp.h"
void ofApp::setup()
{
std::string cfgfile = ofToDataPath( "cfg/darknet.cfg" );
std::string weightfile = ofToDataPath( "darknet.weights" );
std::string nameslist = ofToDataPath( "cfg/imagenet.shortnames.list" );
darknet.init( cfgfile, weightfile, nameslist );
video.setDeviceID( 0 );
video.setDesiredFrameRate( 30 );
video.initGrabber( 320, 240 );
osc.setup("localhost", 6448);
}
void ofApp::update()
{
video.update();
}
void ofApp::draw()
{
video.draw( 700, 100 );
if( video.isFrameNew() ) {
if(ofGetKeyPressed(' ')){
classifications = darknet.classify( video.getPixels() );
// Layer to extract
int n = 13;
ofPushMatrix();
// Get the float array from the gpu
float * layer = get_network_output_layer_gpu( darknet.net, n);
// Info about the layer
auto l = darknet.net.layers[n];
// Draw the layer
float squareSize = 50.0;
float size = squareSize / l.out_w;
int i=0;
for(int u=0;u<l.out_c;u++){
for(int y=0;y<l.out_h;y++){
for(int x=0;x<l.out_w;x++){
ofSetColor(ofClamp(layer[i]*255,0,255));
ofDrawRectangle(x*size, y*size, size, size);
i++;
}
}
ofTranslate(squareSize+5,0);
if(u != 0 && u%11==10){
ofTranslate(-(squareSize+5)*11,(squareSize+5));
}
}
ofPopMatrix();
ofSetColor(255);
// Send layer over osc
ofxOscMessage m;
m.setAddress("/wek/inputs");
for(int i=0;i<l.out_c*l.out_w*l.out_h;i+=2){
m.addFloatArg(layer[i]);
}
ofLog()<<m.getNumArgs();
osc.sendMessage(m, false);
}
}
}
#pragma once
#include "ofMain.h"
#include "ofxOsc.h"
#include "ofxDarknet.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofxDarknet darknet;
ofVideoGrabber video;
std::vector< classification > classifications;
ofxOscSender osc;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment