11
11
image = cv .imread (os .path .join (testdata , "cv" , "shared" , "lena.png" ))
12
12
image = cv .cvtColor (image , cv .COLOR_BGR2RGB )
13
13
14
- def run_tflite_model (model_name , inp_size ):
14
+ def run_tflite_model (model_name , inp_size = None , inp = None ):
15
15
interpreter = tf .lite .Interpreter (model_name + ".tflite" ,
16
16
experimental_preserve_all_tensors = True )
17
17
interpreter .allocate_tensors ()
@@ -21,9 +21,10 @@ def run_tflite_model(model_name, inp_size):
21
21
output_details = interpreter .get_output_details ()
22
22
23
23
# 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
27
28
28
29
interpreter .set_tensor (input_details [0 ]['index' ], inp )
29
30
@@ -32,7 +33,7 @@ def run_tflite_model(model_name, inp_size):
32
33
for details in output_details :
33
34
out = interpreter .get_tensor (details ['index' ]) # Or use an intermediate layer index
34
35
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 )
36
37
37
38
38
39
def run_mediapipe_solution (solution , inp_size ):
@@ -44,6 +45,23 @@ def run_mediapipe_solution(solution, inp_size):
44
45
run_tflite_model ("face_landmark" , (192 , 192 ))
45
46
run_tflite_model ("face_detection_short_range" , (128 , 128 ))
46
47
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
+
47
65
run_mediapipe_solution (mp .solutions .selfie_segmentation .SelfieSegmentation (model_selection = 0 ), (256 , 256 ))
48
66
49
67
# Save TensorFlow model as TFLite
0 commit comments