1
1
#import " FaceDetectorManagerMlkit.h"
2
2
#import < React/RCTConvert.h>
3
- #if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
3
+ #if __has_include(<MLKitFaceDetection/MLKitFaceDetection.h>)
4
+ @import MLKitVision;
4
5
5
6
@interface FaceDetectorManagerMlkit ()
6
- @property (nonatomic , strong ) FIRVisionFaceDetector *faceRecognizer;
7
- @property (nonatomic , strong ) FIRVision *vision;
8
- @property (nonatomic , strong ) FIRVisionFaceDetectorOptions *options;
7
+ @property (nonatomic , strong ) MLKFaceDetector *faceRecognizer;
8
+ @property (nonatomic , strong ) MLKFaceDetectorOptions *options;
9
9
@property (nonatomic , assign ) float scaleX;
10
10
@property (nonatomic , assign ) float scaleY;
11
11
@end
@@ -15,13 +15,12 @@ @implementation FaceDetectorManagerMlkit
15
15
- (instancetype )init
16
16
{
17
17
if (self = [super init ]) {
18
- self.options = [[FIRVisionFaceDetectorOptions alloc ] init ];
19
- self.options .performanceMode = FIRVisionFaceDetectorPerformanceModeFast ;
20
- self.options .landmarkMode = FIRVisionFaceDetectorLandmarkModeNone ;
21
- self.options .classificationMode = FIRVisionFaceDetectorClassificationModeNone ;
18
+ self.options = [[MLKFaceDetectorOptions alloc ] init ];
19
+ self.options .performanceMode = MLKFaceDetectorPerformanceModeFast ;
20
+ self.options .landmarkMode = MLKFaceDetectorLandmarkModeNone ;
21
+ self.options .classificationMode = MLKFaceDetectorClassificationModeNone ;
22
22
23
- self.vision = [FIRVision vision ];
24
- self.faceRecognizer = [_vision faceDetectorWithOptions: _options];
23
+ self.faceRecognizer = [MLKFaceDetector faceDetectorWithOptions: _options];
25
24
}
26
25
return self;
27
26
}
@@ -57,7 +56,7 @@ - (void)setTracking:(id)json queue:(dispatch_queue_t)sessionQueue
57
56
dispatch_async (sessionQueue, ^{
58
57
self.options .trackingEnabled = requestedValue;
59
58
self.faceRecognizer =
60
- [self .vision faceDetectorWithOptions: self .options];
59
+ [MLKFaceDetector faceDetectorWithOptions: self .options];
61
60
});
62
61
}
63
62
}
@@ -71,7 +70,7 @@ - (void)setLandmarksMode:(id)json queue:(dispatch_queue_t)sessionQueue
71
70
dispatch_async (sessionQueue, ^{
72
71
self.options .landmarkMode = requestedValue;
73
72
self.faceRecognizer =
74
- [self .vision faceDetectorWithOptions: self .options];
73
+ [MLKFaceDetector faceDetectorWithOptions: self .options];
75
74
});
76
75
}
77
76
}
@@ -85,7 +84,7 @@ - (void)setPerformanceMode:(id)json queue:(dispatch_queue_t)sessionQueue
85
84
dispatch_async (sessionQueue, ^{
86
85
self.options .performanceMode = requestedValue;
87
86
self.faceRecognizer =
88
- [self .vision faceDetectorWithOptions: self .options];
87
+ [MLKFaceDetector faceDetectorWithOptions: self .options];
89
88
});
90
89
}
91
90
}
@@ -99,7 +98,7 @@ - (void)setClassificationMode:(id)json queue:(dispatch_queue_t)sessionQueue
99
98
dispatch_async (sessionQueue, ^{
100
99
self.options .classificationMode = requestedValue;
101
100
self.faceRecognizer =
102
- [self .vision faceDetectorWithOptions: self .options];
101
+ [MLKFaceDetector faceDetectorWithOptions: self .options];
103
102
});
104
103
}
105
104
}
@@ -112,11 +111,11 @@ - (void)findFacesInFrame:(UIImage *)uiImage
112
111
{
113
112
self.scaleX = scaleX;
114
113
self.scaleY = scaleY;
115
- FIRVisionImage *image = [[FIRVisionImage alloc ] initWithImage: uiImage];
114
+ MLKVisionImage *visionImage = [[MLKVisionImage alloc ] initWithImage: uiImage];
116
115
NSMutableArray *emptyResult = [[NSMutableArray alloc ] init ];
117
116
[_faceRecognizer
118
- processImage: image
119
- completion: ^(NSArray <FIRVisionFace *> *faces, NSError *error) {
117
+ processImage: visionImage
118
+ completion: ^(NSArray <MLKFace *> *faces, NSError *error) {
120
119
if (error != nil || faces == nil ) {
121
120
completed (emptyResult);
122
121
} else {
@@ -128,7 +127,7 @@ - (void)findFacesInFrame:(UIImage *)uiImage
128
127
- (NSArray *)processFaces : (NSArray *)faces
129
128
{
130
129
NSMutableArray *result = [[NSMutableArray alloc ] init ];
131
- for (FIRVisionFace *face in faces) {
130
+ for (MLKFace *face in faces) {
132
131
NSMutableDictionary *resultDict =
133
132
[[NSMutableDictionary alloc ] initWithCapacity: 20 ];
134
133
// Boundaries of face in image
@@ -153,71 +152,71 @@ - (NSArray *)processFaces:(NSArray *)faces
153
152
// If landmark detection was enabled (mouth, ears, eyes, cheeks, and
154
153
// nose available):
155
154
/* * Midpoint of the left ear tip and left ear lobe. */
156
- FIRVisionFaceLandmark *leftEar =
157
- [face landmarkOfType: FIRFaceLandmarkTypeLeftEar ];
155
+ MLKFaceLandmark *leftEar =
156
+ [face landmarkOfType: MLKFaceLandmarkTypeLeftEar ];
158
157
if (leftEar != nil ) {
159
158
[resultDict setObject: [self processPoint: leftEar.position]
160
159
forKey: @" leftEarPosition" ];
161
160
}
162
161
/* * Midpoint of the right ear tip and right ear lobe. */
163
- FIRVisionFaceLandmark *rightEar =
164
- [face landmarkOfType: FIRFaceLandmarkTypeRightEar ];
162
+ MLKFaceLandmark *rightEar =
163
+ [face landmarkOfType: MLKFaceLandmarkTypeRightEar ];
165
164
if (rightEar != nil ) {
166
165
[resultDict setObject: [self processPoint: rightEar.position]
167
166
forKey: @" rightEarPosition" ];
168
167
}
169
168
/* * Center of the bottom lip. */
170
- FIRVisionFaceLandmark *mouthBottom =
171
- [face landmarkOfType: FIRFaceLandmarkTypeMouthBottom ];
169
+ MLKFaceLandmark *mouthBottom =
170
+ [face landmarkOfType: MLKFaceLandmarkTypeMouthBottom ];
172
171
if (mouthBottom != nil ) {
173
172
[resultDict setObject: [self processPoint: mouthBottom.position]
174
173
forKey: @" bottomMouthPosition" ];
175
174
}
176
175
/* * Right corner of the mouth */
177
- FIRVisionFaceLandmark *mouthRight =
178
- [face landmarkOfType: FIRFaceLandmarkTypeMouthRight ];
176
+ MLKFaceLandmark *mouthRight =
177
+ [face landmarkOfType: MLKFaceLandmarkTypeMouthRight ];
179
178
if (mouthRight != nil ) {
180
179
[resultDict setObject: [self processPoint: mouthRight.position]
181
180
forKey: @" rightMouthPosition" ];
182
181
}
183
182
/* * Left corner of the mouth */
184
- FIRVisionFaceLandmark *mouthLeft =
185
- [face landmarkOfType: FIRFaceLandmarkTypeMouthLeft ];
183
+ MLKFaceLandmark *mouthLeft =
184
+ [face landmarkOfType: MLKFaceLandmarkTypeMouthLeft ];
186
185
if (mouthLeft != nil ) {
187
186
[resultDict setObject: [self processPoint: mouthLeft.position]
188
187
forKey: @" leftMouthPosition" ];
189
188
}
190
189
/* * Left eye. */
191
- FIRVisionFaceLandmark *eyeLeft =
192
- [face landmarkOfType: FIRFaceLandmarkTypeLeftEye ];
190
+ MLKFaceLandmark *eyeLeft =
191
+ [face landmarkOfType: MLKFaceLandmarkTypeLeftEye ];
193
192
if (eyeLeft != nil ) {
194
193
[resultDict setObject: [self processPoint: eyeLeft.position]
195
194
forKey: @" leftEyePosition" ];
196
195
}
197
196
/* * Right eye. */
198
- FIRVisionFaceLandmark *eyeRight =
199
- [face landmarkOfType: FIRFaceLandmarkTypeRightEye ];
197
+ MLKFaceLandmark *eyeRight =
198
+ [face landmarkOfType: MLKFaceLandmarkTypeRightEye ];
200
199
if (eyeRight != nil ) {
201
200
[resultDict setObject: [self processPoint: eyeRight.position]
202
201
forKey: @" rightEyePosition" ];
203
202
}
204
203
/* * Left cheek. */
205
- FIRVisionFaceLandmark *cheekLeft =
206
- [face landmarkOfType: FIRFaceLandmarkTypeLeftCheek ];
204
+ MLKFaceLandmark *cheekLeft =
205
+ [face landmarkOfType: MLKFaceLandmarkTypeLeftCheek ];
207
206
if (cheekLeft != nil ) {
208
207
[resultDict setObject: [self processPoint: cheekLeft.position]
209
208
forKey: @" leftCheekPosition" ];
210
209
}
211
210
/* * Right cheek. */
212
- FIRVisionFaceLandmark *cheekRight =
213
- [face landmarkOfType: FIRFaceLandmarkTypeRightCheek ];
211
+ MLKFaceLandmark *cheekRight =
212
+ [face landmarkOfType: MLKFaceLandmarkTypeRightCheek ];
214
213
if (cheekRight != nil ) {
215
214
[resultDict setObject: [self processPoint: cheekRight.position]
216
215
forKey: @" rightCheekPosition" ];
217
216
}
218
217
/* * Midpoint between the nostrils where the nose meets the face. */
219
- FIRVisionFaceLandmark *noseBase =
220
- [face landmarkOfType: FIRFaceLandmarkTypeNoseBase ];
218
+ MLKFaceLandmark *noseBase =
219
+ [face landmarkOfType: MLKFaceLandmarkTypeNoseBase ];
221
220
if (noseBase != nil ) {
222
221
[resultDict setObject: [self processPoint: noseBase.position]
223
222
forKey: @" noseBasePosition" ];
@@ -256,10 +255,10 @@ - (NSDictionary *)processBounds:(CGRect)bounds
256
255
return boundsDict;
257
256
}
258
257
259
- - (NSDictionary *)processPoint : (FIRVisionPoint *)point
258
+ - (NSDictionary *)processPoint : (MLKVisionPoint *)point
260
259
{
261
- float originX = [ point.x floatValue ] * _scaleX;
262
- float originY = [ point.y floatValue ] * _scaleY;
260
+ float originX = point.x * _scaleX;
261
+ float originY = point.y * _scaleY;
263
262
NSDictionary *pointDict = @{
264
263
265
264
@" x" : @(originX),
0 commit comments