Skip to content

Instantly share code, notes, and snippets.

@astanin
Created July 12, 2012 12:36
Show Gist options
  • Save astanin/3097851 to your computer and use it in GitHub Desktop.
Save astanin/3097851 to your computer and use it in GitHub Desktop.
OpenCV example. Show webcam image and detect face.
#!/usr/bin/env python2
"""
OpenCV example. Show webcam image and detect face.
"""
import cv2
TRAINSET = "/usr/share/OpenCV/lbpcascades/lbpcascade_frontalface.xml"
DOWNSCALE = 4
webcam = cv2.VideoCapture(0)
cv2.namedWindow("preview")
classifier = cv2.CascadeClassifier(TRAINSET)
if webcam.isOpened(): # try to get the first frame
rval, frame = webcam.read()
else:
rval = False
while rval:
# detect faces and draw bounding boxes
minisize = (frame.shape[1]/DOWNSCALE,frame.shape[0]/DOWNSCALE)
miniframe = cv2.resize(frame, minisize)
faces = classifier.detectMultiScale(miniframe)
for f in faces:
x, y, w, h = [ v*DOWNSCALE for v in f ]
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255))
cv2.putText(frame, "Press ESC to close.", (5, 25),
cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255,255,255))
cv2.imshow("preview", frame)
# get next frame
rval, frame = webcam.read()
key = cv2.waitKey(20)
if key in [27, ord('Q'), ord('q')]: # exit on ESC
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment