Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Through the Interface

October 1 0, 201 2

Creating a face-recognising security cam with a Raspberry Pi Part 4


This is the final part of the series on creating a face-recognising security cam. We started by showing how to get motion detection working, and then followed with an initial ov erv iew and then posts on the separate Facebookdownloader tool and the onboard face detection component. In this post, well see how we managed to connect up a USB-powered LED message-board from DreamCheeky . I was originally inspired to use this dev ice to present the results of the Facecam while hav ing dinner at a friends place. Hes a fellow geek ev en his welcome mat say s so so we Googled around for a solution and came up with the DreamCheeky , mainly because someone had already created a Linux driv er for it. The dev ice is USB-powered and Im thankfully able to power both this and the Logitech webcam directly from the Raspberry Pi: I dont need to resort to a powered USB hub, which is sometimes needed if y our webcam needs more juice than the av erage. Here are the steps I followed to install and build the necessary components on the Raspberry Pi:
#C r e a t eo u rm a i nf o l d e r c d m k d i rl e d c dl e d #G e tt h el i b h i ds o u r c ea n db u i l di t w g e t" h t t p : / / a l i o t h . d e b i a n . o r g / f r s / d o w n l o a d . p h p / 1 9 5 8 / l i b h i d 0 . 2 . 1 6 . t a r . g z " t a rx z f* . t a r . g z r m* . g z c dl i b * . / c o n f i g u r e& &m a k e s u d om a k ei n s t a l l #W en e e das y m b o l i cl i n kt ot h el i b h i do u t p u tf o rl a t e r s u d ol ns/ u s r / l o c a l / l i b / l i b h i d . s o . 0/ u s r / l i b / l i b h i d . s o . 0 s u d ol d c o n f i g #G e tt h es o u r c ec o d ef o rt h ed c l e dc o m p o n e n t w g e t" h t t p : / / w w w . l a s t o u t p o s t . c o m / ~ m a l a k a i / d c l e d / d c l e d 2 . 0 . t g z " t a rx z f* . t g z r m* . t g z c dd c * #M a k et h ed c l e dc o m p o n e n t s u d op a c m a nSm a k el i b u s b m a k e s u d om a k ei n s t a l l

Im not 1 00% happy with the abov e process: for home use its OK, but as the required library , libhid is licensed under the GPL license there are serious limitations on how y ou might want to release this as We realise that this is a serious impediment. The GPL is a "viral" licence, and you w ill only be able to use libhid in other GPL projects. We w ould like to change the licence, but libhid uses the MGE UPS SY STEMS

HID Parser, w hich is GPL, and thus w e cannot. Our solution is to rew rite the HID parser. One of these days. We are also in contact w ith MGE, trying to convince them to loosen their licence. If the licencing issues are solved, w e are likely to re-release libhid under the Artistic Licence. That said, there is apparently hope that itll be possible at some point to swap this module out for an alternativ e. Once the needed components are built, it should be a simple matter of calling the dcled command to test whether it display s tex t properly or not. Here are the usage instructions for dcled:
[ p i @ a l a r m p i~ ] $d c l e dh e l p U s a g e -d c l e d[ o p t s ][ f i l e s ] b r i g h t n e s s c l o c k c l o c k 2 4 h b c d c l o c k d e b u g e c h o h e l p m e s s a g e n o d e v p r e a m b l e r e p e a t f a s t p r i n t s p e e d t e s t f o n t f o n t d i r b c C B d e h m n p r f s t g G H o wb r i g h t ,0 2 S h o wt h et i m e S h o wt h e2 4 ht i m e S h o wt h et i m ei nb i n a r y M o s t l yu s e l e s s S e n dc o p yt os t d o u t S h o wt h i sm e s s a g e As i n g l el i n em e s s a g et os c r o l l D o n ' tu s et h ed e v i c e S e n dag r a p h i cb e f o r et h et e x t . K e e ps c r o l l i n gf o r e v e r J u m pt oe n do fm e s s a g e . G e n e r a ld e l a yi nm s O u t p u tat e s tp a t t e r n S e l e c taf o n t S e l e c taf o n td i r e c t o r y

A v a i l a b l ep r e a m b l eg r a p h i c s : 1-d o t s 2-s t a t i c 3-s q u i g g l e 4-c l o c k 2 4 5-c l o c k 6-s p i r a l 7-f i r e 8-b c d c l o c k O p t i o n a lf o n t s : 1-s m a l l -V e r ys m a l lc h a r a c t e r s 2-s g a -S t a n d a r dg a l a c t i ca l p h a b e t 3-s m a l l _ i n v -V e r ys m a l li n v e r t e dc h a r a c t e r s -As t r i n go fr a n d o md o t s -W a r m su pl i k ea no l dT V -As q u i g g l yl i n e -S h o w st h e2 4h o u rt i m e -S h o w st h et i m e -D r a w sas p i r a l -An i c ew a r mh e a r t h -S h o w st h et i m ei nb i n a r y

Y ou can easily test the unit, then, by using this command to send a repeatedly scrolling message to the screen:
s u d od c l e dr" T h i si sat e s t "

If y ou see the following message, then y ouv e probably forgotten to run as root v ia sudo:
h i d _ f o r c e _ o p e nf a i l e dw i t hr e t u r nc o d e6 C o u l d n ' tf i n dt h ed e v i c e . W a se x p e c t i n gt of i n dar e a d a b l e d e v i c et h a tm a t c h e dv e n d o r1 d 3 4a n dp r o d u c t1 3 . I st h e d e v i c ep l u g g e di n ?D oy o uh a v ep e r m i s s i o n ?

Y ou can also use the lsusb command to check whether the dev ice has been recognised by the Pi. Now we hav e the driv er working, its a fairly simple matter to create another daemon to look for files in a certain folder and then send the messages they contain to the message-board before deleting them. Well use the "-p 7 option to cause the message to be posted with a flame-like pre- & post-amble. Heres the C/C++ code I used to implement this:

# i n c l u d e< s y s / t y p e s . h > # i n c l u d e< s y s / s t a t . h > # i n c l u d e< s t d i o . h > # i n c l u d e< s t d l i b . h > # i n c l u d e< f c n t l . h > # i n c l u d e< e r r n o . h > # i n c l u d e< u n i s t d . h > # i n c l u d e< s y s l o g . h > # i n c l u d e< d i r e n t . h > # i n c l u d e< s t r i n g . h > # i n c l u d e< s t r i n g > # i n c l u d e< v e c t o r > # i n c l u d e< i o s t r e a m > # i n c l u d e< f s t r e a m > # i n c l u d e< a l g o r i t h m > # i n c l u d e< s y s l o g . h > u s i n gn a m e s p a c es t d ; / /I n p u ta n do u t p u tf o l d e rl o c a t i o n s c o n s tc h a r*i n D i r=" / h o m e / p i / f a c e s / o u t " ; / /G e tt h el i s to ff i l e si nad i r e c t o r y i n tg e t d i r ( s t r i n gd i r ,v e c t o r < s t r i n g >& f i l e s ) { D I R* d p ; s t r u c td i r e n t* d i r p ; i f ( ( d p=o p e n d i r ( d i r . c _ s t r ( ) ) )= =N U L L ) { c h a rm s g [ 2 0 0 ] ; s n p r i n t f ( m s g , s i z e o f ( m s g ) 1 , " E r r o r ( % d )o p e n i n g% s " , e r r n o , d i r . c _ s t r ( ) ) ; s y s l o g ( L O G _ I N F O ,m s g ) ; r e t u r ne r r n o ; } w h i l e( ( d i r p=r e a d d i r ( d p ) )! =N U L L ) { f i l e s . p u s h _ b a c k ( s t r i n g ( d i r p > d _ n a m e ) ) ; } c l o s e d i r ( d p ) ; s o r t ( f i l e s . b e g i n ( ) ,f i l e s . e n d ( ) ) ; r e t u r n0 ; } i n tm a i n ( v o i d ) { / /O u rp r o c e s sI Da n dS e s s i o nI D

p i d _ tp i d ,s i d ; / /F o r ko f ft h ep a r e n tp r o c e s s p i d=f o r k ( ) ; i f( p i d<0 ) { e x i t ( E X I T _ F A I L U R E ) ; } / /I fw eg o tag o o dP I D ,t h e nw ec a ne x i tt h ep a r e n tp r o c e s s i f( p i d>0 ) { e x i t ( E X I T _ S U C C E S S ) ; } / /C h a n g et h ef i l em o d em a s k u m a s k ( 0 ) ; / /O p e na n yl o g sh e r e o p e n l o g ( " l e d m s g d " ,L O G _ P I D | L O G _ C O N S ,L O G _ U S E R ) ; / /C r e a t ean e wS I Df o rt h ec h i l dp r o c e s s s i d=s e t s i d ( ) ; i f( s i d<0 ) { / /L o gt h ef a i l u r e s y s l o g ( L O G _ I N F O ," U n a b l et og e tS I D . " ) ; c l o s e l o g ( ) ; e x i t ( E X I T _ F A I L U R E ) ; } / /C h a n g et h ec u r r e n tw o r k i n gd i r e c t o r y i f( c h d i r ( " / " )<0 ) { / /L o gt h ef a i l u r e s y s l o g ( L O G _ I N F O ," U n a b l et oc h a n g ew o r k i n gd i r e c t o r y . " ) ; c l o s e l o g ( ) ; e x i t ( E X I T _ F A I L U R E ) ; } / /C l o s eo u tt h es t a n d a r df i l ed e s c r i p t o r s c l o s e ( S T D I N _ F I L E N O ) ; c l o s e ( S T D O U T _ F I L E N O ) ; c l o s e ( S T D E R R _ F I L E N O ) ; / /D a e m o n s p e c i f i ci n i t i a l i z a t i o ng o e sh e r e s t r u c ts t a ts t={ 0 } ; i f( s t a t ( i n D i r ,& s t )= =1 )

{ m k d i r ( i n D i r ,0 7 0 0 ) ; } v e c t o r < s t r i n g >f i l e s=v e c t o r < s t r i n g > ( ) ; c o n s tc h a r* c o n t e n t s=N U L L ; / *T h eB i gL o o p* / s y s l o g ( L O G _ I N F O ," m a i nl o o pb e g i n s " ) ; w h i l e( 1 ) { / /G e tt h ef i l e si no u r" i n "d i r e c t o r y f i l e s . c l e a r ( ) ; g e t d i r ( i n D i r ,f i l e s ) ; i f( ( i n t ) f i l e s . s i z e ( )> =3 ) c o n t e n t s=f i l e s [ 2 ] . c _ s t r ( ) ; e l s e c o n t e n t s=N U L L ; i f( c o n t e n t s! =N U L L ) { s y s l o g ( L O G _ I N F O ," f o u n daf i l e " ) ; s y s l o g ( L O G _ I N F O ,c o n t e n t s ) ; c h a ri n p u t [ 2 5 6 ] ; i n p u t [ 0 ]=0 ; s t r c a t ( i n p u t ,i n D i r ) ; s t r c a t ( i n p u t ," / " ) ; s t r c a t ( i n p u t ,c o n t e n t s ) ; c h a rc m d [ 2 0 0 ] ; s n p r i n t f ( c m d , s i z e o f ( c m d ) 1 , " d c l e dp7% s " , i n p u t ) ; s y s t e m ( c m d ) ; r e m o v e ( i n p u t ) ; } s l e e p ( 0 . 5 ) ;/ *w a i th a l fas e c o n d* / } c l o s e l o g ( ) ; e x i t ( E X I T _ S U C C E S S ) ; }

The simplest way to get this source onto the dev ice is to wget it from this blog. Then its a simple matter of building it (again, apologies for the lack of makefile):
w g e t" h t t p : / / t h r o u g h t h e i n t e r f a c e . t y p e p a d . c o m / f i l e s / L e d M s g D a e m o n . c p p " g + +L e d M s g D a e m o n . c p pol e d m s g d

Once built y ou should copy & paste the ex ecutable to the appropriate folder:
c pl e d m s g d/ e t c / r c . d

And edit the last line of the /etc/rc.conf file to make sure ledmsgd gets launched along with facerecd on boot:
D A E M O N S = ( ! h w c l o c ks y s l o g n gn e t w o r ko p e n n t p d@ n e t f s@ c r o n d@ s s h d@ m o t i o nl e d m s g df a c e r e c d )

That should be it for getting the last of our components in place. At this stage, y ou should now hav e an at least partially functional security webcam, assuming y ouv e been able to train and copy across a face database in the form of a facedata.xml file. For fun, heres a quick test of my own dev ice at my homes front door. I hadnt realised that the human ey e (and brain) manages to create the effect of scrolling tex t from an LED message-board better than when not filtered through a v ideo recording (ev en when in HD), but then I suppose that sav es me the trouble of attempting to protect the innocent (i.e. my Facebook friends, if y ou can call them that. ;-)

I looked at the debug images that were stored in the ~/faces/debug folder, and saw that in general my face was getting detected appropriately , ev en if the recognition process clearly still needs tweaking to reduce (eliminate?) the false positiv es:

Part of the issue clearly stems that we dont look the same when captured v ia a security camera (especially when recording a v ideo about the ex perience :-) as we do when we get tagged in photos on Facebook. Its v ery possible the issue goes deeper than that, but Im going to leav e my inv estigations there, for now.

Posted at 04:26 PM in Raspberry Pi | Permalink Com m ents

Stu dijo... Hi Kean I'm try ing to get the DreamCheeky LED message board working from the Pi as y ou hav e, just this bit. Did y ou hav e any issues getting LibHid working? I am seeing an awful lot of problems with it on the Raspberry Pi Raspbian distro. For starters it requires LibUsb 0.1 and possibly LibUsb-dev . I hav e both of these components installed v ia aptget, but it's still complaining with this error during make root@raspberry pi:~/kost-dcled-c81 9fec/libhid-0.2.1 6# make make all-recursiv e make[1 ]: Entering directory `/root/kost-dcled-c81 9fec/libhid-0.2.1 6' Making all in hidparser make[2]: Entering directory `/root/kost-dcled-c81 9fec/libhid-0.2.1 6/hidparser' make[2]: Nothing to be done for `all'. make[2]: Leav ing directory `/root/kost-dcled-c81 9fec/libhid-0.2.1 6/hidparser' Making all in src make[2]: Entering directory `/root/kost-dcled-c81 9fec/libhid-0.2.1 6/src' make[2]: Leav ing directory `/root/kost-dcled-c81 9fec/libhid-0.2.1 6/src' Making all in test make[2]: Entering directory `/root/kost-dcled-c81 9fec/libhid-0.2.1 6/test' make[3]: Entering directory `/root/kost-dcled-c81 9fec/libhid-0.2.1 6/src' make[3]: Leav ing directory `/root/kost-dcled-c81 9fec/libhid-0.2.1 6/src' if gcc -DHAV E_CONFIG_H -I. -I. -I.. -DNDEBUG -I../include -I../hidparser -O2 -Wall -W -Werror -MT lshid.o -MD -MP -MF ".deps/lshid.Tpo" -c -o lshid.o lshid.c; \ then mv -f ".deps/lshid.Tpo" ".deps/lshid.Po"; else rm -f ".deps/lshid.Tpo"; ex it 1 ; fi lshid.c: In function dev ice_iterator: lshid.c:32:87 : error: param eter len set but not used [-Werror=unused-but-set-param eter] cc1 : all warnings being treated as errors make[2]: *** [lshid.o] Error 1 make[2]: Leav ing directory `/root/kost-dcled-c81 9fec/libhid-0.2.1 6/test' make[1 ]: *** [all-recursiv e] Error 1 make[1 ]: Leav ing directory `/root/kost-dcled-c81 9fec/libhid-0.2.1 6' make: *** [all] Error 2 Any help would be appreciated. Thanks Responder Nov ember 03, 201 2 at 03:23 PM

Kean Walmsley dijo en respuesta a Stu... Hi Stu, I didn't hit this particular issue, but then I'v e only built it on Arch Linux ARM, not on Raspbian.

Good luck with fix ing it, Kean Responder Nov ember 05, 201 2 at 1 0:00 AM

faust dijo en respuesta a Stu... i know it's been a while since y ou posted, but if y ou edit lshid.c in y our libhid/test folder, around line 41 , comment out the len = * bit, and add those under: len = len; custom = custom ; and it should compile fine. im hav ing lots of errors too, but i did manage to make libhid... dcled doesnt want to compile though.. Responder January 01 , 201 3 at 07 :47 AM

Thomas Schwaiger dijo... This is cool stuff! I dare y ou hook it up to y our door locking mechanism, so it only unlocks for y ou and y our family ! :) Responder December 1 7 , 201 2 at 08:1 0 PM

Kean Walmsley dijo en respuesta a Thomas Schwaiger... The tech isn't quite there y et, but may be one day ... :-) Kean Responder December 1 7 , 201 2 at 08:1 2 PM Comment below or sign in with Ty pePad Facebook Twitter and more...

You might also like