forked from joyycom/VNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewCtrl_Camera_Face.mm
126 lines (105 loc) · 5.96 KB
/
ViewCtrl_Camera_Face.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
#import "ViewCtrl_Camera_Face.h"
#import "vnn_kit.h"
#if USE_FACE
# import "vnn_face.h"
#endif
@interface ViewCtrl_Camera_Face ()
@property (nonatomic, assign) NSUInteger segctrl_num_pts_select_index;
@property (nonatomic, strong) UISegmentedControl * segctrl_num_pts;
@property (nonatomic, assign) VNNHandle handle;
@property (nonatomic, assign) int use_278pts;
@end
@implementation ViewCtrl_Camera_Face
- (UISegmentedControl *)segctrl_num_pts {
if (!_segctrl_num_pts) {
_segctrl_num_pts = [[UISegmentedControl alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 1.0 / 9.0,
(ACTUAL_SCREEN_HEIGHT - SCREEN_HEIGHT) / 2 + SCREEN_HEIGHT * 15 / 16.0,
SCREEN_WIDTH * 7 / 9.0,
SCREEN_HEIGHT * 0.75 / 16.0)];
if (@available(iOS 13.0, *)) {
[_segctrl_num_pts setSelectedSegmentTintColor:UIColorFromRGB(0x0000f0)];
}
[_segctrl_num_pts setApportionsSegmentWidthsByContent:YES];
[_segctrl_num_pts insertSegmentWithTitle:@"104点" atIndex:0 animated:YES];
[_segctrl_num_pts insertSegmentWithTitle:@"278点" atIndex:1 animated:YES];
_segctrl_num_pts_select_index = 0;
[_segctrl_num_pts setSelectedSegmentIndex:_segctrl_num_pts_select_index];
}
return _segctrl_num_pts;
}
- (void)loadView {
[super loadView];
[self.view addSubview:self.segctrl_num_pts];
}
- (void)viewDidLoad {
# if USE_FACE
VNN_SetLogLevel(VNN_LOG_LEVEL_ALL);
const void *argv[] = {
[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"files/models/vnn_face278_data/face_mobile[1.0.0].vnnmodel"].UTF8String,
};
const int argc = sizeof(argv)/sizeof(argv[0]);
VNN_Create_Face(&_handle, argc, argv);
# endif
[super viewDidLoad];
}
- (void)onBtnBack {
# if USE_FACE
VNN_Destroy_Face(&_handle);
# endif
[super onBtnBack];
}
- (void)videoCaptureCallback:(CVPixelBufferRef _Nullable)pixelBuffer {
# if USE_FACE
if (_handle > 0) {
_use_278pts = (int)_segctrl_num_pts_select_index;
VNN_Set_Face_Attr(_handle, "_use_278pts", &_use_278pts);
VNN_Image input;
VNN_Create_VNNImage_From_PixelBuffer(pixelBuffer, &input, false);
VNN_FaceFrameDataArr output;
memset(&output, 0x00, sizeof(VNN_FaceFrameDataArr));
VNN_Apply_Face_CPU(_handle, &input, &output);
VNN_Free_VNNImage(pixelBuffer, &input, false);
[self.glUtils rectsDrawer]->_rects.clear();
[self.glUtils circlesDrawer]->_circles.clear();
for (auto i = 0; i < output.facesNum; i++) {
[self.glUtils rectsDrawer]->_rects.emplace_back(
vnn::renderkit::DrawRect2D(
MIN(1.f, MAX(0, output.facesArr[i].faceRect.x0)), // left
MIN(1.f, MAX(0, output.facesArr[i].faceRect.y0)), // top
MIN(1.f, MAX(0, output.facesArr[i].faceRect.x1)), // right
MIN(1.f, MAX(0, output.facesArr[i].faceRect.y1)), // bottom
3.f, // thickness
vnn::renderkit::DrawColorRGBA(1.f, 1.f, 0.f, 1.f)
)
);
for (auto j = 0; j < output.facesArr[i].faceLandmarksNum; j++) {
[self.glUtils circlesDrawer]->_circles.emplace_back(
vnn::renderkit::DrawCircle2D(
output.facesArr[i].faceLandmarks[j].x,
output.facesArr[i].faceLandmarks[j].y,
8,
output.facesArr[i].faceLandmarkScores[j] > 0.5? vnn::renderkit::DrawColorRGBA(0.f, 1.f, 0.f, 1.f) : vnn::renderkit::DrawColorRGBA(1.f, 0.f, 0.f, .5f)
)
);
}
}
}
# endif
NSInteger rotateType = UIView_GLRenderUtils_RotateType_None;
NSInteger flipType = UIView_GLRenderUtils_FlipType_None;
if (self.cameraOrientation == AVCaptureVideoOrientationLandscapeRight) {
rotateType = UIView_GLRenderUtils_RotateType_90R;
}
if (self.cameraOrientation == AVCaptureVideoOrientationLandscapeLeft) {
rotateType = UIView_GLRenderUtils_RotateType_90L;
}
if (CVPixelBufferGetPlaneCount(pixelBuffer) != 0){
[self.glUtils draw_With_YTexture:self.NSYTex UVTexture:self.NSUVTex RotateType:rotateType FlipType:flipType];
} else {
[self.glUtils draw_With_BGRATexture:self.NSBGRATex RotateType:rotateType FlipType:flipType];
}
dispatch_async(dispatch_get_main_queue(), ^{
self.segctrl_num_pts_select_index = (int)self.segctrl_num_pts.selectedSegmentIndex;
});
}
@end