Image tracking con opencv
- palichis's blog
- Inicie sesión o regístrese para enviar comentarios
- 179 lecturas
Continuando con mi investigación acerca de las librerías opencv, ahora les traigo otro ejemplo donde la cámara captura un rostro y esta es seguida por toda la zona de captura de la camara
import cv
from optparse import OptionParser
import math
image_scale = 5
def Track():
# sigue a la imagen encontrada
tpl = vector[0][1]
tm = vector[0][2]
h = vector[0][3]
w = vector[0][4]
cv.MatchTemplate(frame, tpl, tm, cv.CV_TM_SQDIFF_NORMED)
(minval, maxval, minloc, maxloc) = cv.MinMaxLoc(tm, None)
if minval < 0.2:
# Dibujar rectangulo.
cv.Rectangle( frame_vivo,( minloc[0]*image_scale, minloc[1]*image_scale ),( (minloc[0] + w)*image_scale,(minloc[1] + h)*image_scale),cv.Scalar( 0, 255, 255, 0 ), 2, 0, 0 )
else:
print "Objeto perdido.\n"
vector.insert(0,(0,2))
tracking = None
return
def detectFaces(img, vector):
# Deteccion de rostros por medio del detector.
faces = cv.HaarDetectObjects( img, cascade, storage, 1.1, 3, 0, (10, 10) )
if faces:
for ((x, y, w, h), n) in faces:
pt1 = (int(x * image_scale), int(y * image_scale))
pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
cv.Rectangle(frame_vivo, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
tracking = 1
else:
return
W = img.width - w + 1
H = img.height - h + 1
tm = cv.CreateImage((W, H), cv.IPL_DEPTH_32F, 1)
r = (x,y,w,h)
tpl = cv.CreateImage((w, h), img.depth, img.nChannels);
cv.SetImageROI(img,r)
cv.Copy(img,tpl,None)
cv.ResetImageROI(img);
if tracking:
vector.insert(0,(tracking,tpl, tm, h, w))
return
if __name__ == '__main__':
parser = OptionParser(usage = "usage: %prog [options] [filename|camera_index]")
parser.add_option("-c", "--cascade", action="store", dest="cascade", type="str", help="Haar cascade file, default %default", default = "../../data/haarcascades/haarcascade_frontalface_alt.xml")
(options, args) = parser.parse_args()
cascade = cv.Load(options.cascade)
if len(args) != 1:
parser.print_help()
sys.exit(1)
input_name = args[0]
if input_name.isdigit():
capture = cv.CreateCameraCapture(int(input_name))
else:
capture = cv.CaptureFromFile(input_name)
if capture:
storage = cv.CreateMemStorage(0)
# Ciclo Infinito para poder optener imagenes en tiempo real.
vector = []
tracking = 0
while True:
frame_vivo = cv.QueryFrame( capture ) # Obtenemos Frame desde el Buffer de Video
frame = cv.CreateImage((frame_vivo.width/image_scale,frame_vivo.height/image_scale),8, frame_vivo.nChannels)
cv.Resize(frame_vivo, frame, cv.CV_INTER_LINEAR)
if not frame: break # Si no Obtenemos Frame , Entonces Finalizamos el Ciclo
#print tracking
if vector:
#print vector
tracking = vector[0][0]
if tracking==1:
Track()
else:
vector = []
detectFaces(frame, vector)
cv.ShowImage( "Video", frame_vivo ) # Desplegamos la Imagen en Pantalla
if cv.WaitKey(10) >= 0: # Si se Preciono la Tecla ESC entonces se rompe el Ciclo
break
cv.DestroyWindow( "Video" )
al igual que el código anterior se necesita el entrenamiento haarcascade código
para ejecutar
$python training.py -c haarcascade 0
donde 0 es el indice de nuestra webcam, pudiendo tambien sustituir el 0 por un archivo de video

