Spaces:
Running
Running
Fix landmarks model issue
Browse files- src/face_proportions.py +20 -8
src/face_proportions.py
CHANGED
@@ -1,18 +1,30 @@
|
|
1 |
import dlib
|
2 |
-
import yaml
|
3 |
import cv2
|
4 |
import os
|
5 |
import numpy as np
|
6 |
import imutils
|
|
|
|
|
7 |
from src.cv_utils import get_image, resize_image_height
|
8 |
from typing import List, Union
|
9 |
from PIL import Image as PILImage
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
|
18 |
class GetFaceProportions:
|
@@ -27,9 +39,9 @@ class GetFaceProportions:
|
|
27 |
|
28 |
@staticmethod
|
29 |
def detect_face_landmarks(gray_image: np.array) -> List[Union[np.array, np.array]]:
|
30 |
-
|
31 |
detector = dlib.get_frontal_face_detector()
|
32 |
-
|
|
|
33 |
rects = detector(gray_image, 1)
|
34 |
for rect in rects:
|
35 |
shape = predictor(gray_image, rect)
|
|
|
1 |
import dlib
|
|
|
2 |
import cv2
|
3 |
import os
|
4 |
import numpy as np
|
5 |
import imutils
|
6 |
+
import urllib.request
|
7 |
+
import bz2
|
8 |
from src.cv_utils import get_image, resize_image_height
|
9 |
from typing import List, Union
|
10 |
from PIL import Image as PILImage
|
11 |
|
12 |
+
|
13 |
+
def download_shape_predictor_model() -> str:
|
14 |
+
model_url = "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2"
|
15 |
+
local_bz2_path = "models/shape_predictor_68_face_landmarks.dat.bz2"
|
16 |
+
local_dat_path = "models/shape_predictor_68_face_landmarks.dat"
|
17 |
+
|
18 |
+
if not os.path.exists(local_dat_path):
|
19 |
+
os.makedirs("models", exist_ok=True)
|
20 |
+
print("Downloading shape predictor model...")
|
21 |
+
urllib.request.urlretrieve(model_url, local_bz2_path)
|
22 |
+
|
23 |
+
with bz2.open(local_bz2_path, "rb") as f_in, open(local_dat_path, "wb") as f_out:
|
24 |
+
f_out.write(f_in.read())
|
25 |
+
print("Model extracted.")
|
26 |
+
|
27 |
+
return local_dat_path
|
28 |
|
29 |
|
30 |
class GetFaceProportions:
|
|
|
39 |
|
40 |
@staticmethod
|
41 |
def detect_face_landmarks(gray_image: np.array) -> List[Union[np.array, np.array]]:
|
|
|
42 |
detector = dlib.get_frontal_face_detector()
|
43 |
+
predictor_path = download_shape_predictor_model()
|
44 |
+
predictor = dlib.shape_predictor(predictor_path)
|
45 |
rects = detector(gray_image, 1)
|
46 |
for rect in rects:
|
47 |
shape = predictor(gray_image, rect)
|