Skip to content

Instantly share code, notes, and snippets.

@santhalakshminarayana
Created October 16, 2019 15:55
Show Gist options
  • Save santhalakshminarayana/058cbedb9a603c8a97843e4708ff61b0 to your computer and use it in GitHub Desktop.
Save santhalakshminarayana/058cbedb9a603c8a97843e4708ff61b0 to your computer and use it in GitHub Desktop.
Face recognition detect faces in image
import cv2
import dlib
# Load cnn_face_detector with 'mmod_face_detector'
dnnFaceDetector=dlib.cnn_face_detection_model_v1("mmod_human_face_detector.dat")
# Load image
img=cv2.imread('path_to_image')
# Convert to gray scale
gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find faces in image
rects=dnnFaceDetector(gray,1)
left,top,right,bottom=0,0,0,0
# For each face 'rect' provides face location in image as pixel loaction
for (i,rect) in enumerate(rects):
left=rect.rect.left() #x1
top=rect.rect.top() #y1
right=rect.rect.right() #x2
bottom=rect.rect.bottom() #y2
width=right-left
height=bottom-top
# Crop image
img_crop=img[top:top+height,left:left+width]
#save crop image with person name as image name
cv2.imwrite('path_to_image_as_person_name',img_crop)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment