Created
March 7, 2018 22:04
-
-
Save abdul-rehman-2050/2841990209878d219deab62787952986 to your computer and use it in GitHub Desktop.
Face detector using Opencv Python version 2.4 and frontal face cascade features
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
import cv2 | |
import sys | |
video_capture = cv2.VideoCapture(0) | |
#video_capture.set(3,320) | |
#video_capture.set(4,240) | |
face_cascade = cv2.CascadeClassifier('haar_frontalface_alt2.xml') | |
if __name__ == "__main__": | |
try: | |
while(True): | |
# Capture frame-by-frame | |
ret, frame = video_capture.read() | |
if ret==True: | |
#just to remove mirror effect in camera | |
frame = cv2.flip(frame,2) | |
# Our operations on the frame come here | |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
faces = face_cascade.detectMultiScale( | |
gray, | |
scaleFactor=1.1, | |
minNeighbors=5, | |
minSize=(30, 30), | |
flags=cv2.cv.CV_HAAR_SCALE_IMAGE | |
) | |
# Draw a rectangle around the faces | |
for (x, y, w, h) in faces: | |
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), thickness=cv2.cv.CV_FILLED) | |
cv2.rectangle(frame, (x, y), (x+w+3, y+h+3), (0, 0, 0), 2) | |
# Display the resulting frame | |
cv2.imshow('frame',frame) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
else: | |
break | |
except KeyboardInterrupt as k: | |
sys.stderr.write("program will exit\nBye!\n") | |
except Exception, e: | |
sys.stderr.write(str(e) + "\n") | |
# When everything done, release the capture | |
video_capture.release() | |
cv2.destroyAllWindows() | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to use CAP_DSHOW?