Skip to content

Instantly share code, notes, and snippets.

@sanikamal
Created January 12, 2021 07:39
Show Gist options
  • Save sanikamal/3eb124aedf03203f8302ec521d857085 to your computer and use it in GitHub Desktop.
Save sanikamal/3eb124aedf03203f8302ec521d857085 to your computer and use it in GitHub Desktop.
Face Detection using OpenCV & Python
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade/haarcascade_frontalface_default.xml')
# Read the input image
img = cv2.imread('images/sanikamal.jpg')
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.3, 4)
# Draw rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2)
crop_face = img[y:y + h, x:x + w]
cv2.imwrite('faces/'+str(w) + str(h) + '_faces.jpg', crop_face)
# Display the output
cv2.imshow('img', img)
cv2.imshow("img_cropped",crop_face)
cv2.waitKey()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment