Skip to content

Tag: intel

OpenCV on Ubuntu

digital_eye

Open Computer Vision Library or just OpenCV, is a cross-platform computer vision library focused on real-time image processing for video files or webcams.

You have two options to obtain the environment to develop on OpenCV. You can insert a new repository in your package manager or compile it by yourself.

For Ubuntu 9.10 Karmic Koala there’s this repository with OpenCV’s package.

To compile it you have to install some additional libraries compile it by your self. And it’s instructions vary for each distribution and version. For example, from Ubuntu Linux 9.10 to 9.04, the process varies slightly. I followed the instructions on this post “Installing OpenCV 2.0 on Ubuntu 9.10 Karmic Koala”.

After you have installed and have a well configured OpenCV development environment, you can compile a “source.c” file into a “program” binary like this:

gcc gcc source.c -o program `pkg-config opencv ‑‑libs ‑‑cflags`

Simple Face Detection Player

Here’s a simple video player that also performs facial detection thought the Open Computer Vision Library.

Here’s a code developed using codes from nashruddin.com and samples from OpenCV, including the haar classifier xml. More detailed explanation on the theory about how the OpenCV face detection algorithm works can be found here.

The code:

#include 
#include 
#include 

CvHaarClassifierCascade *cascade;
CvMemStorage *storage;

int main(int argc, char *argv[]) {
    CvCapture *video = NULL;
    IplImage *frame = NULL;
    int delay = 0, key, i=0;
    char *window_name = "Video";
    char *cascadefile = "haarcascade_frontalface_alt.xml";

    /* check for video file passed by command line */
    if (argc>1) {
        video = cvCaptureFromFile(argv[1]);
    }
    else {
        printf("Usage: %s VIDEO_FILE\n", argv[0]);
        return 1;
    }

    /* check file was correctly opened */
    if (!video) {
        printf("Unable to open \"%s\"\n", argv[1]);
        return 1;
    }

    /* load the classifier */
    cascade = ( CvHaarClassifierCascade* )cvLoad( cascadefile, 0, 0, 0 );
    if(!cascade){
        printf("Error loading the classifier.");
	return 1;
    }

    /* setup the memory buffer for the face detector */
    storage = cvCreateMemStorage( 0 );
    if(!storage){
        printf("Error creating the memory storage.");
	return 1;
    }

    /* create a video window, auto size */
    cvNamedWindow(window_name, CV_WINDOW_AUTOSIZE);

    /* get a frame. Necessary for use the cvGetCaptureProperty */
    frame = cvQueryFrame(video);

    /* calculate the delay between each frame and display video's FPS */
    printf("%2.2f FPS\n", cvGetCaptureProperty(video, CV_CAP_PROP_FPS));
    delay = (int) (1000/cvGetCaptureProperty(video, CV_CAP_PROP_FPS));

    while (frame) {
	/* show loaded frame */
        cvShowImage(window_name, frame);
	
	/* wait delay and check for the quit key */
        key = cvWaitKey(delay);
        if(key=='q') break;
	/* load and check next frame*/
        frame = cvQueryFrame(video);
	if(!frame) {
		printf("error loading frame.\n");
		return 1;
	}

        /* detect faces */
        CvSeq *faces = cvHaarDetectObjects(
            frame, /* image to detect objects in */
            cascade, /* haar classifier cascade */
            storage, /* resultant sequence of the object candidate rectangles */
            1.1, /* increse window by 10% between the subsequent scans*/
            3, /* 3 neighbors makes up an object */
            0 /* flags CV_HAAR_DO_CANNY_PRUNNING */,
            cvSize( 40, 40 ) 
        );

        /* for each face found, draw a red box */
        for( i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ ) {
             CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );
             cvRectangle( frame,
                  cvPoint( r->x, r->y ),
                  cvPoint( r->x + r->width, r->y + r->height ),
                  CV_RGB( 255, 0, 0 ), 1, 8, 0 );
        }
    }
}

Yeah, I know the code needs a few adjustments. ¬¬

To compile it in a well configured OpenCV development environment:

gcc faceplayer.c -o faceplayer `pkg-config opencv ‑‑libs ‑‑cflags`

To run it you have to put in the same directory of the binary the XML classifier (haarcascade_frontalface_alt.xml) that comes with OpenCV sources at OpenCV-2.0.0/data/haarcascades/. And so:

./faceplayer video.avi

The results I got so far is that it works well for faces but sometimes its also detects more than faces. And here a video of it working live.

A example of good result:

rick roll face detection

A example of bad result:

rick roll face detection bad result

Maybe with some adjustments it could performs even better. But was really easy to create it using OpenCV.

Ubuntu Jaunty: má performance gráfica

ubuntu glass logo

Nas versões anteriores do Ubuntu, inclusive no 8.04 que eu utilizava anteriormente, não havia do que me queixar. Porém com a mudança para o 9.04 (Jaunty Jackalope) de imediato foi possível reparar uma queda drástica na performance da aceleração gráfica 3D.

Minha placa gráfica no meu notebook, um Amazon FL31.

silveira@fl31:~$ lspci -nn | grep VGA
00:02.0 VGA compatible controller [0300]: Intel Corporation Mobile 945GM/GMS, 943/940GML Express Integrated Graphics Controller [8086:27a2] (rev 03)

Na release desta versão do Ubuntu havia um aviso de possível regreção na performance em placas de vídeo da Intel onde há a indicação de algumas soluções provisórias.

Regressão do Driver

Eu experimentei uma solução mais drástica que foi alternar o driver de vídeo para a versão anterior. O desempenho melhorou bastante, mas ainda senti uma boa diferença para os padrões que eu estou habituado. Além disso continuava impossível rodar alguns jogos, as texturas eram carregadas nos lugares errados e poucos quadros por segundo. Eu acabei por retornar os drivers para o default do Jaunty e descartar essa opção.

Atualização do Driver

Uma solução alternativa que eu me deparei foi atualizar os drivers para uma versão mais nova. Como root execute os comandos abaixo para adicionar os repositórios a lista do APT, adicionar suas chaves e atualizar as informações de pacote e instalar os novos drivers:

echo “deb http://ppa.launchpad.net/xorg-edgers/ppa/ubuntu jaunty main” >> /etc/apt/sources.list.d/intel.list

sudo apt-key adv –recv-keys –keyserver keyserver.ubuntu.com 165d673674a995b3e64bf0cf4f191a5a8844c542

aptitude update && aptitude safe-upgrade

A performance ainda não está tão boa quanto já foi mas está completamente suave e usável.