This project is belongs to the original creator (https://github.com/khrlimam/mtcnn-pytorch). I just fixed some problems that may have been caused by newer version of PyTorch.
Install the package with pip:
pip install git https://github.com/cardinalblue/mtcnn-pytorch.git
pip install git ssh://[email protected]/cardinalblue/mtcnn-pytorch.git
from torch_mtcnn import detect_faces
from PIL import Image
image = Image.open('image.jpg')
bounding_boxes, landmarks = detect_faces(image)
You can use the bounding_boxes
to crop the image (assuming there is only 1 face):
bounding_boxes = list(map(int, bounding_boxes[0]))
img_1 = cv2.imread(path)
img_1[ bounding_boxes[1] : bounding_boxes[3], bounding_boxes[0] : bounding_boxes[2]]
I have included a utility function get_faces
. This function gets the image path and returns all of the faces in the image:
from torch_mtcnn import get_faces
faces = get_faces('img.jpg')
Please see the requirements.txt
The original project belongs to https://github.com/khrlimam/mtcnn-pytorch.