Skip to content

Instantly share code, notes, and snippets.

@mattgorecki
Created July 21, 2011 02:14
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattgorecki/1096373 to your computer and use it in GitHub Desktop.
Save mattgorecki/1096373 to your computer and use it in GitHub Desktop.
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()
@gansai
Copy link

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