Skip to content

Instantly share code, notes, and snippets.

@karthick965938
Created November 20, 2019 06:10
Show Gist options
  • Save karthick965938/d984e9783d208525dfbd59a2b9b102cc to your computer and use it in GitHub Desktop.
Save karthick965938/d984e9783d208525dfbd59a2b9b102cc to your computer and use it in GitHub Desktop.
Open cv python face detection
import cv2
# Load the cascade
#https://github.com/karthick965938/Face-detection/blob/master/haarcascades/haarcascade_frontalface_default.xml
face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')
# To capture video from webcam.
cap = cv2.VideoCapture(0)
# To use a video file as input
while True:
# Read the frame
_, img = cap.read()
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw the rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Display
cv2.imshow('img', img)
# Stop if escape key is pressed
k = cv2.waitKey(30) & 0xff
if k==27:
break
# Release the VideoCapture object
cap.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment