trying to build a super-simple threaded opencv vidgrabber haar finder

I’m trying to build a super simple barebones opencv vidgrabber haar finder that is threaded, using ofxThread. I’ve tried to craft one using my working non-threaded haar finder and the ofxThread example, but am running into multiple problems. It builds but dies, best I can tell complaining about ofTexture::loadData().

I found another thread where someone built a multi-threaded haar finder but it is not simple and is not making much sense to me.

I did read about the need to not use drawing functions in the thread. I had hoped that calling my thread’s draw() from the main draw() would get around this, but maybe not? If not, how do I thread the finder while also getting its results drawn to the screen?

Here’s the code:

  
// *** testApp.cpp *** //  
//  
#include "testApp.h"  
  
void testApp::setup(){ TO.start(); }  
void testApp::update(){ }  
void testApp::draw(){ TO.draw(); }  
  
  
  
// *** testApp.h *** //  
#pragma once  
#include "ofMain.h"  
#include "threadedObject.h"  
  
class testApp : public ofBaseApp{  
	public:  
		void setup();  
		void update();  
		void draw();  
        threadedObject TO;  
      
};  
  
  
  
// *** threadedObject.h *** //  
#ifndef _THREADED_OBJECT  
#define _THREADED_OBJECT  
  
#include "ofMain.h"  
#include "ofxCvHaarFinder.h"  
#include "ofxOpenCv.h"  
  
class threadedObject : public ofThread{  
      
public:  
    ofxCvHaarFinder finder;  
    ofVideoGrabber  vidGrabber;  
    ofxCvGrayscaleImage grayImage;  
    ofxCvColorImage colorImage;  
      
    threadedObject(){  
        vidGrabber.setVerbose(true);  
        vidGrabber.initGrabber(320,240);  
        vidGrabber.setDesiredFrameRate(15);  
          
        grayImage.allocate(320,240);  
        colorImage.allocate(320,240);  
        finder.setup("haarcascade_frontalface_alt2.xml");  
        finder.setNeighbors(3);  
    }  
      
    void start(){  
        startThread(true, false);   // blocking, verbose  
    }  
      
    void stop(){  
        stopThread();  
    }  
      
    void threadedFunction(){  
          
        while( isThreadRunning() != 0 ){  
            if( lock() ){  
                bool newFrame = false;  
                  
                vidGrabber.grabFrame();  
                newFrame = vidGrabber.isFrameNew();  
                  
                if(newFrame) {  
                    colorImage.setFromPixels(vidGrabber.getPixels(),320,240);  
                    grayImage = colorImage;  
                    finder.findHaarObjects(grayImage, 80, 80);  
                }  
                unlock();  
            }  
        }  
    }  
      
    void draw(){  
          
        if( lock() ){  
            colorImage.draw(0, 0);  
            ofNoFill();  
              
            for(int i = 0; i < finder.blobs.size(); i++) {  
                ofRectangle cur = finder.blobs[i].boundingRect;  
                ofRect(cur.x, cur.y, cur.width, cur.height);  
            }  
            unlock();  
        }  
    }  
};  
  
#endif  
  
  
  

all the images have a texture that is updated on update. you need to deactivate it with setUseTexture(false) and then use a texture to actually draw the data in draw

what Arturo is saying and be aware that there was (at least in my version) a protected ofxCvGrayscaleImage in ofxCvHaarFinder that uses a texture

The way i got around it was to extend ofxCvHaarFinder as opposed to modding it
https://github.com/jvcleave/MultiThreadedHaarFinder/blob/master/src/jvcHaarFinder.cpp

AFAIK, locking and the threaded function will give you no performance improvement because what lock does is to prevent any other thread to run.
BTW, OpenGL calls can nly be done from the main thread, hence draw() and anything related to textures.
on ofxThread only threadedfunction runs on a different thread, any other function will run on the main thread.
Check this thread as it might give you a good approach about threads and share images between them.
http://forum.openframeworks.cc/t/simple-particles-system-and-thread/7248/0