Face recognition

Tech B 1 Tallied Votes 651 Views Share

This is a facial recognition system. Supports one face so far. This is an example ported from C++ to python. To see the original and to obtain the dll used visit this site.

I do not take credit of the dll, only the ported python code.

The comments in the code should be enough to see whats going on, if not or need more clarity let me know.

Requires PIL.

#fdtest.cpp mostly ported to python
#supports 1 face detection so far
#http://www.kyb.mpg.de/bs/people/kienzle/facedemo/facedemo.htm
#Tech B.
#3/9/2010

from ctypes import *
import Image, os

#add style and clear screen
os.system('color 02')
os.system('cls')

fd = cdll.LoadLibrary("fdlib.dll") #dll found at http://www.kyb.mpg.de/bs/people/kienzle/facedemo/facedemo.htm
im = Image.open(raw_input("Image to load: "))

#converts to grayscale if not already
if im.mode != 'L':
    im = im.convert("L")

#assignments
pixX = 0
pixY = 0
pixList = []

graydata = c_ubyte * (640*480)
graydata = graydata()

w = c_int(640)
h = c_int(480)
threshold = c_int(0)

x = c_int * 256
y = c_int * 256
size = c_int * 256
x = x()
y = y()
size = size()

#obtain list of pixels
#this is an expensive algarythem, takes too long to complete...
while pixX*pixY <= (640*480):
    pixList.append(im.getpixel((pixX,pixY)))
    pixX += 1
    if pixX == 640:
        pixX = 0
        pixY += 1
    if pixY == 480:
        break

#convert pixel list from above to ctype array
count = 0
while count < (640*480):
    graydata[count] = c_ubyte(pixList[count])
    count += 1

print "\nDetecting faces...\n"
fd.fdlib_detectfaces(byref(graydata), w, h, threshold)

n = fd.fdlib_getndetections()
if n == 1:
    print "%d face found\n" % n
    fd.fdlib_getdetection(c_int(0), x, y, size)
    print "X: %d Y: %d Size: %d" % (x[0], y[0], size[0])
Tech B 48 Posting Whiz in Training

This one is for Real Time detection. It could probably me optimized, but it works!!! This is the first cool thing I've done. It taught me a lot about ctypes, how to use it and how it works. I can't take credit for the actual detection because I am using a DLL from someone else.

This will detect 1 face at least 85% of the time. Other factor such as light, head position, and distance play a role.

Requires:

#Face Detection Real-Time
#supports 1 face detection so far
#http://www.kyb.mpg.de/bs/people/kienzle/facedemo/facedemo.htm
#Tech B.
#3/10/2010

from VideoCapture import Device
from ctypes import *
import Image, ImageDraw, os, time, pygame, sys
from pygame.locals import *
from psyco import full
full()

#add style and clear screen
os.system('color 02')

fd = cdll.LoadLibrary("fdlib.dll") #dll found at http://www.kyb.mpg.de/bs/people/kienzle/facedemo/facedemo.htm


#assignments
cam = Device()
pygame.init()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption('Facial Recognition')

pixX = 0
pixY = 0
pixList = []

w = c_int(640)
h = c_int(480)
threshold = c_int(0)

x = c_int * 256
y = c_int * 256
size = c_int * 256
x = x()
y = y()
size = size()


graydata = c_ubyte * (640*480)
graydata = graydata()
while 1:

    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()
    im = cam.getImage()
    draw = ImageDraw.Draw(im)
    img = im.convert("L")
    imd = list(img.getdata())
    cnt = 0
    pixX = 0
    pixY = 0
    cnt = 0
    for pix in imd:
        graydata[cnt] = imd[cnt]
        cnt+=1

    fd.fdlib_detectfaces(byref(graydata), w, h, threshold)
    n = fd.fdlib_getndetections()
    if n == 1:
        fd.fdlib_getdetection(c_int(0), x, y, size)

    bBoxTres = size[0]/2
    draw.rectangle([(x[0]+bBoxTres,y[0]+bBoxTres),(x[0]-bBoxTres,y[0]-bBoxTres)], outline=255)
    imn = pygame.image.frombuffer(im.tostring(), (640,480), "RGB")
    screen.blit(imn, (0,0))
    pygame.display.flip()
toofan0909 0 Newbie Poster

Hello Tech B,

Is ur program able to detect faces inside the pictures ? for me i am unable to detect any .

Regards
Toofan

Tech B 48 Posting Whiz in Training

Yes, the first piece of code picks up on 1 face inside a picture.
Does it give you a traceback, or just not pick up any face?
Make sure you have all the required modules and the dll from this site.

Head posistion needs to be frontal, and not too angled.
This was written in Python2.6 and uses PIL 1.1.6 I beleive.

It can now recognize multiple faces, it is real-time and requires a webcam.

#fdtest.cpp mostly ported to python
#Multi face
#Tech B.
#3/9/2010
#Version 1.5

from VideoCapture import Device
from ctypes import *
import Image, ImageDraw, os, time, pygame, sys
from pygame.locals import *
from psyco import full
full()

#add style and clear screen
os.system('color 02')

fd = cdll.LoadLibrary("fdlib.dll") #dll found at http://www.kyb.mpg.de/bs/people/kienzle/facedemo/facedemo.htm


#assignments
cam = Device()
pygame.init()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption('Facial Recognition')
font = pygame.font.SysFont("Curier",26)

pixX = 0
pixY = 0
pixList = []

w = c_int(640)
h = c_int(480)
threshold = c_int(0)

x = c_int * 256
y = c_int * 256
size = c_int * 256
x = x()
y = y()
size = size()


graydata = c_ubyte * (640*480)
graydata = graydata()
fps = 25.0
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()
    im = cam.getImage()
    draw = ImageDraw.Draw(im)
    img = im.convert("L") #convert to grayscale
    imd = list(img.getdata()) #graydata needed
    cnt = 0
    pixX = 0
    pixY = 0
    cnt = 0
    for pix in imd: #Convert python data types to ctypes
        graydata[cnt] = imd[cnt]
        cnt+=1

    fd.fdlib_detectfaces(byref(graydata), w, h, threshold)
    n = fd.fdlib_getndetections() #number of faces
    i = 0
    while i < n:
        fd.fdlib_getdetection(c_int(i), x, y, size)
        bBoxTres = size[0]/2
        draw.rectangle([(x[0]+bBoxTres,y[0]+bBoxTres),(x[0]-bBoxTres,y[0]-bBoxTres)], outline=224)
        i += 1
    faceNumber = font.render('Number of Faces: '+str(n), True, (46,224,1))
    imn = pygame.image.frombuffer(im.tostring(), (640,480), "RGB")
    screen.blit(imn, (0,0))
    screen.blit(faceNumber,(0,0))
    pygame.display.flip()
    pygame.time.delay(int(1000 * 1.0/fps))
toofan0909 0 Newbie Poster
Tech B 48 Posting Whiz in Training

I see the problem. I'm reading from 640x480 image.

Okay I thought it was the problem, I couldn't get it to pick it up, I did however try the aplication that came with the dll, and it couldn't pick it up either. Try a defrent picture.

And as for the image size thing, the new code has im.size from the Image module.
example:

import Image

im = Image.open('test.jpg')

imSize = im.size

xSize = imSize[0]
ySize = imSize[1]
toofan0909 0 Newbie Poster
#http://www.kyb.mpg.de/bs/people/kienzle/facedemo/facedemo.htm
#Tech B.
#3/9/2010
#modified by toofan0909
#30/March/2010

from ctypes import *
import Image, os , ImageDraw

#add style and clear screen
os.system('color 02')
os.system('cls')

fd = cdll.LoadLibrary("fdlib.dll") #dll found at http://www.kyb.mpg.de/bs/people/kienzle/facedemo/facedemo.htm

infile = raw_input("Image to load: ")
outfile = os.path.splitext(infile)[0] + ".face.jpg"

im = Image.open(infile)
im_base=im


#converts to grayscale if not already 
if im.mode != 'L':
    im = im.convert("L")
imSize = im.size 
xSize = imSize[0]
ySize = imSize[1]

#assignments
pixX = 0
pixY = 0
pixList = []

graydata = c_ubyte * (xSize*ySize)
graydata = graydata()

w = c_int(xSize)
h = c_int(ySize)
threshold = c_int(0)

x = c_int * 256
y = c_int * 256
size = c_int * 256
x = x()
y = y()
size = size()

#obtain list of pixels
#this is an expensive algarythem, takes too long to complete...
print"\nStarting to load the image in memory for processing\n" 

while pixX*pixY <= (xSize*ySize):
    pixList.append(im.getpixel((pixX,pixY)))
    pixX += 1
    if pixX == xSize:
        pixX = 0
        pixY += 1
    if pixY == ySize:
        break

#convert pixel list from above to ctype array
count = 0
while count < (xSize*ySize):
    graydata[count] = c_ubyte(pixList[count])
    count += 1

print "\nDetecting faces...\n"
fd.fdlib_detectfaces(byref(graydata), w, h, threshold)

n = fd.fdlib_getndetections()
draw = ImageDraw.Draw(im_base) # Create a draw object

i = 0    
while i < n:        
    fd.fdlib_getdetection(c_int(i), x, y, size)
    print "X: %d Y: %d Size: %d" % (x[0], y[0], size[0])
    bBoxTres = size[0]/2
    draw.rectangle([(x[0]+bBoxTres,y[0]+bBoxTres),(x[0]-bBoxTres,y[0]-bBoxTres)], outline=224)
    i=i+1

if n > 0 :
    print "\nfaces found in the given input file"
    print "Saving file ",outfile
    im_base.save(outfile, "JPEG")
else:
    print "No faces found in the given input file"


print "\nEnd of program"
Tech B 48 Posting Whiz in Training

Yeah, that's about the way I updated it. I went ahead and also updated the pixel algorithm
from:

while pixX*pixY <= (xSize*ySize):
    pixList.append(im.getpixel((pixX,pixY)))
    pixX += 1
    if pixX == xSize:
        pixX = 0
        pixY += 1
    if pixY == ySize:
        break

To this:

for pix in imd:
        graydata[cnt] = imd[cnt]
        cnt+=1

Its a lot quicker, and less expensive.

Alveen Scofield 0 Newbie Poster

Halo! Are u able to convert VB 6 to VB 2008 (.Net)? Can u help me to convert a program about face recognition?

Tech B 48 Posting Whiz in Training

I'm sorry, I don't know VB yet. I'm taking it next semister; school doesn't start till aug 23.

It shouldn't be hard if you know how to code in VB. Just figure out how to call functions in the dll.

I'll work on it when I start class as a side project, if your willing to wait that long.

Alveen Scofield 0 Newbie Poster

I’ts OK. Thanx 4 reply. But I can’t wait that long. I’m doing my final thesis about face recognition in absence system using webcam. Can u help me using other language? Coz I don’t know Python….

Member Avatar for Tstrv
Tstrv

Hey gays,
I am new here :)
I want to build face recognition program with neural network. I have 40 different people with 10 different pictures for each of them. The size of each image is 92x112 pixels, with 256 grey levels per pixel. I do not know how to get the gray level for each of the pixels. I need it in dictionary like this {(pixX,pixY):greyLevel}. Need help. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.