Forked from astanin/opencv_show_webcam_detect_face.py
Last active
August 29, 2015 14:07
-
-
Save Robotonics/919bcbf504ea233f885f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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