Getting Haar to update with a new image loaded

Hi guys,
I’m super new to openFrameworks and programming, so I’m probably missing something easy and obvious.

I’m trying to build a little app that will let a user load an image and then find faces in the scene, allow them to save the image and load the next one.

I found the load example and that works fine, but i’m unsure how to get the image to update each time and draw boxes over faces.

I’ve tried putting this in bothofApp::keyPressed and ofApp::KeyReleased, but I’m not getting the results I’m hoping for ( boxes drawn over faces )

if (key == 'f') {
		for (size_t i = 0, ilen = haar.blobs.size(); i < ilen; i++) {
			ofNoFill();
			ofDrawRectangle(haar.blobs[i].boundingRect);
		}

in ofApp::update(), I only have
haar.findHaarObjects(image)

do I need to have a image.isFrameNew in update to make sure it refreshes each time?

Thanks for any help you can give.

Hi @ekaj,

I will assume you are talking about this example?

Maybe try this:
in ofApp.cpp::draw():

for (size_t i = 0, ilen = haar.blobs.size(); i < ilen; i++) {
    ofNoFill();
    haar.blobs[i].draw();

}

Rather than doing it in ofApp::keyPressed as what’s being on screen is rendered through the draw function.

But then you will find that pressing the f key has to trigger the load / image processing of an other image, so you might want to create a function in there, like ’ loadNextImgAndProcessHaar` .

Hope this helps,

Best,

P

Hi Pierre,
I was talking about a combination of that Haar example and this File Load example

Individually, they work great, but if I try to combine the two, the Haar stops working (or at least does’nt draw rectangles)

Putting it in Draw doesn’t seem to have any effect either.

alright,

Feel free to put some code here, I’ll be happy to take a look.

Best,

P

Just ran the example with an extra function reload added, seems to work on my end :

//--------------------------------------------------------------
void ofApp::keyReleased(int key){
    if(key == 'f')reload();
}

//--------------------------------------------------------------
void ofApp::reload(){
    img.load("helena-lopes-e3OUQGT9bWU-unsplash.jpg");
    finder.findHaarObjects(img);
    
}

One thing to note is that the type of image supported is jpeg.

Let me know if you still have any issues with it.

Best,

P

This is heavily cobbled together from those two examples, so hopefully it’s something obvious, but here is my ofApp.h and ofApp.cpp
ofApp.cpp (2.8 KB) ofApp.h (783 Bytes)

I wasn’t sure if forum etiquette was to upload the files or cut and paste code.

I also tried moving the draw into the processOpenFileSelection part of the code if you’re looking for it.

ofApp.cpp (2.6 KB) ofApp.h (349 Bytes)
I re worked it. ‘f’ processes the faces in the image. No need to use arrays if you only use one component, so I removed them.
Also needs to only use jpg.

Have a look.

Best,

P

1 Like

Thanks Pierre.
This is working for me.
Now I’ll spend some timing looking at what you changed and trying to understand what I was doing wrong.

Thanks again!

1 Like