-
Notifications
You must be signed in to change notification settings - Fork 732
/
Copy pathtest_lite_female_photo2cartoon.cpp
213 lines (180 loc) · 8.78 KB
/
test_lite_female_photo2cartoon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// Created by DefTruth on 2022/6/12.
//
#include "lite/lite.h"
static void test_default()
{
std::string head_seg_onnx_path = "../../../examples/hub/onnx/cv/minivision_head_seg.onnx";
std::string cartoon_onnx_path = "../../../examples/hub/onnx/cv/minivision_female_photo2cartoon.onnx";
std::string test_img_path = "../../../examples/lite/resources/test_lite_head_seg.png";
std::string save_mask_path = "../../../examples/logs/test_lite_female_photo2cartoon_seg.jpg";
std::string save_cartoon_path = "../../../examples/logs/test_lite_female_photo2cartoon_cartoon.jpg";
lite::cv::segmentation::HeadSeg *head_seg =
new lite::cv::segmentation::HeadSeg(head_seg_onnx_path, 4); // 4 threads
lite::cv::style::FemalePhoto2Cartoon *female_photo2cartoon =
new lite::cv::style::FemalePhoto2Cartoon(cartoon_onnx_path, 4); // 4 threads
lite::types::HeadSegContent head_seg_content;
cv::Mat img_bgr = cv::imread(test_img_path);
head_seg->detect(img_bgr, head_seg_content);
if (head_seg_content.flag && !head_seg_content.mask.empty())
{
cv::imwrite(save_mask_path, head_seg_content.mask * 255.f);
std::cout << "Default Version HeadSeg Done!" << std::endl;
// Female Photo2Cartoon Style Transfer
lite::types::FemalePhoto2CartoonContent female_cartoon_content;
female_photo2cartoon->detect(img_bgr, head_seg_content.mask, female_cartoon_content);
if (female_cartoon_content.flag && !female_cartoon_content.cartoon.empty())
{
cv::imwrite(save_cartoon_path, female_cartoon_content.cartoon);
std::cout << "Default Version FemalePhoto2Cartoon Done!" << std::endl;
}
}
delete head_seg;
delete female_photo2cartoon;
}
static void test_onnxruntime()
{
#ifdef ENABLE_ONNXRUNTIME
std::string head_seg_onnx_path = "../../../examples/hub/onnx/cv/minivision_head_seg.onnx";
std::string cartoon_onnx_path = "../../../examples/hub/onnx/cv/minivision_female_photo2cartoon.onnx";
std::string test_img_path = "../../../examples/lite/resources/test_lite_female_photo2cartoon.jpg";
std::string save_mask_path = "../../../examples/logs/test_lite_female_photo2cartoon_seg_1_onnx.jpg";
std::string save_cartoon_path = "../../../examples/logs/test_lite_female_photo2cartoon_cartoon_1_onnx.jpg";
lite::onnxruntime::cv::segmentation::HeadSeg *head_seg =
new lite::onnxruntime::cv::segmentation::HeadSeg(head_seg_onnx_path, 4); // 4 threads
lite::onnxruntime::cv::style::FemalePhoto2Cartoon *female_photo2cartoon =
new lite::onnxruntime::cv::style::FemalePhoto2Cartoon(cartoon_onnx_path, 4); // 4 threads
lite::types::HeadSegContent head_seg_content;
cv::Mat img_bgr = cv::imread(test_img_path);
head_seg->detect(img_bgr, head_seg_content);
if (head_seg_content.flag && !head_seg_content.mask.empty())
{
cv::imwrite(save_mask_path, head_seg_content.mask * 255.f);
std::cout << "ONNXRuntime Version HeadSeg Done!" << std::endl;
// Female Photo2Cartoon Style Transfer
lite::types::FemalePhoto2CartoonContent female_cartoon_content;
female_photo2cartoon->detect(img_bgr, head_seg_content.mask, female_cartoon_content);
if (female_cartoon_content.flag && !female_cartoon_content.cartoon.empty())
{
cv::imwrite(save_cartoon_path, female_cartoon_content.cartoon);
std::cout << "ONNXRuntime Version FemalePhoto2Cartoon Done!" << std::endl;
}
}
delete head_seg;
delete female_photo2cartoon;
#endif
}
static void test_mnn()
{
#ifdef ENABLE_MNN
std::string head_seg_mnn_path = "../../../examples/hub/mnn/cv/minivision_head_seg.mnn";
std::string cartoon_mnn_path = "../../../examples/hub/mnn/cv/minivision_female_photo2cartoon.mnn";
std::string test_img_path = "../../../examples/lite/resources/test_lite_female_photo2cartoon.jpg";
std::string save_mask_path = "../../../examples/logs/test_lite_female_photo2cartoon_seg_1_mnn.jpg";
std::string save_cartoon_path = "../../../examples/logs/test_lite_female_photo2cartoon_cartoon_1_mnn.jpg";
lite::mnn::cv::segmentation::HeadSeg *head_seg =
new lite::mnn::cv::segmentation::HeadSeg(head_seg_mnn_path, 4); // 4 threads
lite::mnn::cv::style::FemalePhoto2Cartoon *female_photo2cartoon =
new lite::mnn::cv::style::FemalePhoto2Cartoon(cartoon_mnn_path, 4); // 4 threads
lite::types::HeadSegContent head_seg_content;
cv::Mat img_bgr = cv::imread(test_img_path);
head_seg->detect(img_bgr, head_seg_content);
if (head_seg_content.flag && !head_seg_content.mask.empty())
{
cv::imwrite(save_mask_path, head_seg_content.mask * 255.f);
std::cout << "MNN Version HeadSeg Done!" << std::endl;
// Female Photo2Cartoon Style Transfer
lite::types::FemalePhoto2CartoonContent female_cartoon_content;
female_photo2cartoon->detect(img_bgr, head_seg_content.mask, female_cartoon_content);
if (female_cartoon_content.flag && !female_cartoon_content.cartoon.empty())
{
cv::imwrite(save_cartoon_path, female_cartoon_content.cartoon);
std::cout << "MNN Version FemalePhoto2Cartoon Done!" << std::endl;
}
}
delete head_seg;
delete female_photo2cartoon;
#endif
}
static void test_ncnn()
{
#ifdef ENABLE_NCNN
// WARN: TEST FAILED !!!
std::string head_seg_onnx_path = "../../../examples/hub/onnx/cv/minivision_head_seg.onnx"; // helper
std::string cartoon_param_path = "../../../examples/hub/ncnn/cv/minivision_female_photo2cartoon.opt.param";
std::string cartoon_bin_path = "../../../examples/hub/ncnn/cv/minivision_female_photo2cartoon.opt.bin";
std::string test_img_path = "../../../examples/lite/resources/test_lite_female_photo2cartoon.jpg";
std::string save_mask_path = "../../../examples/logs/test_lite_female_photo2cartoon_seg_1_ncnn.jpg";
std::string save_cartoon_path = "../../../examples/logs/test_lite_female_photo2cartoon_cartoon_1_ncnn.jpg";
lite::cv::segmentation::HeadSeg *head_seg =
new lite::cv::segmentation::HeadSeg(head_seg_onnx_path, 4); // 4 threads
lite::ncnn::cv::style::FemalePhoto2Cartoon *female_photo2cartoon =
new lite::ncnn::cv::style::FemalePhoto2Cartoon(
cartoon_param_path, cartoon_bin_path, 4); // 4 threads
lite::types::HeadSegContent head_seg_content;
cv::Mat img_bgr = cv::imread(test_img_path);
head_seg->detect(img_bgr, head_seg_content);
if (head_seg_content.flag && !head_seg_content.mask.empty())
{
cv::imwrite(save_mask_path, head_seg_content.mask * 255.f);
std::cout << "NCNN Version HeadSeg Done!" << std::endl;
// Female Photo2Cartoon Style Transfer
lite::types::FemalePhoto2CartoonContent female_cartoon_content;
female_photo2cartoon->detect(img_bgr, head_seg_content.mask, female_cartoon_content);
if (female_cartoon_content.flag && !female_cartoon_content.cartoon.empty())
{
cv::imwrite(save_cartoon_path, female_cartoon_content.cartoon);
std::cout << "NCNN Version FemalePhoto2Cartoon Done!" << std::endl;
}
}
delete head_seg;
delete female_photo2cartoon;
#endif
}
static void test_tnn()
{
#ifdef ENABLE_TNN
std::string head_seg_onnx_path = "../../../examples/hub/onnx/cv/minivision_head_seg.onnx"; // helper
std::string cartoon_proto_path = "../../../examples/hub/tnn/cv/minivision_female_photo2cartoon.opt.tnnproto";
std::string cartoon_model_path = "../../../examples/hub/tnn/cv/minivision_female_photo2cartoon.opt.tnnmodel";
std::string test_img_path = "../../../examples/lite/resources/test_lite_female_photo2cartoon.jpg";
std::string save_mask_path = "../../../examples/logs/test_lite_female_photo2cartoon_seg_1_tnn.jpg";
std::string save_cartoon_path = "../../../examples/logs/test_lite_female_photo2cartoon_cartoon_1_tnn.jpg";
lite::cv::segmentation::HeadSeg *head_seg =
new lite::cv::segmentation::HeadSeg(head_seg_onnx_path, 4); // 4 threads
lite::tnn::cv::style::FemalePhoto2Cartoon *female_photo2cartoon =
new lite::tnn::cv::style::FemalePhoto2Cartoon(
cartoon_proto_path, cartoon_model_path, 4); // 4 threads
lite::types::HeadSegContent head_seg_content;
cv::Mat img_bgr = cv::imread(test_img_path);
head_seg->detect(img_bgr, head_seg_content);
if (head_seg_content.flag && !head_seg_content.mask.empty())
{
cv::imwrite(save_mask_path, head_seg_content.mask * 255.f);
std::cout << "TNN Version HeadSeg Done!" << std::endl;
// Female Photo2Cartoon Style Transfer
lite::types::FemalePhoto2CartoonContent female_cartoon_content;
female_photo2cartoon->detect(img_bgr, head_seg_content.mask, female_cartoon_content);
if (female_cartoon_content.flag && !female_cartoon_content.cartoon.empty())
{
cv::imwrite(save_cartoon_path, female_cartoon_content.cartoon);
std::cout << "TNN Version FemalePhoto2Cartoon Done!" << std::endl;
}
}
delete head_seg;
delete female_photo2cartoon;
#endif
}
static void test_lite()
{
test_default();
test_onnxruntime();
test_mnn();
// test_ncnn();
test_tnn();
}
int main(__unused int argc, __unused char *argv[])
{
test_lite();
return 0;
}