'How Can I Detect and Crop A Face By Its Contour Precisely?
I've been committed in cropping faces by contour of 1000 human face picture. (shown below). I've googled it, firstly attempted using opencv in python. Neither the cascade-classifier nor the contour detector can satisfy my need since I want to detect and crop the face not in a rectangle, but in the natural contour of the head.
I do this for establishing a training set for succeeding input in VGG-face, thus I don want any background information but only human faces. I've posted my open (using dlib with 81 critical points) below. I'm curious that is there any other tools \ algorithms \ packages that allows me to detect and crop face the way I need?
I'd be appreciate it if you reply.
p.s. my python code:
#-*- coding: utf-8 -*-
import dlib
import numpy as np
import cv2
from matplotlib import pyplot as plt
def faceCrop(predictor_path, img_path):
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
img = cv2.imread(img_path, 1)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rects = detector(img_gray, 0)
for i in range(len(rects)):
landmarks = np.matrix([[p.x, p.y] for p in predictor(img, rects[i]).parts()])
for idx, point in enumerate(landmarks):
# Print 81 critical point
pos = (point[0, 0], point[0, 1])
# Dot on each critial point
cv2.circle(img, pos, 1, color = (255, 0, 0))
font = cv2.FONT_HERSHEY_SIMPLEX
# cv2.putText(img, str(idx + 1), pos, font, 0.3, (0, 0, 255), 1, cv2.LINE_AA)
print("index=" + str(idx+1) + " x=" + str(pos[0]) + " y=" + str(pos[1]))
cv2.imshow('img',img)
cv2.waitKey()
# plt.show()
img_path = '/Users/georgezhao/Desktop/PKU/挑战杯/Pictures/210_姜永明.png'
predictor_path = '/Users/georgezhao/Desktop/PKU/挑战杯/code/2-切脸/2_shape_predictor_81_face_landmarks-master/shape_predictor_81_face_landmarks.dat'
faceCrop(predictor_path=predictor_path, img_path=img_path)
And unfortunately I got this. Please check the output here(have no reputation yet)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
