schwittlick / ofxDarknet

darknet neural network addon for openFrameworks
MIT License
496 stars 89 forks source link

image/recorded video as input #21

Closed MyVanitar closed 7 years ago

MyVanitar commented 7 years ago

@mrzl @genekogan

Hi,

In the sample code, the default is a connected webcam or camera with device ID=0.

What about if we decided to process an image/recorded video instead of live video?

genekogan commented 7 years ago

in yolo:

ofVideoPlayer movie;   
darknet.yolo(movie.getPixels(), 0.24f);

in others, same basic idea. use movie.getPixels() as input.

MyVanitar commented 7 years ago

@genekogan

You mean I should modify inside ofApp.cpp ? Where the path to the single image or recorded video should be defined?

MyVanitar commented 7 years ago

@genekogan

This is all code inside that file:

#include "ofApp.h"

void ofApp::setup() 
{
    std::string cfgfile = ofToDataPath( "cfg/yolo-obj.cfg" );
    std::string weightfile = ofToDataPath( "yolo-obj_2000.weights" );
    std::string namesfile = ofToDataPath( "cfg/obj.names" );

    darknet.init( cfgfile, weightfile, namesfile );

    video.setDeviceID( 0 );
    video.setDesiredFrameRate( 30 );
    video.initGrabber( 640, 480 );
}

void ofApp::update()
{
    video.update();
}

void ofApp::draw()
{
    float thresh = ofMap( ofGetMouseX(), 0, ofGetWidth(), 0, 1 );

    ofSetColor( 255 );
    video.draw( 0, 0 );

    if( video.isFrameNew() ) {
        std::vector< detected_object > detections = darknet.yolo( video.getPixels(), thresh );

        ofNoFill(); 
        for( detected_object d : detections )
        {
            ofSetColor( d.color );
            glLineWidth( ofMap( d.probability, 0, 1, 0, 8 ) );
            ofNoFill();
            ofDrawRectangle( d.rect );
            ofDrawBitmapStringHighlight( d.label + ": " + ofToString(d.probability), d.rect.x, d.rect.y + 20 );
        }
    }
}
genekogan commented 7 years ago

you can swap ofVideoGrabber for ofVideoPlayer into darknet. anything that has pixels can be run through. see ofVideoPlayer documentation on how to use the movie player.

MyVanitar commented 7 years ago

Thank you. so as I understood, a single image will go through this either, yes?

genekogan commented 7 years ago

yes.

MyVanitar commented 7 years ago

Fine.