Skip to content

Commit c5a2dc7

Browse files
authored
Merge pull request #1257 from dkurt:tflite_face_blendshape_model
Test TFLite Face Blendshape V2 model #1257
1 parent f422f9f commit c5a2dc7

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

testdata/dnn/download_models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,11 @@ def is_archive(self):
12271227
],
12281228
sha='682e59855466f88eb0cab9d40ca16e9fd6303bea',
12291229
filename='../intel/person-detection-retail-0013/FP32/person-detection-retail-0013.bin'),
1230+
Model(
1231+
name='MediaPipe Blendshape V2 (TFLite)',
1232+
url='https://storage.googleapis.com/mediapipe-assets/face_blendshapes.tflite?generation=1677787708051579',
1233+
sha='eaf27df74abb6e112f3edbd7b06eb3d464fd02cc',
1234+
filename='tflite/face_blendshapes.tflite'),
12301235
]
12311236

12321237
# Note: models will be downloaded to current working directory
1.27 KB
Binary file not shown.
Binary file not shown.

testdata/dnn/tflite/generate.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
image = cv.imread(os.path.join(testdata, "cv", "shared", "lena.png"))
1212
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
1313

14-
def run_tflite_model(model_name, inp_size):
14+
def run_tflite_model(model_name, inp_size=None, inp=None):
1515
interpreter = tf.lite.Interpreter(model_name + ".tflite",
1616
experimental_preserve_all_tensors=True)
1717
interpreter.allocate_tensors()
@@ -21,9 +21,10 @@ def run_tflite_model(model_name, inp_size):
2121
output_details = interpreter.get_output_details()
2222

2323
# Run model
24-
inp = cv.resize(image, inp_size)
25-
inp = np.expand_dims(inp, 0)
26-
inp = inp.astype(np.float32) / 255 # NHWC
24+
if inp is None:
25+
inp = cv.resize(image, inp_size)
26+
inp = np.expand_dims(inp, 0)
27+
inp = inp.astype(np.float32) / 255 # NHWC
2728

2829
interpreter.set_tensor(input_details[0]['index'], inp)
2930

@@ -32,7 +33,7 @@ def run_tflite_model(model_name, inp_size):
3233
for details in output_details:
3334
out = interpreter.get_tensor(details['index']) # Or use an intermediate layer index
3435
out_name = details['name']
35-
np.save(f"{model_name}_out_{out_name}.npy", out)
36+
np.save(f"{model_name}_out_{out_name.replace(":", "_")}.npy", out)
3637

3738

3839
def run_mediapipe_solution(solution, inp_size):
@@ -44,6 +45,23 @@ def run_mediapipe_solution(solution, inp_size):
4445
run_tflite_model("face_landmark", (192, 192))
4546
run_tflite_model("face_detection_short_range", (128, 128))
4647

48+
# Download from https://storage.googleapis.com/mediapipe-assets/facemesh2_lite_iris_faceflag_2023_02_14.tflite?generation=1681322470818178
49+
# run_tflite_model("facemesh2_lite_iris_faceflag_2023_02_14", (192, 192))
50+
51+
# source: https://storage.googleapis.com/mediapipe-assets/Model%20Card%20Blendshape%20V2.pdf
52+
face_blendshapes_inp = np.load("facemesh2_lite_iris_faceflag_2023_02_14_out_StatefulPartitionedCall:1.npy").reshape(-1, 3)
53+
face_blendshapes_inp = face_blendshapes_inp[[
54+
0, 1, 4, 5, 6, 7, 8, 10, 13, 14, 17, 21, 33, 37, 39, 40, 46, 52, 53, 54, 55, 58, 61, 63, 65, 66, 67, 70, 78, 80,
55+
81, 82, 84, 87, 88, 91, 93, 95, 103, 105, 107, 109, 127, 132, 133, 136, 144, 145, 146, 148, 149, 150, 152, 153, 154, 155, 157,
56+
158, 159, 160, 161, 162, 163, 168, 172, 173, 176, 178, 181, 185, 191, 195, 197, 234, 246, 249, 251, 263, 267, 269, 270, 276, 282,
57+
283, 284, 285, 288, 291, 293, 295, 296, 297, 300, 308, 310, 311, 312, 314, 317, 318, 321, 323, 324, 332, 334, 336, 338, 356,
58+
361, 362, 365, 373, 374, 375, 377, 378, 379, 380, 381, 382, 384, 385, 386, 387, 388, 389, 390, 397, 398, 400, 402, 405,
59+
409, 415, 454, 466, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477
60+
]]
61+
face_blendshapes_inp = face_blendshapes_inp[:, [0, 1]].reshape(1, -1, 2)
62+
np.save("face_blendshapes_inp.npy", np.ascontiguousarray(face_blendshapes_inp))
63+
run_tflite_model("face_blendshapes", inp=face_blendshapes_inp)
64+
4765
run_mediapipe_solution(mp.solutions.selfie_segmentation.SelfieSegmentation(model_selection=0), (256, 256))
4866

4967
# Save TensorFlow model as TFLite

0 commit comments

Comments
 (0)