api.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from __future__ import print_function
  2. import os
  3. import torch
  4. from torch.utils.model_zoo import load_url
  5. from enum import Enum
  6. import numpy as np
  7. import cv2
  8. try:
  9. import urllib.request as request_file
  10. except BaseException:
  11. import urllib as request_file
  12. from .models import FAN, ResNetDepth
  13. from .utils import *
  14. class LandmarksType(Enum):
  15. """Enum class defining the type of landmarks to detect.
  16. ``_2D`` - the detected points ``(x,y)`` are detected in a 2D space and follow the visible contour of the face
  17. ``_2halfD`` - this points represent the projection of the 3D points into 3D
  18. ``_3D`` - detect the points ``(x,y,z)``` in a 3D space
  19. """
  20. _2D = 1
  21. _2halfD = 2
  22. _3D = 3
  23. class NetworkSize(Enum):
  24. # TINY = 1
  25. # SMALL = 2
  26. # MEDIUM = 3
  27. LARGE = 4
  28. def __new__(cls, value):
  29. member = object.__new__(cls)
  30. member._value_ = value
  31. return member
  32. def __int__(self):
  33. return self.value
  34. ROOT = os.path.dirname(os.path.abspath(__file__))
  35. class FaceAlignment:
  36. def __init__(self, landmarks_type, network_size=NetworkSize.LARGE,
  37. device='cuda', flip_input=False, face_detector='sfd', verbose=False):
  38. self.device = device
  39. self.flip_input = flip_input
  40. self.landmarks_type = landmarks_type
  41. self.verbose = verbose
  42. network_size = int(network_size)
  43. if 'cuda' in device:
  44. torch.backends.cudnn.benchmark = True
  45. # Get the face detector
  46. face_detector_module = __import__('face_detection.detection.' + face_detector,
  47. globals(), locals(), [face_detector], 0)
  48. self.face_detector = face_detector_module.FaceDetector(device=device, verbose=verbose)
  49. def get_detections_for_batch(self, images):
  50. images = images[..., ::-1]
  51. detected_faces = self.face_detector.detect_from_batch(images.copy())
  52. results = []
  53. for i, d in enumerate(detected_faces):
  54. if len(d) == 0:
  55. results.append(None)
  56. continue
  57. d = d[0]
  58. d = np.clip(d, 0, None)
  59. x1, y1, x2, y2 = map(int, d[:-1])
  60. results.append((x1, y1, x2, y2))
  61. return results