Page 1 of 2

select a face from an image.

Posted: Sun Mar 10, 2013 9:44 am
by jassing
I am writing an app that (part of) it is to take a picture against a static background. (ie: Person stands in front of a white (or whatever colour) background, and then (via webcam) grab an image.

Has anyone done anything like auto-selecting a rectangle that is just slightly outside of the persons face?
I figure this can be done by looking at whole image, find pixels that are 'out of bounds' of the defined colour (to account for light variations, wrinkles in the background, say "20% off from white" -- once you find pixels that don't match, that would be the 'face' and then crop the image, starting slightly to the upper and left of the 1st off pixel, then select to just past the last pixel (lower right)

I have next to zero experience manipulating images, so I'm not sure of the best way to go on this...

Re: select a face from an image.

Posted: Sun Mar 10, 2013 10:11 am
by Danilo
Wikipedia: Face_detection leads you to Wikipedia: OpenCV, a cross-platform library.

See http://opencv.org/ for Docs, Tutorials, Downloads.

For example: Ever wondered how your digital camera detects peoples and faces? Look here to find out!

Image

But maybe that's just too much and you are looking for a simpler approach?

Re: select a face from an image.

Posted: Sun Mar 10, 2013 12:06 pm
by Little John
Hi Danilo,

thanks for the links, very interesting!
http://en.wikipedia.org/wiki/OpenCV wrote:All of the new developments and algorithms in OpenCV are now developed in the C++ interface.
Unfortunately, I don't know much about C++. Can we access the C++ interface with PureBasic?

Re: select a face from an image.

Posted: Sun Mar 10, 2013 12:18 pm
by Azur

Re: select a face from an image.

Posted: Sun Mar 10, 2013 2:20 pm
by Danilo
Little John wrote:Unfortunately, I don't know much about C++. Can we access the C++ interface with PureBasic?
Everything is possible if you find somebody who has some time left for doing it (direct access hack or wrapper). :)
Usually such things need some more time, it is not done in 10 minutes. So I can't help you atm.

Found some articles explaining OpenCV face detection. Start there: "How Face Detection Works" and check some other articles on this site: Seeing With OpenCV - A Five-Part Series

Maybe you can implement the Viola-Jones method yourself directly, with some additional research?

Re: select a face from an image.

Posted: Sun Mar 10, 2013 4:48 pm
by idle
If you're on windows you can have a look at this example I did a while back

webcam face tracker example.
http://www.purebasic.fr/english/viewtop ... 12&t=37148

Re: select a face from an image.

Posted: Mon Mar 11, 2013 3:02 am
by Danilo
Little John wrote:Can we access the C++ interface with PureBasic?
Had some time, so I used the following code to translate to PureBasic: Seeing With OpenCV, Part 2: Finding Faces in Images

DOWNLOAD: OpenCV244_PB510_FaceDetect_Test_001.zip (3,39MB)

Tested with XP and Win7. It is just a simple test, try with some smaller images (but faces not too small!). ;)

You could also test with other "opencv\data\haarcascades\*.xml" files, for example smile detection.

Example (myself):
Image

Re: select a face from an image.

Posted: Mon Mar 11, 2013 4:43 am
by jassing
idle wrote:If you're on windows you can have a look at this example I did a while back

webcam face tracker example.
http://www.purebasic.fr/english/viewtop ... 12&t=37148
thank you --I'll have a look.
I wanted it to be as automatic as possible.

Re: select a face from an image.

Posted: Mon Mar 11, 2013 5:17 am
by idle
Try the openCV example.
In my example I think you might need to specify a threshold range, I can't remember
and I can't run it in the VM to check it.

Re: select a face from an image.

Posted: Mon Mar 11, 2013 5:33 am
by applePi
in the Danilo example it works with more than one face
Image

Image
but there are extra third square in the second picture
thanks

Re: select a face from an image.

Posted: Mon Mar 11, 2013 6:35 am
by Danilo
applePi wrote:in the Danilo example it works with more than one face
Image

Image
but there are extra third square in the second picture
thanks
It is a false detection! Did you try to disable your antivirus? ;)

You need to play with the parameters of cvHaarDetectObjects() a little bit. Try setting min_neighbors (last arg for GetObjects) to 3 or 4,
I had set it to 2 in the example. Also, try with other haarcascades from OpenCV package, I just used the default ones.

The images are converted to 8-bit internally by default. Use the following flags for cvLoadImage() to change it:

Code: Select all

Image = cvLoadImage(ImageFileName,#CV_LOAD_IMAGE_COLOR|#CV_LOAD_IMAGE_ANYDEPTH)
Try also flag #CV_LOAD_IMAGE_GRAYSCALE. Everything is very flexible.

Read about the parameter tuning at: Seeing With OpenCV, Part 2: Finding Faces in Images - Parameters and Tuning

Don't use it for robotics with guns! A false detection and you are dead... or maybe kitty cat. :cry:

Re: select a face from an image.

Posted: Mon Mar 11, 2013 9:46 am
by Kiffi
Danilo wrote:It is a false detection!
here's another one:

Image

strange...

Re: select a face from an image.

Posted: Mon Mar 11, 2013 10:12 am
by Rings
Kiffi wrote:
Danilo wrote:It is a false detection!
here's another one:

Image

strange...
muaahhh, you made my day (monday for special)
thx...

Re: select a face from an image.

Posted: Mon Mar 11, 2013 12:27 pm
by dige
:lol:

Re: select a face from an image.

Posted: Mon Mar 11, 2013 6:46 pm
by Little John
Danilo wrote:
Little John wrote:Can we access the C++ interface with PureBasic?
Had some time, so I used the following code to translate to PureBasic: Seeing With OpenCV, Part 2: Finding Faces in Images

DOWNLOAD: OpenCV244_PB510_FaceDetect_Test_001.zip (3,39MB)
Hi Danilo,

I didn't intend to ask you to do this ... you did it anyway. :-)
Thank you very much!
Now it's time to play with it. :-)

@Kiffi: :lol: