Skip to content

Commit 3563e94

Browse files
authored
Merge pull request #1042 from dkurt:dnn_tflite
* Test data for TFLite importer * TFLite Hair Segmentation test data * Remove commented code
1 parent 9beaefb commit 3563e94

7 files changed

+67
-0
lines changed

testdata/dnn/download_models.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,26 @@ def get_confirm_token(response): # in case of large files
10201020
url='https://raw.githubusercontent.com/zihaomu/opencv_extra_data_backup/main/NanoTrackV2/models/nanotrack_head_sim_v2.onnx',
10211021
sha='39f168489671700cf739e402dfc67d41ce648aef',
10221022
filename='onnx/models/nanotrack_head_sim_v2.onnx'),
1023+
Model(
1024+
name='Face Mesh (TFLite)',
1025+
url='https://storage.googleapis.com/mediapipe-assets/face_landmark.tflite',
1026+
sha='eb01d1d88c833aaea64c880506da72e4a4f43154',
1027+
filename='tflite/face_landmark.tflite'),
1028+
Model(
1029+
name='Face Detection (TFLite)',
1030+
url='https://storage.googleapis.com/mediapipe-assets/face_detection_short_range.tflite',
1031+
sha='e8f749fafc23bb88daac85bc9f7e0698436f29a0',
1032+
filename='tflite/face_detection_short_range.tflite'),
1033+
Model(
1034+
name='Selfie Segmentation (TFLite)',
1035+
url='https://storage.googleapis.com/mediapipe-assets/selfie_segmentation.tflite',
1036+
sha='8d497f51bd678fa5fb95c3871be72eb5d722b831',
1037+
filename='tflite/selfie_segmentation.tflite'),
1038+
Model(
1039+
name='Hair Segmentation (TFLite)',
1040+
url='https://storage.googleapis.com/mediapipe-assets/hair_segmentation.tflite',
1041+
sha='bba28400dfc264b1ed7ee95df718fada1879644d',
1042+
filename='tflite/hair_segmentation.tflite'),
10231043
]
10241044

10251045
# Note: models will be downloaded to current working directory
Binary file not shown.
Binary file not shown.
5.61 KB
Binary file not shown.
132 Bytes
Binary file not shown.

testdata/dnn/tflite/generate.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Use this script to generate test data for dnn module and TFLite models
2+
import os
3+
import numpy as np
4+
import tensorflow as tf
5+
import mediapipe as mp
6+
7+
import cv2 as cv
8+
9+
testdata = os.environ['OPENCV_TEST_DATA_PATH']
10+
11+
image = cv.imread(os.path.join(testdata, "cv", "shared", "lena.png"))
12+
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
13+
14+
def run_tflite_model(model_name, inp_size):
15+
interpreter = tf.lite.Interpreter(model_name + ".tflite",
16+
experimental_preserve_all_tensors=True)
17+
interpreter.allocate_tensors()
18+
19+
# Get input and output tensors.
20+
input_details = interpreter.get_input_details()
21+
output_details = interpreter.get_output_details()
22+
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
27+
28+
interpreter.set_tensor(input_details[0]['index'], inp)
29+
30+
interpreter.invoke()
31+
32+
for details in output_details:
33+
out = interpreter.get_tensor(details['index']) # Or use an intermediate layer index
34+
out_name = details['name']
35+
np.save(f"{model_name}_out_{out_name}.npy", out)
36+
37+
38+
def run_mediapipe_solution(solution, inp_size):
39+
with solution as selfie_segmentation:
40+
inp = cv.resize(image, inp_size)
41+
results = selfie_segmentation.process(inp)
42+
np.save(f"selfie_segmentation_out_activation_10.npy", results.segmentation_mask)
43+
44+
run_tflite_model("face_landmark", (192, 192))
45+
run_tflite_model("face_detection_short_range", (128, 128))
46+
47+
run_mediapipe_solution(mp.solutions.selfie_segmentation.SelfieSegmentation(model_selection=0), (256, 256))
256 KB
Binary file not shown.

0 commit comments

Comments
 (0)