Spaces:
Runtime error
Runtime error
Create helpers.py
Browse files- helpers.py +85 -0
helpers.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from ultralyticsplus import YOLO
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
from tensorflow.keras.models import Model, Sequential
|
5 |
+
from tensorflow.keras.layers import (
|
6 |
+
Convolution2D,
|
7 |
+
LocallyConnected2D,
|
8 |
+
MaxPooling2D,
|
9 |
+
Flatten,
|
10 |
+
Dense,
|
11 |
+
Dropout,
|
12 |
+
)
|
13 |
+
import os
|
14 |
+
import zipfile
|
15 |
+
import gdown
|
16 |
+
import tensorflow as tf
|
17 |
+
|
18 |
+
|
19 |
+
def load_detector():
|
20 |
+
# load model
|
21 |
+
model = YOLO('https://github.com/akanametov/yolov8-face/releases/download/v0.0.0/yolov8n-face.pt')
|
22 |
+
|
23 |
+
# set model parameters
|
24 |
+
model.overrides['conf'] = 0.25 # NMS confidence threshold
|
25 |
+
model.overrides['iou'] = 0.45 # NMS IoU threshold
|
26 |
+
model.overrides['agnostic_nms'] = False # NMS class-agnostic
|
27 |
+
model.overrides['max_det'] = 50 # maximum number of detections per image
|
28 |
+
return model
|
29 |
+
|
30 |
+
def extract_faces(model, image):
|
31 |
+
# perform inference
|
32 |
+
results = model.predict(image)
|
33 |
+
ids = np.array(results[0].boxes.xyxy).astype(np.int32)
|
34 |
+
img = Image.open(image)
|
35 |
+
crops = []
|
36 |
+
for id in ids:
|
37 |
+
crops.append(Image.fromarray(np.array(img)[id[1] : id[3], id[0]: id[2]]))
|
38 |
+
return crops
|
39 |
+
|
40 |
+
def load_model(
|
41 |
+
url="https://github.com/swghosh/DeepFace/releases/download/weights-vggface2-2d-aligned/VGGFace2_DeepFace_weights_val-0.9034.h5.zip",
|
42 |
+
):
|
43 |
+
base_model = Sequential()
|
44 |
+
base_model.add(
|
45 |
+
Convolution2D(32, (11, 11), activation="relu", name="C1", input_shape=(152, 152, 3))
|
46 |
+
)
|
47 |
+
base_model.add(MaxPooling2D(pool_size=3, strides=2, padding="same", name="M2"))
|
48 |
+
base_model.add(Convolution2D(16, (9, 9), activation="relu", name="C3"))
|
49 |
+
base_model.add(LocallyConnected2D(16, (9, 9), activation="relu", name="L4"))
|
50 |
+
base_model.add(LocallyConnected2D(16, (7, 7), strides=2, activation="relu", name="L5"))
|
51 |
+
base_model.add(LocallyConnected2D(16, (5, 5), activation="relu", name="L6"))
|
52 |
+
base_model.add(Flatten(name="F0"))
|
53 |
+
base_model.add(Dense(4096, activation="relu", name="F7"))
|
54 |
+
base_model.add(Dropout(rate=0.5, name="D0"))
|
55 |
+
base_model.add(Dense(8631, activation="softmax", name="F8"))
|
56 |
+
|
57 |
+
# ---------------------------------
|
58 |
+
|
59 |
+
home = os.getcwd()
|
60 |
+
|
61 |
+
if os.path.isfile(home + "/VGGFace2_DeepFace_weights_val-0.9034.h5") != True:
|
62 |
+
print("VGGFace2_DeepFace_weights_val-0.9034.h5 will be downloaded...")
|
63 |
+
|
64 |
+
output = home + "/VGGFace2_DeepFace_weights_val-0.9034.h5.zip"
|
65 |
+
|
66 |
+
gdown.download(url, output, quiet=False)
|
67 |
+
|
68 |
+
# unzip VGGFace2_DeepFace_weights_val-0.9034.h5.zip
|
69 |
+
with zipfile.ZipFile(output, "r") as zip_ref:
|
70 |
+
zip_ref.extractall(home)
|
71 |
+
|
72 |
+
base_model.load_weights(home + "/VGGFace2_DeepFace_weights_val-0.9034.h5")
|
73 |
+
|
74 |
+
# drop F8 and D0. F7 is the representation layer.
|
75 |
+
deepface_model = Model(inputs=base_model.layers[0].input, outputs=base_model.layers[-3].output)
|
76 |
+
|
77 |
+
return deepface_model
|
78 |
+
|
79 |
+
def get_embeddings(model, imgs):
|
80 |
+
embeddings = []
|
81 |
+
for img in imgs:
|
82 |
+
img = np.expand_dims(np.array(img.resize((152,152))), axis = 0)
|
83 |
+
embedding = model.predict(img, verbose=0)[0]
|
84 |
+
embeddings.append(embedding)
|
85 |
+
return embeddings
|