Skip to content
This repository was archived by the owner on Jun 16, 2023. It is now read-only.

Commit 670b973

Browse files
authored
feat: migrate to google ML Kit (#3241) [skip ci]
Co-authored-by: Mike Duminy <michael-james.duminy@klarna.com>
1 parent 0359c32 commit 670b973

9 files changed

+146
-151
lines changed

ios/RN/BarcodeDetectorManagerMlkit.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
#import <UIKit/UIKit.h>
3-
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
4-
#import <FirebaseMLVision/FirebaseMLVision.h>
3+
#if __has_include(<MLKitBarcodeScanning/MLKitBarcodeScanning.h>)
4+
@import MLKitBarcodeScanning;
55
#endif
66

77
@interface BarcodeDetectorManagerMlkit : NSObject

ios/RN/BarcodeDetectorManagerMlkit.m

+69-70
Large diffs are not rendered by default.

ios/RN/FaceDetectorManagerMlkit.h

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11

22
#import <UIKit/UIKit.h>
3-
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
4-
#import <FirebaseMLVision/FirebaseMLVision.h>
3+
#if __has_include(<MLKitFaceDetection/MLKitFaceDetection.h>)
4+
@import MLKitFaceDetection;
5+
56
typedef NS_ENUM(NSInteger, RNFaceDetectionMode) {
6-
RNFaceDetectionFastMode = FIRVisionFaceDetectorPerformanceModeFast,
7-
RNFaceDetectionAccurateMode = FIRVisionFaceDetectorPerformanceModeAccurate
7+
RNFaceDetectionFastMode = MLKFaceDetectorPerformanceModeFast,
8+
RNFaceDetectionAccurateMode = MLKFaceDetectorPerformanceModeAccurate
89
};
910

1011
typedef NS_ENUM(NSInteger, RNFaceDetectionLandmarks) {
11-
RNFaceDetectAllLandmarks = FIRVisionFaceDetectorLandmarkModeAll,
12-
RNFaceDetectNoLandmarks = FIRVisionFaceDetectorLandmarkModeNone
12+
RNFaceDetectAllLandmarks = MLKFaceDetectorLandmarkModeAll,
13+
RNFaceDetectNoLandmarks = MLKFaceDetectorLandmarkModeNone
1314
};
1415

1516
typedef NS_ENUM(NSInteger, RNFaceDetectionClassifications) {
16-
RNFaceRunAllClassifications = FIRVisionFaceDetectorClassificationModeAll,
17-
RNFaceRunNoClassifications = FIRVisionFaceDetectorClassificationModeNone
17+
RNFaceRunAllClassifications = MLKFaceDetectorClassificationModeAll,
18+
RNFaceRunNoClassifications = MLKFaceDetectorClassificationModeNone
1819
};
20+
1921
#endif
2022

2123
@interface FaceDetectorManagerMlkit : NSObject

ios/RN/FaceDetectorManagerMlkit.m

+40-41
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#import "FaceDetectorManagerMlkit.h"
22
#import <React/RCTConvert.h>
3-
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
3+
#if __has_include(<MLKitFaceDetection/MLKitFaceDetection.h>)
4+
@import MLKitVision;
45

56
@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;
99
@property(nonatomic, assign) float scaleX;
1010
@property(nonatomic, assign) float scaleY;
1111
@end
@@ -15,13 +15,12 @@ @implementation FaceDetectorManagerMlkit
1515
- (instancetype)init
1616
{
1717
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;
2222

23-
self.vision = [FIRVision vision];
24-
self.faceRecognizer = [_vision faceDetectorWithOptions:_options];
23+
self.faceRecognizer = [MLKFaceDetector faceDetectorWithOptions:_options];
2524
}
2625
return self;
2726
}
@@ -57,7 +56,7 @@ - (void)setTracking:(id)json queue:(dispatch_queue_t)sessionQueue
5756
dispatch_async(sessionQueue, ^{
5857
self.options.trackingEnabled = requestedValue;
5958
self.faceRecognizer =
60-
[self.vision faceDetectorWithOptions:self.options];
59+
[MLKFaceDetector faceDetectorWithOptions:self.options];
6160
});
6261
}
6362
}
@@ -71,7 +70,7 @@ - (void)setLandmarksMode:(id)json queue:(dispatch_queue_t)sessionQueue
7170
dispatch_async(sessionQueue, ^{
7271
self.options.landmarkMode = requestedValue;
7372
self.faceRecognizer =
74-
[self.vision faceDetectorWithOptions:self.options];
73+
[MLKFaceDetector faceDetectorWithOptions:self.options];
7574
});
7675
}
7776
}
@@ -85,7 +84,7 @@ - (void)setPerformanceMode:(id)json queue:(dispatch_queue_t)sessionQueue
8584
dispatch_async(sessionQueue, ^{
8685
self.options.performanceMode = requestedValue;
8786
self.faceRecognizer =
88-
[self.vision faceDetectorWithOptions:self.options];
87+
[MLKFaceDetector faceDetectorWithOptions:self.options];
8988
});
9089
}
9190
}
@@ -99,7 +98,7 @@ - (void)setClassificationMode:(id)json queue:(dispatch_queue_t)sessionQueue
9998
dispatch_async(sessionQueue, ^{
10099
self.options.classificationMode = requestedValue;
101100
self.faceRecognizer =
102-
[self.vision faceDetectorWithOptions:self.options];
101+
[MLKFaceDetector faceDetectorWithOptions:self.options];
103102
});
104103
}
105104
}
@@ -112,11 +111,11 @@ - (void)findFacesInFrame:(UIImage *)uiImage
112111
{
113112
self.scaleX = scaleX;
114113
self.scaleY = scaleY;
115-
FIRVisionImage *image = [[FIRVisionImage alloc] initWithImage:uiImage];
114+
MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:uiImage];
116115
NSMutableArray *emptyResult = [[NSMutableArray alloc] init];
117116
[_faceRecognizer
118-
processImage:image
119-
completion:^(NSArray<FIRVisionFace *> *faces, NSError *error) {
117+
processImage:visionImage
118+
completion:^(NSArray<MLKFace *> *faces, NSError *error) {
120119
if (error != nil || faces == nil) {
121120
completed(emptyResult);
122121
} else {
@@ -128,7 +127,7 @@ - (void)findFacesInFrame:(UIImage *)uiImage
128127
- (NSArray *)processFaces:(NSArray *)faces
129128
{
130129
NSMutableArray *result = [[NSMutableArray alloc] init];
131-
for (FIRVisionFace *face in faces) {
130+
for (MLKFace *face in faces) {
132131
NSMutableDictionary *resultDict =
133132
[[NSMutableDictionary alloc] initWithCapacity:20];
134133
// Boundaries of face in image
@@ -153,71 +152,71 @@ - (NSArray *)processFaces:(NSArray *)faces
153152
// If landmark detection was enabled (mouth, ears, eyes, cheeks, and
154153
// nose available):
155154
/** Midpoint of the left ear tip and left ear lobe. */
156-
FIRVisionFaceLandmark *leftEar =
157-
[face landmarkOfType:FIRFaceLandmarkTypeLeftEar];
155+
MLKFaceLandmark *leftEar =
156+
[face landmarkOfType:MLKFaceLandmarkTypeLeftEar];
158157
if (leftEar != nil) {
159158
[resultDict setObject:[self processPoint:leftEar.position]
160159
forKey:@"leftEarPosition"];
161160
}
162161
/** Midpoint of the right ear tip and right ear lobe. */
163-
FIRVisionFaceLandmark *rightEar =
164-
[face landmarkOfType:FIRFaceLandmarkTypeRightEar];
162+
MLKFaceLandmark *rightEar =
163+
[face landmarkOfType:MLKFaceLandmarkTypeRightEar];
165164
if (rightEar != nil) {
166165
[resultDict setObject:[self processPoint:rightEar.position]
167166
forKey:@"rightEarPosition"];
168167
}
169168
/** Center of the bottom lip. */
170-
FIRVisionFaceLandmark *mouthBottom =
171-
[face landmarkOfType:FIRFaceLandmarkTypeMouthBottom];
169+
MLKFaceLandmark *mouthBottom =
170+
[face landmarkOfType:MLKFaceLandmarkTypeMouthBottom];
172171
if (mouthBottom != nil) {
173172
[resultDict setObject:[self processPoint:mouthBottom.position]
174173
forKey:@"bottomMouthPosition"];
175174
}
176175
/** Right corner of the mouth */
177-
FIRVisionFaceLandmark *mouthRight =
178-
[face landmarkOfType:FIRFaceLandmarkTypeMouthRight];
176+
MLKFaceLandmark *mouthRight =
177+
[face landmarkOfType:MLKFaceLandmarkTypeMouthRight];
179178
if (mouthRight != nil) {
180179
[resultDict setObject:[self processPoint:mouthRight.position]
181180
forKey:@"rightMouthPosition"];
182181
}
183182
/** Left corner of the mouth */
184-
FIRVisionFaceLandmark *mouthLeft =
185-
[face landmarkOfType:FIRFaceLandmarkTypeMouthLeft];
183+
MLKFaceLandmark *mouthLeft =
184+
[face landmarkOfType:MLKFaceLandmarkTypeMouthLeft];
186185
if (mouthLeft != nil) {
187186
[resultDict setObject:[self processPoint:mouthLeft.position]
188187
forKey:@"leftMouthPosition"];
189188
}
190189
/** Left eye. */
191-
FIRVisionFaceLandmark *eyeLeft =
192-
[face landmarkOfType:FIRFaceLandmarkTypeLeftEye];
190+
MLKFaceLandmark *eyeLeft =
191+
[face landmarkOfType:MLKFaceLandmarkTypeLeftEye];
193192
if (eyeLeft != nil) {
194193
[resultDict setObject:[self processPoint:eyeLeft.position]
195194
forKey:@"leftEyePosition"];
196195
}
197196
/** Right eye. */
198-
FIRVisionFaceLandmark *eyeRight =
199-
[face landmarkOfType:FIRFaceLandmarkTypeRightEye];
197+
MLKFaceLandmark *eyeRight =
198+
[face landmarkOfType:MLKFaceLandmarkTypeRightEye];
200199
if (eyeRight != nil) {
201200
[resultDict setObject:[self processPoint:eyeRight.position]
202201
forKey:@"rightEyePosition"];
203202
}
204203
/** Left cheek. */
205-
FIRVisionFaceLandmark *cheekLeft =
206-
[face landmarkOfType:FIRFaceLandmarkTypeLeftCheek];
204+
MLKFaceLandmark *cheekLeft =
205+
[face landmarkOfType:MLKFaceLandmarkTypeLeftCheek];
207206
if (cheekLeft != nil) {
208207
[resultDict setObject:[self processPoint:cheekLeft.position]
209208
forKey:@"leftCheekPosition"];
210209
}
211210
/** Right cheek. */
212-
FIRVisionFaceLandmark *cheekRight =
213-
[face landmarkOfType:FIRFaceLandmarkTypeRightCheek];
211+
MLKFaceLandmark *cheekRight =
212+
[face landmarkOfType:MLKFaceLandmarkTypeRightCheek];
214213
if (cheekRight != nil) {
215214
[resultDict setObject:[self processPoint:cheekRight.position]
216215
forKey:@"rightCheekPosition"];
217216
}
218217
/** Midpoint between the nostrils where the nose meets the face. */
219-
FIRVisionFaceLandmark *noseBase =
220-
[face landmarkOfType:FIRFaceLandmarkTypeNoseBase];
218+
MLKFaceLandmark *noseBase =
219+
[face landmarkOfType:MLKFaceLandmarkTypeNoseBase];
221220
if (noseBase != nil) {
222221
[resultDict setObject:[self processPoint:noseBase.position]
223222
forKey:@"noseBasePosition"];
@@ -256,10 +255,10 @@ - (NSDictionary *)processBounds:(CGRect)bounds
256255
return boundsDict;
257256
}
258257

259-
- (NSDictionary *)processPoint:(FIRVisionPoint *)point
258+
- (NSDictionary *)processPoint:(MLKVisionPoint *)point
260259
{
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;
263262
NSDictionary *pointDict = @{
264263

265264
@"x" : @(originX),

ios/RN/RNCameraManager.m

+2-4
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ + (NSDictionary *)pictureSizes
165165

166166
+ (NSDictionary *)faceDetectorConstants
167167
{
168-
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
168+
#if __has_include(<MLKitFaceDetection/MLKitFaceDetection.h>)
169169
return [FaceDetectorManagerMlkit constants];
170170
#else
171171
return [NSDictionary new];
@@ -174,7 +174,7 @@ + (NSDictionary *)faceDetectorConstants
174174

175175
+ (NSDictionary *)barcodeDetectorConstants
176176
{
177-
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
177+
#if __has_include(<MLKitBarcodeScanning/MLKitBarcodeScanning.h>)
178178
return [BarcodeDetectorManagerMlkit constants];
179179
#else
180180
return [NSDictionary new];
@@ -304,7 +304,6 @@ + (NSDictionary *)barcodeDetectorConstants
304304

305305
RCT_CUSTOM_VIEW_PROPERTY(barCodeScannerEnabled, BOOL, RNCamera)
306306
{
307-
308307
view.isReadingBarCodes = [RCTConvert BOOL:json];
309308
[view setupOrDisableBarcodeScanner];
310309
}
@@ -332,7 +331,6 @@ + (NSDictionary *)barcodeDetectorConstants
332331

333332
RCT_CUSTOM_VIEW_PROPERTY(textRecognizerEnabled, BOOL, RNCamera)
334333
{
335-
336334
view.canReadText = [RCTConvert BOOL:json];
337335
[view setupOrDisableTextDetector];
338336
}

ios/RN/RNFaceDetectorModuleMLKit.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#import "RNFaceDetectorModuleMLKit.h"
2-
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
2+
#if __has_include(<MLKitFaceDetection/MLKitFaceDetection.h>)
33
#import "RNFileSystem.h"
44
#import "RNImageUtils.h"
55

@@ -58,7 +58,7 @@ - (NSDictionary *)constantsToExport
5858
reject(@"E_FACE_DETECTION_FAILED", [NSString stringWithFormat:@"The file does not exist. Given path: `%@`.", path], nil);
5959
return;
6060
}
61-
FIRVisionFaceDetectorOptions *newOptions = [[FIRVisionFaceDetectorOptions alloc] init];
61+
MLKFaceDetectorOptions *newOptions = [[MLKFaceDetectorOptions alloc] init];
6262
if (options[kDetectLandmarksOptionName]) {
6363
newOptions.landmarkMode = [options[kDetectLandmarksOptionName] integerValue];
6464
}

ios/RN/TextDetectorManager.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
2-
#import <FirebaseMLVision/FirebaseMLVision.h>
1+
#if __has_include(<MLKitTextRecognition/MLKitTextRecognition.h>)
2+
@import MLKitTextRecognition;
33
#endif
44
@interface TextDetectorManager : NSObject
55
typedef void(^postRecognitionBlock)(NSArray *textBlocks);

0 commit comments

Comments
 (0)