python


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
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()

Related Links

How to read a simple binary file
How to run mysql in Django 1.5.5 with Python 3.3.4 in Windows 7
How can I do maps involving string fields in rethinkdb?
Scrollbar not functioning properly Tkinter
Is there a way to quickly create many files many sequential files (ex1 . . . ex 50) in Python?
Pygame playing sound with pygame.mixer.music.load(file) gives NoneType error
How to update multiple values in Mongodb using pymongo?
Manually return an error result and status failure for a celery task?
Python backtrace from C++ embedded in Python
Find nth smallest element in numpy array [duplicate]
How to extract array from the first element of triples in 2d array of triples using numpy
Simulate user clicking in QSystemTrayIcon
Parsing GeoRSS feed with python scripting
User interface optimization when using pack
Selenium doesn't return after starting Chrome but returns after starting Firefox?
Python JSON decoding error TypeError: can't use a string pattern on a bytes-like object

Categories

HOME
symfony
tinyos
npm
itext
amazon-swf
mapping
raspbian
webrtc
facebook-graph-api
qpython3
phpstorm
console
aruco
metronic
jboss-eap-7
cakephp-2.9
nuxt.js
maxima
event-log
uisplitviewcontroller
titan
scenebuilder
phpbb
google-openid
jsonserializer
aws-cognito
url.action
onsen-ui
yosys
switching
branch
percentage
read-write
ifstream
qtablewidget
openpgp
text-files
autocompletetextview
agent
socketscan
onbackpressed
datalog
rkt
email-parsing
jtds
recycle-bin
taskmanager
persistent
dwarf
crash-reports
fedora20
ingres
system.web.ui.webcontrols
cfeclipse
alter
backstop.js
audioqueue
gameanalytics
linkageerror
achievements
django-south
lemoon
jta
geodjango
optionbutton
sem
mptcp
ffserver
kallithea
candidate-key
livechat
em
node-imagemagick
power-law
sgml
factors
baucis
java.util.date
google-closure-library
iconv
xslkey
returnurl
objective-c-protocol
nintendo
emacs23
electronic-signature
serp
charts4j
sendfile
pivotal-crm
hadoop-plugins
file-encodings
graniteds
winsnmp
odbc-sql-server-driver

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App