-
Notifications
You must be signed in to change notification settings - Fork 770
/
Copy pathTNNFaceDetectAlignerViewModel.mm
201 lines (170 loc) · 8.3 KB
/
TNNFaceDetectAlignerViewModel.mm
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
// Tencent is pleased to support the open source community by making TNN available.
//
// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#import "TNNFaceDetectAlignerViewModel.h"
#import "face_detect_aligner.h"
#import "youtu_face_align.h"
#import "blazeface_detector.h"
#import "UIImage+Utility.h"
#import <Metal/Metal.h>
#import <memory>
using namespace std;
@implementation TNNFaceDetectAlignerViewModel
- (std::shared_ptr<BlazeFaceDetector>) loadFaceDetector:(TNNComputeUnits)units {
std::shared_ptr<BlazeFaceDetector> predictor = nullptr;
auto library_path = [[NSBundle mainBundle] pathForResource:@"tnn.metallib" ofType:nil];
auto model_path = [[NSBundle mainBundle] pathForResource:@"model/blazeface/blazeface.tnnmodel"
ofType:nil];
auto proto_path = [[NSBundle mainBundle] pathForResource:@"model/blazeface/blazeface.tnnproto"
ofType:nil];
auto anchor_path = [[NSBundle mainBundle] pathForResource:@"model/blazeface/blazeface_anchors.txt"
ofType:nil];
if (proto_path.length <= 0 || model_path.length <= 0 || anchor_path.length <= 0) {
LOGE("Error: proto or model or anchor path is invalid\n");
return predictor;
}
string proto_content =
[NSString stringWithContentsOfFile:proto_path encoding:NSUTF8StringEncoding error:nil].UTF8String;
NSData *data_mode = [NSData dataWithContentsOfFile:model_path];
string model_content = [data_mode length] > 0 ? string((const char *)[data_mode bytes], [data_mode length]) : "";
if (proto_content.size() <= 0 || model_content.size() <= 0) {
LOGE("Error: proto or model path is invalid\n");
return predictor;
}
//blazeface requires input with shape 128*128
const int target_height = 128;
const int target_width = 128;
DimsVector target_dims = {1, 3, target_height, target_width};
auto option = std::make_shared<BlazeFaceDetectorOption>();
{
option->proto_content = proto_content;
option->model_content = model_content;
option->library_path = library_path.UTF8String;
option->compute_units = units;
option->cache_path = NSTemporaryDirectory().UTF8String;
option->input_width = target_width;
option->input_height = target_height;
//min_score_thresh
option->min_score_threshold = 0.75;
//min_suppression_thresh
option->min_suppression_threshold = 0.3;
//predefined anchor file path
option->anchor_path = string(anchor_path.UTF8String);
}
predictor = std::make_shared<BlazeFaceDetector>();
auto status = predictor->Init(option);
if (status != TNN_OK) {
LOGE("Error: %s\n", status.description().c_str());
return nullptr;
}
return predictor;
}
- (std::shared_ptr<YoutuFaceAlign>) loadYoutuFaceAlign:(TNNComputeUnits)units : (int) phase {
std::shared_ptr<YoutuFaceAlign> predictor = nullptr;
auto library_path = [[NSBundle mainBundle] pathForResource:@"tnn.metallib" ofType:nil];
NSString *model_path = nil;
NSString *proto_path = nil;
NSString *mean_pts_path = nil;
if(1 == phase) {
model_path = [[NSBundle mainBundle] pathForResource:@"model/youtu_face_alignment/youtu_face_alignment_phase1.tnnmodel"
ofType:nil];
proto_path = [[NSBundle mainBundle] pathForResource:@"model/youtu_face_alignment/youtu_face_alignment_phase1.tnnproto"
ofType:nil];
mean_pts_path = [[NSBundle mainBundle] pathForResource:@"model/youtu_face_alignment/youtu_mean_pts_phase1.txt"
ofType:nil];
} else if(2 == phase) {
model_path = [[NSBundle mainBundle] pathForResource:@"model/youtu_face_alignment/youtu_face_alignment_phase2.tnnmodel"
ofType:nil];
proto_path = [[NSBundle mainBundle] pathForResource:@"model/youtu_face_alignment/youtu_face_alignment_phase2.tnnproto"
ofType:nil];
mean_pts_path = [[NSBundle mainBundle] pathForResource:@"model/youtu_face_alignment/youtu_mean_pts_phase2.txt"
ofType:nil];
} else{
LOGE("Error: facealign model phase is invalid\n");
return nullptr;
}
if (proto_path.length <= 0 || model_path.length <= 0) {
LOGE("Error: proto or model path is invalid\n");
return predictor;
}
string proto_content =
[NSString stringWithContentsOfFile:proto_path encoding:NSUTF8StringEncoding error:nil].UTF8String;
NSData *data_mode = [NSData dataWithContentsOfFile:model_path];
string model_content = [data_mode length] > 0 ? string((const char *)[data_mode bytes], [data_mode length]) : "";
if (proto_content.size() <= 0 || model_content.size() <= 0) {
LOGE("Error: proto or model path is invalid\n");
return predictor;
}
//youtu facealign models require input with shape 128*128
const int target_height = 128;
const int target_width = 128;
DimsVector target_dims = {1, 1, target_height, target_width};
auto option = std::make_shared<YoutuFaceAlignOption>();
{
option->proto_content = proto_content;
option->model_content = model_content;
option->library_path = library_path.UTF8String;
option->compute_units = units;
option->cache_path = NSTemporaryDirectory().UTF8String;
option->input_width = target_width;
option->input_height = target_height;
//face threshold
option->face_threshold = 0.5;
option->min_face_size = 20;
//model phase
option->phase = phase;
//net_scale
option->net_scale = phase == 1? 1.2 : 1.3;
//mean pts path
option->mean_pts_path = mean_pts_path ? string(mean_pts_path.UTF8String) : "";
}
predictor = std::make_shared<YoutuFaceAlign>();
auto status = predictor->Init(option);
if (status != TNN_OK) {
LOGE("Error: %s\n", status.description().c_str());
return nullptr;
}
return predictor;
}
-(Status)loadNeuralNetworkModel:(TNNComputeUnits)units {
Status status = TNN_OK;
auto face_detector = [self loadFaceDetector:units];
auto predictor_phase1 = [self loadYoutuFaceAlign:units :1];
auto predictor_phase2 = [self loadYoutuFaceAlign:units :2];
if (!face_detector) {
return Status(TNNERR_MODEL_ERR, "loadFaceDetector failed: pls make sure the face detect model is downloaded");
}
if (!predictor_phase1 || !predictor_phase2) {
return Status(TNNERR_MODEL_ERR, "loadYoutuFaceAlign failed: pls make sure the face alignment model is downloaded");
}
auto predictor = std::make_shared<FaceDetectAligner>();
status = predictor->Init({face_detector, predictor_phase1, predictor_phase2});
self.predictor = predictor;
//TODO: we need to set it to false when change camera
self.prev_face = false;
return status;
}
-(std::vector<std::shared_ptr<ObjectInfo> >)getObjectList:(std::shared_ptr<TNNSDKOutput>)sdk_output {
std::vector<std::shared_ptr<ObjectInfo> > object_list;
if (sdk_output && dynamic_cast<YoutuFaceAlignOutput *>(sdk_output.get())) {
auto face_output = dynamic_cast<YoutuFaceAlignOutput *>(sdk_output.get());
auto face = std::make_shared<YoutuFaceAlignInfo>();
*face = face_output->face;
object_list.push_back(face);
}
return object_list;
}
-(NSString*)labelForObject:(std::shared_ptr<ObjectInfo>)object {
return nil;
}
@end