Wednesday, October 14, 2009

sample of the opencv 2's python interface || example d'utilisation de l'interface python d'opencv 2

English:
I recently was playing with opencv and had a hard time compiling the c code samples (apparently my C/C++ knowledge is outdated) so i decided to take a look at the python samples (that i am way more familiar with). But i quickly realized that the code sample and the new interface don't match (T_T). So after hours of ready this page  and getting crazy on non-exsitent types (ndlr cvSize, cvPoint), i realize that i wasn't reading the most import part of the previous link that say that simple type such as cvSize etc.. are so simple that we can use tuple !!!
Knowing that, i was then able to rewrite the facedect.py (code at the end of the article).

Conclusion: Everything is always in the documentation, we just need to read it !

P.S. : Does anyone know if there is a code plugin for blogspot ?


Francais:
Recement je fasais mumuse avec opencv et rencontrais des difficultes a compiler le code c (visiblement mes connaissance en C/C++ sont depassees). J'ai ansi decide de me jeter sur le code python (que je connais la bien mieux). Cependant, je me suis vite rendu compte que les codes fournis et la nouvelle interface ne colle pas. Donc apres des heures a lire cette page et a m'arrache les cheveux sur des types non existant (cf. cvSize, cvPoint etc..), j'ai remarque que je louper le passage le plus important de l'article sus-cite: les type simple comme cvSize sont maintenant remplace par des tuples !
Etant maintenat au clair avec j'ai pu reecrire la source facedetect.py.

Moralite: Tout est dans la doc, faut juste la lire !

P.S. : vous savez pas s'il existe un plugin code pour blogspot ?





import sys
# i kept it this was to highlight the function from the module but in a real life application
# use form cv import *
import cv
# Global Variables
cascade = None;
storage = cv.CreateMemStorage(0);
cascade_name = "../../data/haarcascades/haarcascade_frontalface_alt.xml";
input_name = "../c/lena.jpg";

# Parameters for haar detection
# From the API:
# The default parameters (scale_factor=1.1, min_neighbors=3, flags=0) are tuned 
# for accurate yet slow object detection. For a faster operation on real video 
# images the settings are: 
# scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING,
# min_size=
min_size = (20, 20)
image_scale = 1.3
haar_scale = 1.2
min_neighbors = 2
haar_flags = 0

def detect_and_draw(img):
    gray = cv.CreateImage((img.width, img.height), 8, 1);
    small_img = cv.CreateImage((cv.Round (img.width / image_scale), cv.Round (img.height / image_scale)), 8, 1);
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY);
    cv.Resize(gray, small_img, cv.CV_INTER_LINEAR);
    cv.EqualizeHist(small_img, small_img);
    if(cascade):
        faces = cv.HaarDetectObjects(small_img, cascade, storage, haar_scale, min_neighbors, 0);
        if faces:
            for (x, y, w, h), n in faces:
                print "coord (%d,%d) and height:%d width:%d" % (x * image_scale, y * image_scale, w * image_scale, h * image_scale);
        else:
            print "no face found";

if __name__ == '__main__':
    if len(sys.argv) > 1:
        if sys.argv[1].startswith("--cascade="):
            cascade_name = sys.argv[1][ len("--cascade="): ]
            if len(sys.argv) > 2:
                input_name = sys.argv[2]
        elif sys.argv[1] == "--help" or sys.argv[1] == "-h":
            print "Usage: facedetect --cascade='' [filename|camera_index]" ;
            sys.exit(-1)
        else:
            input_name = sys.argv[1]
    cascade = cv.Load(cascade_name);
    if not cascade:
        print "ERROR: Could not load classifier cascade"
        sys.exit(-1)
    image = cv.LoadImage(input_name);
    if(image):
        detect_and_draw(image);


No comments: