Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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"
webcam = cv2.VideoCapture(0)
classifier = cv2.CascadeClassifier(TRAINSET)
if webcam.isOpened(): # try to get the first frame
rval, frame =
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 =
key = cv2.waitKey(20)
if key in [27, ord('Q'), ord('q')]: # exit on ESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment