Skip to content

Instantly share code, notes, and snippets.

@tonmoay
Created November 11, 2016 07:00
Show Gist options
  • Save tonmoay/fa871e801359a7760caf169c6f955ec6 to your computer and use it in GitHub Desktop.
Save tonmoay/fa871e801359a7760caf169c6f955ec6 to your computer and use it in GitHub Desktop.
Basic Face Detection Using OpenCV
import cv2
#import numpy as np
import time
#import finished
cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
'''
References:
http://docs.opencv.org/trunk/d7/d8b/tutorial_py_face_detection.html
http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_objdetect/py_face_detection/py_face_detection.html
http://docs.opencv.org/3.1.0/dd/d43/tutorial_py_video_display.html
https://realpython.com/blog/python/face-recognition-with-python/
https://realpython.com/blog/python/face-detection-in-python-using-a-webcam/
'''
#Start of video record
cap = cv2.VideoCapture(0)
time.sleep(5)
while(True):
#Capture frame-by frame
ret, frame = cap.read()
#if ret==0:
# continue
#Image Operation Starts here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#detects faces
face = cascade.detectMultiScale(gray,1.1,5)
#Draw the detected faces
for (x, y, w, h) in face:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 96, 23), 1)
#Display the frame
cv2.imshow('Webcam',frame)
#loop break control
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#Release the capture after finish
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment