Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Amir22010/38e224ba71823c4b54c78789c6b61e7c to your computer and use it in GitHub Desktop.
Save Amir22010/38e224ba71823c4b54c78789c6b61e7c to your computer and use it in GitHub Desktop.
mtcnn face detection code
import cv2
from mtcnn.mtcnn import MTCNN
detector = MTCNN()
image = cv2.imread("street.jpg")
result = detector.detect_faces(image)
# Result is an array with all the bounding boxes detected.
bounding_box = result[0]['box']
keypoints = result[0]['keypoints']
cv2.rectangle(image,
(bounding_box[0], bounding_box[1]),
(bounding_box[0]+bounding_box[2], bounding_box[1] + bounding_box[3]),
(0,155,255),
2)
cv2.circle(image,(keypoints['left_eye']), 2, (0,155,255), 2)
cv2.circle(image,(keypoints['right_eye']), 2, (0,155,255), 2)
cv2.circle(image,(keypoints['nose']), 2, (0,155,255), 2)
cv2.circle(image,(keypoints['mouth_left']), 2, (0,155,255), 2)
cv2.circle(image,(keypoints['mouth_right']), 2, (0,155,255), 2)
cv2.imwrite("detect.jpg", image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment