<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Silveira Neto &#187; Paul Viola</title>
	<atom:link href="http://silveiraneto.net/tag/paul-viola/feed/" rel="self" type="application/rss+xml" />
	<link>http://silveiraneto.net</link>
	<description></description>
	<lastBuildDate>Fri, 09 Mar 2012 04:13:27 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Simple Face Detection Player</title>
		<link>http://silveiraneto.net/2009/12/01/simple-face-detection-player/</link>
		<comments>http://silveiraneto.net/2009/12/01/simple-face-detection-player/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 22:03:15 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[intel]]></category>
		<category><![CDATA[opencv]]></category>
		<category><![CDATA[Paul Viola]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Rainer Lienhar]]></category>
		<category><![CDATA[vision]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=2987</guid>
		<description><![CDATA[<a href="http://silveiraneto.net/2009/12/01/simple-face-detection-player/" title="Simple Face Detection Player"></a>Here&#8217;s a simple video player that also performs facial detection thought the Open Computer Vision Library. Here&#8217;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 &#8230;<p class="read-more"><a href="http://silveiraneto.net/2009/12/01/simple-face-detection-player/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://silveiraneto.net/2009/12/01/simple-face-detection-player/" title="Simple Face Detection Player"></a><p>Here&#8217;s a simple video player that also performs facial detection thought the <a title="OpenCV at Wikipedia" href="http://en.wikipedia.org/wiki/OpenCV">Open Computer Vision Library</a>.</p>
<p>Here&#8217;s a code developed using codes <a title="OpenCV Face Detection" href="http://nashruddin.com/OpenCV_Face_Detection">from nashruddin.com</a> and samples from <a title="OpenCV Project" href="http://sourceforge.net/projects/opencvlibrary/">OpenCV</a>, including the haar classifier xml. More detailed explanation on the theory about how the OpenCV face detection algorithm works can be found <a title="Face Detection on OpenCV" href="http://opencv.willowgarage.com/wiki/FaceDetection">here</a>.</p>
<p>The code:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;highgui.h&gt;</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;cv.h&gt;</span>
&nbsp;
CvHaarClassifierCascade <span style="color: #339933;">*</span>cascade<span style="color: #339933;">;</span>
CvMemStorage <span style="color: #339933;">*</span>storage<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    CvCapture <span style="color: #339933;">*</span>video <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    IplImage <span style="color: #339933;">*</span>frame <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> delay <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> key<span style="color: #339933;">,</span> i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>window_name <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Video&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>cascadefile <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;haarcascade_frontalface_alt.xml&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* check for video file passed by command line */</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>argc<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        video <span style="color: #339933;">=</span> cvCaptureFromFile<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Usage: %s VIDEO_FILE<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* check file was correctly opened */</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>video<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Unable to open <span style="color: #000099; font-weight: bold;">\&quot;</span>%s<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* load the classifier */</span>
    cascade <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> CvHaarClassifierCascade<span style="color: #339933;">*</span> <span style="color: #009900;">&#41;</span>cvLoad<span style="color: #009900;">&#40;</span> cascadefile<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>cascade<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Error loading the classifier.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* setup the memory buffer for the face detector */</span>
    storage <span style="color: #339933;">=</span> cvCreateMemStorage<span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>storage<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Error creating the memory storage.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* create a video window, auto size */</span>
    cvNamedWindow<span style="color: #009900;">&#40;</span>window_name<span style="color: #339933;">,</span> CV_WINDOW_AUTOSIZE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* get a frame. Necessary for use the cvGetCaptureProperty */</span>
    frame <span style="color: #339933;">=</span> cvQueryFrame<span style="color: #009900;">&#40;</span>video<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* calculate the delay between each frame and display video's FPS */</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%2.2f FPS<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> cvGetCaptureProperty<span style="color: #009900;">&#40;</span>video<span style="color: #339933;">,</span> CV_CAP_PROP_FPS<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    delay <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #339933;">/</span>cvGetCaptureProperty<span style="color: #009900;">&#40;</span>video<span style="color: #339933;">,</span> CV_CAP_PROP_FPS<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>frame<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/* show loaded frame */</span>
        cvShowImage<span style="color: #009900;">&#40;</span>window_name<span style="color: #339933;">,</span> frame<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* wait delay and check for the quit key */</span>
        key <span style="color: #339933;">=</span> cvWaitKey<span style="color: #009900;">&#40;</span>delay<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">==</span><span style="color: #ff0000;">'q'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
	<span style="color: #808080; font-style: italic;">/* load and check next frame*/</span>
        frame <span style="color: #339933;">=</span> cvQueryFrame<span style="color: #009900;">&#40;</span>video<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>frame<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;error loading frame.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">/* detect faces */</span>
        CvSeq <span style="color: #339933;">*</span>faces <span style="color: #339933;">=</span> cvHaarDetectObjects<span style="color: #009900;">&#40;</span>
            frame<span style="color: #339933;">,</span> <span style="color: #808080; font-style: italic;">/* image to detect objects in */</span>
            cascade<span style="color: #339933;">,</span> <span style="color: #808080; font-style: italic;">/* haar classifier cascade */</span>
            storage<span style="color: #339933;">,</span> <span style="color: #808080; font-style: italic;">/* resultant sequence of the object candidate rectangles */</span>
            <span style="color:#800080;">1.1</span><span style="color: #339933;">,</span> <span style="color: #808080; font-style: italic;">/* increse window by 10% between the subsequent scans*/</span>
            <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> <span style="color: #808080; font-style: italic;">/* 3 neighbors makes up an object */</span>
            <span style="color: #0000dd;">0</span> <span style="color: #808080; font-style: italic;">/* flags CV_HAAR_DO_CANNY_PRUNNING */</span><span style="color: #339933;">,</span>
            cvSize<span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">40</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">40</span> <span style="color: #009900;">&#41;</span> 
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">/* for each face found, draw a red box */</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #009900;">&#40;</span> faces <span style="color: #339933;">?</span> faces<span style="color: #339933;">-&gt;</span>total <span style="color: #339933;">:</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
             CvRect <span style="color: #339933;">*</span>r <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> CvRect<span style="color: #339933;">*</span> <span style="color: #009900;">&#41;</span>cvGetSeqElem<span style="color: #009900;">&#40;</span> faces<span style="color: #339933;">,</span> i <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
             cvRectangle<span style="color: #009900;">&#40;</span> frame<span style="color: #339933;">,</span>
                  cvPoint<span style="color: #009900;">&#40;</span> r<span style="color: #339933;">-&gt;</span>x<span style="color: #339933;">,</span> r<span style="color: #339933;">-&gt;</span>y <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                  cvPoint<span style="color: #009900;">&#40;</span> r<span style="color: #339933;">-&gt;</span>x <span style="color: #339933;">+</span> r<span style="color: #339933;">-&gt;</span>width<span style="color: #339933;">,</span> r<span style="color: #339933;">-&gt;</span>y <span style="color: #339933;">+</span> r<span style="color: #339933;">-&gt;</span>height <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                  CV_RGB<span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">255</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><small>Yeah, I know the code needs a few adjustments. ¬¬</small></p>
<p>To compile it in a well configured OpenCV development environment:</p>
<blockquote><p>gcc faceplayer.c  -o faceplayer `pkg-config opencv &#8209;&#8209;libs &#8209;&#8209;cflags`</p></blockquote>
<p>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:</p>
<blockquote><p>./faceplayer video.avi</p></blockquote>
<p>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.</p>
<p><center><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/es_O3c-Kc-Q&#038;hl=pt_BR&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/es_O3c-Kc-Q&#038;hl=pt_BR&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></center></p>
<p>A example of good result:</p>
<p style="text-align: center;"><img class="size-full wp-image-2988 aligncenter" title="rick roll face detection" src="http://silveiraneto.net/wp-content/uploads/2009/12/rickroll_face_detection.jpg" alt="rick roll face detection" width="400" height="240" /></p>
<p>A example of bad result:</p>
<p style="text-align: center;"><img src="http://silveiraneto.net/wp-content/uploads/2009/12/rickroll_face_detection_bad_result.jpg" alt="rick roll face detection bad result" title="rick roll face detection bad result" width="400" height="240" class="alignnone size-full wp-image-2991" /></p>
<p>Maybe with some adjustments it could performs even better. But was really easy to create it using OpenCV.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/12/01/simple-face-detection-player/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

