Introduccion a Python – OpenCV
Publicado en Python por Arturo Elias Antón en 8 febrero 2009
Hola a todos hoy los traigo un codigo realizado por Arturo Elias que tiene un blog de programación excelente al final del post colocare el enlace. Vamos a reconocer el rostro de nuestros usuario y dentro de el los ojos y la boca y la nariz. Para esto en esta oportunidad vamos a utilizar una biblioteca llamada opencv.
Acá les dejo el código
#!/usr/bin/python
import pygame
import Image
from pygame.locals import *
import opencv
from opencv import adaptors
from opencv import highgui,cv
def sincroImg(image,scale):
image_size = cv.cvGetSize(image)
grayscale = cv.cvCreateImage(image_size, 8, 1)
image_small = cv.cvCreateImage(cv.cvSize(cv.cvRound(image.width/scale),cv.cvRound(image.height/scale)), 8, 1)
cv.cvCvtColor(image, grayscale, cv.CV_BGR2GRAY)
cv.cvResize(grayscale,image_small,cv.CV_INTER_LINEAR)
cv.cvEqualizeHist(grayscale, grayscale)
return grayscale
def dectect(img,cascade,storage,x,y,scale):
cv.cvClearMemStorage(storage)
return cv.cvHaarDetectObjects(
img,
cascade,
storage,
scale,
2,
0,#cv.CV_HAAR_DO_CANNY_PRUNING,
cv.cvSize(x, y))
camara = highgui.cvCreateCameraCapture(0)
fps = 30
pygame.init()
ventana = pygame.display.set_mode((320,240))
pygame.display.set_caption("OpenCV + Webcam Test")
screen = pygame.display.get_surface()
print "OpenCV version: %s (%d, %d, %d)" % (cv.CV_VERSION,
cv.CV_MAJOR_VERSION,
cv.CV_MINOR_VERSION,
cv.CV_SUBMINOR_VERSION)
cascade_name ='xml/haarcascade_frontalface_alt.xml'
cascade_name_nariz ='xml/nariz.xml'
cascade_name_ojos ='xml/ojos.xml'
cascade_name_boca ='xml/boca.xml'
storage = cv.cvCreateMemStorage(0)
cascade = cv.cvLoadHaarClassifierCascade(cascade_name, cv.cvSize(14,14))
cascade_nariz = cv.cvLoadHaarClassifierCascade(cascade_name_nariz, cv.cvSize(14,14))
cascade_ojos = cv.cvLoadHaarClassifierCascade(cascade_name_ojos, cv.cvSize(22,22))
cascade_boca = cv.cvLoadHaarClassifierCascade(cascade_name_boca, cv.cvSize(25,15))
scale = 2
inx=0
while True:
img1 = highgui.cvQueryFrame(camara)
# face detection
sincroImg(img1,scale)
faces = dectect(img1,cascade,storage,50,50,scale)
b_ojos = False
b_boca = False
b_nariz= False
if faces.total != 0:
face = faces[0]
cv.cvRectangle(img1,
cv.cvPoint( int(face.x), int(face.y)),
cv.cvPoint(int(face.x + face.width), int(face.y + face.height)),
cv.CV_RGB(255,0,0), 3, 8, 0)
face_x1 = int(face.x)
face_y1 = int(face.y)
face_x2 = int(face.x + face.width)
face_y2 = int(face.y + face.height)
ojos = dectect(img1,cascade_ojos,storage,20,20,scale)
for o in ojos:
if o.x > face_x1 and o.y > face_y1 and (o.x + o.width) < face_x2 and (o.y + o.height) < face_y2:
cv.cvRectangle(img1,
cv.cvPoint( int(o.x), int(o.y)),
cv.cvPoint(int(o.x + o.width), int(o.y + o.height)),
cv.CV_RGB(0,0,255), 3, 8, 0)
ojos_x1= o.x
ojos_y1= o.y
ojos_x2= o.x + o.width
ojos_y2= o.y + o.height
b_ojos=True;
bocas =[]
if b_ojos:bocas = dectect(img1,cascade_boca,storage,60,30,scale)
for b in bocas:
if b.x > ojos_x1 and b.y > ojos_y2 and (b.x + b.width) < ojos_x2 and (b.y + b.height) < face_y2:
cv.cvRectangle(img1,
cv.cvPoint( int(b.x), int(b.y)),
cv.cvPoint(int(b.x + b.width), int(b.y + b.height)),
cv.CV_RGB(0,255,0), 3, 8, 0)
boca_x1= b.x
boca_y1= b.y
boca_x2= b.x + b.width
boca_y2= b.y + b.height
b_boca= True
narices =[]
if b_boca:narices = dectect(img1,cascade_nariz,storage,40,40,scale)
for n in narices:
if n.x > ojos_x1 and n.y < boca_y1 and n.y >= ojos_y2 and n.x + n.width < ojos_x2 :
cv.cvRectangle(img1,
cv.cvPoint( int(n.x), int(n.y)),
cv.cvPoint(int(n.x + n.width), int(n.y + n.height)),
cv.CV_RGB(0,0,0), 3, 8, 0)
b_nariz = True
img = adaptors.Ipl2PIL(img1).resize((320,240))
if b_nariz:
img.save('img/img'+str(inx)+'.jpg')
inx+=1
pgimg = pygame.image.frombuffer(img.tostring(), img.size, img.mode)
screen.blit(pgimg, (0,0))
pygame.display.flip()
pygame.time.delay(int(1000 * 1.0/fps))
En la comunidad hay una archivo tar.gz con todos los xml necesarios y algunos otros para que prueben
Comunidad
Python R2