John Schnatter John Schnatter - 3 months ago 30
Python Question

How to make a specific type of python face tracker

Recently I have been working on a face tracker using python 3 and opencv and my laptops webcam, I've made it so that a window will pop up and display the webcam feed when you run the .pyw script, and I've mad a python script that detects a face on a frozen image with haarcascades, but I cant seem to combine the two,
so here's the webcam program;

import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False

while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
vc.release()
cv2.destroyWindow("preview")


Here is the face detect script, which I have In a folder with the haar xml file and a jpg, I have created a desktop shortcut to the script which runs it with the picture and the frontal face xml upon a double click of the shortcut.

import cv2
import sys
import time

imagePath = sys.argv[1]
cascPath = sys.argv[2]

faceCascade = cv2.CascadeClassifier(cascPath)

image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

faces = faceCascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize=(30, 30))#,flags = cv2.cv.CV_HAAR_IMAGE_SCALE)

print("Found {0} faces!".format(len(faces)))


for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)


cv2.imshow("Faces found" ,image)
cv2.waitKey(0)&0xFF


Remember all I need to do is make a live face tracker using the first block of code, also note I am a beginner, so please explain - Thanks!

Oh yeah, combined they look like;

import cv2
import sys
import time

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

imagePath = sys.argv[1]
cascPath = sys.argv[2]

faceCascade = cv2.CascadeClassifier(cascPath)

image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

faces = faceCascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize=(30, 30))#,flags = cv2.cv.CV_HAAR_IMAGE_SCALE)

for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False

while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
vc.release()
cv2.destroyWindow("preview")


My errors are as follows,

Traceback (most recent call last):
File "C:\Users\tcwissemann\Desktop\WebDetect.pyw", line 8, in
imagePath = sys.argv[1]
IndexError: list index out of range

Answer

Never mind, I already fixed it.

import cv2
import numpy as np

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier('haarcascade_smile.xml')

vc = cv2.VideoCapture(0)

while -1: 
    ret, img = vc.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,171),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,188,255),2)

        smiling = smile_cascade.detectMultiScale(roi_gray, 1.7, 5)
        for (sx,sy,sw,sh) in smiling:
            cv2.rectangle(roi_color,(sx,sy),(sx+sw,sy+sh),(240,19,93),2)

    cv2.imshow('WebDetect',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        vc.release()
        cv2.destroyAllWindows()