-
-
Save biranchi2018/6317b0433b18c4bed3cdade3c05446ab to your computer and use it in GitHub Desktop.
OpenCV example. Show webcam image and detect face.
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