Created
February 16, 2021 13:27
-
-
Save j-adamczyk/ef1dd37f2431be62d1e59668585590d0 to your computer and use it in GitHub Desktop.
Face detection using MTCNN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
from facenet_pytorch.models.mtcnn import MTCNN | |
import numpy as np | |
import torch | |
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') | |
mtcnn = MTCNN(keep_all=True, device=device) | |
def mtcnn_detect(img: np.ndarray) -> np.ndarray: | |
boxes, probs = mtcnn.detect(img) | |
for box in boxes: | |
x_left = min(box[0], box[2]) | |
x_right = max(box[0], box[2]) | |
y_left = min(box[1], box[3]) | |
y_right = max(box[1], box[3]) | |
img = cv2.rectangle(img, (x_left, y_left), (x_right, y_right), | |
(255, 0, 0), 2) | |
return img |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment