Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Nächste Überarbeitung | Vorhergehende ÜberarbeitungLetzte ÜberarbeitungBeide Seiten der Revision | ||
face_detection:torch3vision [2009/05/26 10:30] – Externe Bearbeitung 127.0.0.1 | face_detection:torch3vision [2011/05/11 17:25] – gerald | ||
---|---|---|---|
Zeile 2: | Zeile 2: | ||
{{tag> | {{tag> | ||
+ | |||
+ | jpegtopnm is obsolete under debian. Use djpeg instead | ||
+ | < | ||
[[http:// | [[http:// | ||
+ | |||
+ | Quelle (grossteils) ((http:// | ||
+ | |||
+ | You will need the C++ Compiler ' | ||
+ | < | ||
+ | |||
+ | * Download Torch3Vision and un-tar it: tar -zxf Torch3vision2.1.tgz | ||
+ | * Build Torch3vision: | ||
+ | (Or on AMD 64: cp Linux_i686.cfg.vision2.1 Linux_x86_64.cfg | ||
+ | * ./ | ||
+ | * Build the vison examples for face detection: cd vision2.1/ | ||
+ | | ||
+ | |||
+ | |||
+ | Jetzt 3 Programme in neuen Unterverzeichnis '' | ||
+ | * haarscan - Schnelle, zuverlässige Erkennung | ||
+ | * mlpcascadescan - Sehr zuverlössige Erkennung, bei ganz kleinen Bildern geht es nicht richtig, grosse (> | ||
+ | * mlpscan - Seh keinen rechten Unterschied zum vorherigen. | ||
+ | |||
+ | Jedes dieser 3 Programme braucht eine Modeldatei zur Gesichtererkennung. Diese 3 Dateien liegen unter '' | ||
+ | |||
+ | |||
+ | So now we have a working set of face-detection programmes. The command line interface isn’t too friendly, so they take a little playing around. For starters, the binaries on my Ubuntu system don’t read JPEG images (although the code seems to be there, the build system is non-standard and didn’t automatically pick up my jpeg libraries. So, I needed to convert my images to PPM format, which is one of those image formats that no-one uses but somehow is the lowest common denominator for image processing command line apps. I use the program ‘jpegtopnm’ from package ‘netpbm’. | ||
+ | < | ||
+ | Of the three facial detection programs available, I found ‘mlpcascadescan’ to be the most effective and quickest, although they all have similar interfaces so this will basically be the same for all of them. We need to pass the source image and the model file, and we tell it to write the face position and to save a drawing with the face detected: | ||
+ | < | ||
+ | mlpcascadescan andy.ppm -savepos -draw \ | ||
+ | | ||
+ | </ | ||
+ | This command takes about 20s to run on my creaky old laptop, and creates two files. | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | Parmaters (as far as i found out): | ||
+ | * -minWsize n: min size n in pixel to recognize a face | ||
+ | * -nbest n : save the best n results only | ||
+ | * -dir < | ||
+ | * -savepos | ||
+ | * -draw -> draw ppm image with detections | ||
+ | * -savejpg | ||
+ | * -verbose: tell a little bit more what you doing... | ||
+ | |||
+ | The other file ‘andy.pos’ contains the results of face detection. Line one is the number of detections, then each line has format x y w h, very easy to parse. | ||
+ | < | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | I played around with the step-factors in the x and y directions to shave a second or so off the face detection routine, the values I chose were 0.1 and 0.2 respectively (I don’t need any accuracy in the y direction really, since my use is to cut the face down the middle). | ||
+ | |||
+ | Then, since these are portrait photographs, | ||
+ | < | ||
+ | WIDTH=`identify -format “%w” “andy.jpg”` | ||
+ | | ||
+ | </ | ||
+ | So now here’s the final face detection command | ||
+ | < | ||
+ | mlpcascadescan “$ppm” -dir /tmp/ -savepos -model $MODEL \ | ||
+ | | ||
+ | </ | ||
+ | And finally, as promised, I’ll tell you how to blank-out one side of the face: of course, using Image Magick. Using the ‘chop’ or ‘crop’ commands didn’t work for this purpose, where I wanted the image to keep it’s dimensions but have one half just be white. So I decided to draw a white rectangle over half of the picture. | ||
+ | < | ||
+ | convert -fill white \ | ||
+ | -draw “rectangle $FACE_CENTER, | ||
+ | | ||
+ | </ | ||
+ | |||
+ | Whole script: | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | |||
+ | . ~/ | ||
+ | |||
+ | DRAW=" | ||
+ | STEPX=" | ||
+ | STEPY=" | ||
+ | MODEL=" | ||
+ | |||
+ | # Find the center of the face in the image. Crop the image along the center of | ||
+ | # the face. | ||
+ | |||
+ | # Requirements: | ||
+ | which mlpcascadescan > /dev/null || \ | ||
+ | die " | ||
+ | |||
+ | [ -e " | ||
+ | |||
+ | FACE_CENTER="" | ||
+ | |||
+ | # Find the center of the face. Takes a jpeg and finds the face. | ||
+ | function find_center() { | ||
+ | jpeg=" | ||
+ | temp="/ | ||
+ | ppm=" | ||
+ | pos=" | ||
+ | echo " | ||
+ | jpegtopnm $jpeg > $ppm | ||
+ | if [ -e " | ||
+ | rm " | ||
+ | fi | ||
+ | HEIGHT=`identify -format " | ||
+ | WIDTH=`identify -format " | ||
+ | MIN_FACE_WIDTH=`echo $WIDTH / 6 | bc` | ||
+ | echo " | ||
+ | echo " | ||
+ | -minWsize $MIN_FACE_WIDTH -stepxfactor $STEPX -stepyfactor $STEPY" | ||
+ | mlpcascadescan " | ||
+ | -minWsize $MIN_FACE_WIDTH -stepxfactor $STEPX -stepyfactor $STEPY | ||
+ | [ -e " | ||
+ | FACE_POS=`head -n 2 " | ||
+ | FACE_X=`echo $FACE_POS | awk ' | ||
+ | FACE_W=`echo $FACE_POS | awk ' | ||
+ | FACE_CENTER=`echo $FACE_X + $FACE_W/2 | bc` | ||
+ | echo " | ||
+ | } | ||
+ | |||
+ | function crop_image() { | ||
+ | jpeg=" | ||
+ | ext=${jpeg## | ||
+ | base=`basename " | ||
+ | crop=" | ||
+ | HEIGHT=`identify -format " | ||
+ | WIDTH=`identify -format " | ||
+ | echo " | ||
+ | convert -fill white -draw " | ||
+ | } | ||
+ | |||
+ | |||
+ | for file in $@; do | ||
+ | echo " | ||
+ | find_center $file | ||
+ | crop_image $file | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ |