Update videoretalking/third_part/face_detection/api.py
Browse files
videoretalking/third_part/face_detection/api.py
CHANGED
|
@@ -1,79 +1,79 @@
|
|
| 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 |
-
|
| 13 |
-
from .models import FAN, ResNetDepth
|
| 14 |
-
from .utils import *
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
class LandmarksType(Enum):
|
| 18 |
-
"""Enum class defining the type of landmarks to detect.
|
| 19 |
-
|
| 20 |
-
``_2D`` - the detected points ``(x,y)`` are detected in a 2D space and follow the visible contour of the face
|
| 21 |
-
``_2halfD`` - this points represent the projection of the 3D points into 3D
|
| 22 |
-
``_3D`` - detect the points ``(x,y,z)``` in a 3D space
|
| 23 |
-
|
| 24 |
-
"""
|
| 25 |
-
_2D = 1
|
| 26 |
-
_2halfD = 2
|
| 27 |
-
_3D = 3
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
class NetworkSize(Enum):
|
| 31 |
-
# TINY = 1
|
| 32 |
-
# SMALL = 2
|
| 33 |
-
# MEDIUM = 3
|
| 34 |
-
LARGE = 4
|
| 35 |
-
|
| 36 |
-
def __new__(cls, value):
|
| 37 |
-
member = object.__new__(cls)
|
| 38 |
-
member._value_ = value
|
| 39 |
-
return member
|
| 40 |
-
|
| 41 |
-
def __int__(self):
|
| 42 |
-
return self.value
|
| 43 |
-
|
| 44 |
-
ROOT = os.path.dirname(os.path.abspath(__file__))
|
| 45 |
-
|
| 46 |
-
class FaceAlignment:
|
| 47 |
-
def __init__(self, landmarks_type, network_size=NetworkSize.LARGE,
|
| 48 |
-
device='cuda', flip_input=False, face_detector='sfd', verbose=False):
|
| 49 |
-
self.device = device
|
| 50 |
-
self.flip_input = flip_input
|
| 51 |
-
self.landmarks_type = landmarks_type
|
| 52 |
-
self.verbose = verbose
|
| 53 |
-
|
| 54 |
-
network_size = int(network_size)
|
| 55 |
-
|
| 56 |
-
if 'cuda' in device:
|
| 57 |
-
torch.backends.cudnn.benchmark = True
|
| 58 |
-
|
| 59 |
-
# Get the face detector
|
| 60 |
-
face_detector_module = __import__('face_detection.detection.' + face_detector,
|
| 61 |
-
globals(), locals(), [face_detector], 0)
|
| 62 |
-
self.face_detector = face_detector_module.FaceDetector(device=device, verbose=verbose)
|
| 63 |
-
|
| 64 |
-
def get_detections_for_batch(self, images):
|
| 65 |
-
images = images[..., ::-1]
|
| 66 |
-
detected_faces = self.face_detector.detect_from_batch(images.copy())
|
| 67 |
-
results = []
|
| 68 |
-
|
| 69 |
-
for i, d in enumerate(detected_faces):
|
| 70 |
-
if len(d) == 0:
|
| 71 |
-
results.append(None)
|
| 72 |
-
continue
|
| 73 |
-
d = d[0]
|
| 74 |
-
d = np.clip(d, 0, None)
|
| 75 |
-
|
| 76 |
-
x1, y1, x2, y2 = map(int, d[:-1])
|
| 77 |
-
results.append((x1, y1, x2, y2))
|
| 78 |
-
|
| 79 |
return results
|
|
|
|
| 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 |
+
|
| 13 |
+
from .models import FAN, ResNetDepth
|
| 14 |
+
from .utils import *
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class LandmarksType(Enum):
|
| 18 |
+
"""Enum class defining the type of landmarks to detect.
|
| 19 |
+
|
| 20 |
+
``_2D`` - the detected points ``(x,y)`` are detected in a 2D space and follow the visible contour of the face
|
| 21 |
+
``_2halfD`` - this points represent the projection of the 3D points into 3D
|
| 22 |
+
``_3D`` - detect the points ``(x,y,z)``` in a 3D space
|
| 23 |
+
|
| 24 |
+
"""
|
| 25 |
+
_2D = 1
|
| 26 |
+
_2halfD = 2
|
| 27 |
+
_3D = 3
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class NetworkSize(Enum):
|
| 31 |
+
# TINY = 1
|
| 32 |
+
# SMALL = 2
|
| 33 |
+
# MEDIUM = 3
|
| 34 |
+
LARGE = 4
|
| 35 |
+
|
| 36 |
+
def __new__(cls, value):
|
| 37 |
+
member = object.__new__(cls)
|
| 38 |
+
member._value_ = value
|
| 39 |
+
return member
|
| 40 |
+
|
| 41 |
+
def __int__(self):
|
| 42 |
+
return self.value
|
| 43 |
+
|
| 44 |
+
ROOT = os.path.dirname(os.path.abspath(__file__))
|
| 45 |
+
|
| 46 |
+
class FaceAlignment:
|
| 47 |
+
def __init__(self, landmarks_type, network_size=NetworkSize.LARGE,
|
| 48 |
+
device='cuda', flip_input=False, face_detector='sfd', verbose=False):
|
| 49 |
+
self.device = device
|
| 50 |
+
self.flip_input = flip_input
|
| 51 |
+
self.landmarks_type = landmarks_type
|
| 52 |
+
self.verbose = verbose
|
| 53 |
+
|
| 54 |
+
network_size = int(network_size)
|
| 55 |
+
|
| 56 |
+
if 'cuda' in device:
|
| 57 |
+
torch.backends.cudnn.benchmark = True
|
| 58 |
+
|
| 59 |
+
# Get the face detector
|
| 60 |
+
face_detector_module = __import__('videoretalking.third_part.face_detection.detection.' + face_detector,
|
| 61 |
+
globals(), locals(), [face_detector], 0)
|
| 62 |
+
self.face_detector = face_detector_module.FaceDetector(device=device, verbose=verbose)
|
| 63 |
+
|
| 64 |
+
def get_detections_for_batch(self, images):
|
| 65 |
+
images = images[..., ::-1]
|
| 66 |
+
detected_faces = self.face_detector.detect_from_batch(images.copy())
|
| 67 |
+
results = []
|
| 68 |
+
|
| 69 |
+
for i, d in enumerate(detected_faces):
|
| 70 |
+
if len(d) == 0:
|
| 71 |
+
results.append(None)
|
| 72 |
+
continue
|
| 73 |
+
d = d[0]
|
| 74 |
+
d = np.clip(d, 0, None)
|
| 75 |
+
|
| 76 |
+
x1, y1, x2, y2 = map(int, d[:-1])
|
| 77 |
+
results.append((x1, y1, x2, y2))
|
| 78 |
+
|
| 79 |
return results
|