Skip to content

Instantly share code, notes, and snippets.

@j-adamczyk
Created February 16, 2021 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j-adamczyk/ef1dd37f2431be62d1e59668585590d0 to your computer and use it in GitHub Desktop.
Save j-adamczyk/ef1dd37f2431be62d1e59668585590d0 to your computer and use it in GitHub Desktop.
Face detection using MTCNN
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