Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Detecting faces with SimpleCV
#!/usr/bin/python
import SimpleCV
import time
import simplejson as json
import sys
if __name__ == "__main__":
captureDir = '/opt/display/applications/tldisplayclient/static'
ofilename = '%s/webcam-original.png' % captureDir
dfilename = '%s/webcam-detected.png' % captureDir
matches = 0
green = (0, 255, 0)
sleeptime = 2
try:
cam = SimpleCV.Camera()
except:
print "Can't open webcam"
sys.exit()
while 1:
tstart = time.time()
try:
frame = cam.getImage()
frame.save(ofilename)
except:
print "Can't grab frame from webcam"
sys.exit()
facedetect = frame.findHaarFeatures('/usr/local/share/opencv/haarcascades/haarcascade_frontalface_alt.xml')
# Only count if we find a face
if facedetect:
# Count all the matches
for f in facedetect:
matches += 1
# Draw boxes around matches
facedetect.sortColorDistance(green)[0].draw(green)
frame.save(dfilename)
else:
# Otherwise, save the undetected image for comparison
frame.save(dfilename)
faces = {"faces": matches, "taken": time.time()}
tfinish = time.time()
faces['elapsed'] = tfinish - tstart
facesjson = json.dumps(faces)
f = open("/opt/display/applications/tldisplayclient/static/faces.json", 'w')
f.write(facesjson)
f.close()
matches = 0
time.sleep(sleeptime)
sys.exit()
@goldenbucks

This comment has been minimized.

Copy link

@goldenbucks goldenbucks commented May 8, 2015

Hi I have question regarding these lines:

captureDir = '/opt/display/applications/tldisplayclient/static' <- this directory
ofilename = '%s/webcam-original.png' % captureDir <- how about this one
dfilename = '%s/webcam-detected.png' % captureDir

f = open("/opt/display/applications/tldisplayclient/static/faces.json", 'w') <- what directory is this
f.write(facesjson)
f.close()
please do help me thank you

@gansai

This comment has been minimized.

Copy link

@gansai gansai commented Nov 28, 2015

captureDir is a variable to hold the path for directory.
ofilename is a variable to hold the path of file which points to original image, basically, instead of %s, captureDir's value will be placed.
f = open() -- this is a directory, where author wants to write output json values.

hope it cleared your doubts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment