Skip to content

Commit

Permalink
feat: migrate to google ML Kit (react-native-camera#3241) [skip ci]
Browse files Browse the repository at this point in the history
Co-authored-by: Mike Duminy <[email protected]>
  • Loading branch information
mikeduminy and mikeduminy authored Jul 15, 2021
1 parent 0359c32 commit 670b973
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 151 deletions.
4 changes: 2 additions & 2 deletions ios/RN/BarcodeDetectorManagerMlkit.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#import <UIKit/UIKit.h>
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
#import <FirebaseMLVision/FirebaseMLVision.h>
#if __has_include(<MLKitBarcodeScanning/MLKitBarcodeScanning.h>)
@import MLKitBarcodeScanning;
#endif

@interface BarcodeDetectorManagerMlkit : NSObject
Expand Down
139 changes: 69 additions & 70 deletions ios/RN/BarcodeDetectorManagerMlkit.m

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions ios/RN/FaceDetectorManagerMlkit.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@

#import <UIKit/UIKit.h>
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
#import <FirebaseMLVision/FirebaseMLVision.h>
#if __has_include(<MLKitFaceDetection/MLKitFaceDetection.h>)
@import MLKitFaceDetection;

typedef NS_ENUM(NSInteger, RNFaceDetectionMode) {
RNFaceDetectionFastMode = FIRVisionFaceDetectorPerformanceModeFast,
RNFaceDetectionAccurateMode = FIRVisionFaceDetectorPerformanceModeAccurate
RNFaceDetectionFastMode = MLKFaceDetectorPerformanceModeFast,
RNFaceDetectionAccurateMode = MLKFaceDetectorPerformanceModeAccurate
};

typedef NS_ENUM(NSInteger, RNFaceDetectionLandmarks) {
RNFaceDetectAllLandmarks = FIRVisionFaceDetectorLandmarkModeAll,
RNFaceDetectNoLandmarks = FIRVisionFaceDetectorLandmarkModeNone
RNFaceDetectAllLandmarks = MLKFaceDetectorLandmarkModeAll,
RNFaceDetectNoLandmarks = MLKFaceDetectorLandmarkModeNone
};

typedef NS_ENUM(NSInteger, RNFaceDetectionClassifications) {
RNFaceRunAllClassifications = FIRVisionFaceDetectorClassificationModeAll,
RNFaceRunNoClassifications = FIRVisionFaceDetectorClassificationModeNone
RNFaceRunAllClassifications = MLKFaceDetectorClassificationModeAll,
RNFaceRunNoClassifications = MLKFaceDetectorClassificationModeNone
};

#endif

@interface FaceDetectorManagerMlkit : NSObject
Expand Down
81 changes: 40 additions & 41 deletions ios/RN/FaceDetectorManagerMlkit.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#import "FaceDetectorManagerMlkit.h"
#import <React/RCTConvert.h>
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
#if __has_include(<MLKitFaceDetection/MLKitFaceDetection.h>)
@import MLKitVision;

@interface FaceDetectorManagerMlkit ()
@property(nonatomic, strong) FIRVisionFaceDetector *faceRecognizer;
@property(nonatomic, strong) FIRVision *vision;
@property(nonatomic, strong) FIRVisionFaceDetectorOptions *options;
@property(nonatomic, strong) MLKFaceDetector *faceRecognizer;
@property(nonatomic, strong) MLKFaceDetectorOptions *options;
@property(nonatomic, assign) float scaleX;
@property(nonatomic, assign) float scaleY;
@end
Expand All @@ -15,13 +15,12 @@ @implementation FaceDetectorManagerMlkit
- (instancetype)init
{
if (self = [super init]) {
self.options = [[FIRVisionFaceDetectorOptions alloc] init];
self.options.performanceMode = FIRVisionFaceDetectorPerformanceModeFast;
self.options.landmarkMode = FIRVisionFaceDetectorLandmarkModeNone;
self.options.classificationMode = FIRVisionFaceDetectorClassificationModeNone;
self.options = [[MLKFaceDetectorOptions alloc] init];
self.options.performanceMode = MLKFaceDetectorPerformanceModeFast;
self.options.landmarkMode = MLKFaceDetectorLandmarkModeNone;
self.options.classificationMode = MLKFaceDetectorClassificationModeNone;

self.vision = [FIRVision vision];
self.faceRecognizer = [_vision faceDetectorWithOptions:_options];
self.faceRecognizer = [MLKFaceDetector faceDetectorWithOptions:_options];
}
return self;
}
Expand Down Expand Up @@ -57,7 +56,7 @@ - (void)setTracking:(id)json queue:(dispatch_queue_t)sessionQueue
dispatch_async(sessionQueue, ^{
self.options.trackingEnabled = requestedValue;
self.faceRecognizer =
[self.vision faceDetectorWithOptions:self.options];
[MLKFaceDetector faceDetectorWithOptions:self.options];
});
}
}
Expand All @@ -71,7 +70,7 @@ - (void)setLandmarksMode:(id)json queue:(dispatch_queue_t)sessionQueue
dispatch_async(sessionQueue, ^{
self.options.landmarkMode = requestedValue;
self.faceRecognizer =
[self.vision faceDetectorWithOptions:self.options];
[MLKFaceDetector faceDetectorWithOptions:self.options];
});
}
}
Expand All @@ -85,7 +84,7 @@ - (void)setPerformanceMode:(id)json queue:(dispatch_queue_t)sessionQueue
dispatch_async(sessionQueue, ^{
self.options.performanceMode = requestedValue;
self.faceRecognizer =
[self.vision faceDetectorWithOptions:self.options];
[MLKFaceDetector faceDetectorWithOptions:self.options];
});
}
}
Expand All @@ -99,7 +98,7 @@ - (void)setClassificationMode:(id)json queue:(dispatch_queue_t)sessionQueue
dispatch_async(sessionQueue, ^{
self.options.classificationMode = requestedValue;
self.faceRecognizer =
[self.vision faceDetectorWithOptions:self.options];
[MLKFaceDetector faceDetectorWithOptions:self.options];
});
}
}
Expand All @@ -112,11 +111,11 @@ - (void)findFacesInFrame:(UIImage *)uiImage
{
self.scaleX = scaleX;
self.scaleY = scaleY;
FIRVisionImage *image = [[FIRVisionImage alloc] initWithImage:uiImage];
MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:uiImage];
NSMutableArray *emptyResult = [[NSMutableArray alloc] init];
[_faceRecognizer
processImage:image
completion:^(NSArray<FIRVisionFace *> *faces, NSError *error) {
processImage:visionImage
completion:^(NSArray<MLKFace *> *faces, NSError *error) {
if (error != nil || faces == nil) {
completed(emptyResult);
} else {
Expand All @@ -128,7 +127,7 @@ - (void)findFacesInFrame:(UIImage *)uiImage
- (NSArray *)processFaces:(NSArray *)faces
{
NSMutableArray *result = [[NSMutableArray alloc] init];
for (FIRVisionFace *face in faces) {
for (MLKFace *face in faces) {
NSMutableDictionary *resultDict =
[[NSMutableDictionary alloc] initWithCapacity:20];
// Boundaries of face in image
Expand All @@ -153,71 +152,71 @@ - (NSArray *)processFaces:(NSArray *)faces
// If landmark detection was enabled (mouth, ears, eyes, cheeks, and
// nose available):
/** Midpoint of the left ear tip and left ear lobe. */
FIRVisionFaceLandmark *leftEar =
[face landmarkOfType:FIRFaceLandmarkTypeLeftEar];
MLKFaceLandmark *leftEar =
[face landmarkOfType:MLKFaceLandmarkTypeLeftEar];
if (leftEar != nil) {
[resultDict setObject:[self processPoint:leftEar.position]
forKey:@"leftEarPosition"];
}
/** Midpoint of the right ear tip and right ear lobe. */
FIRVisionFaceLandmark *rightEar =
[face landmarkOfType:FIRFaceLandmarkTypeRightEar];
MLKFaceLandmark *rightEar =
[face landmarkOfType:MLKFaceLandmarkTypeRightEar];
if (rightEar != nil) {
[resultDict setObject:[self processPoint:rightEar.position]
forKey:@"rightEarPosition"];
}
/** Center of the bottom lip. */
FIRVisionFaceLandmark *mouthBottom =
[face landmarkOfType:FIRFaceLandmarkTypeMouthBottom];
MLKFaceLandmark *mouthBottom =
[face landmarkOfType:MLKFaceLandmarkTypeMouthBottom];
if (mouthBottom != nil) {
[resultDict setObject:[self processPoint:mouthBottom.position]
forKey:@"bottomMouthPosition"];
}
/** Right corner of the mouth */
FIRVisionFaceLandmark *mouthRight =
[face landmarkOfType:FIRFaceLandmarkTypeMouthRight];
MLKFaceLandmark *mouthRight =
[face landmarkOfType:MLKFaceLandmarkTypeMouthRight];
if (mouthRight != nil) {
[resultDict setObject:[self processPoint:mouthRight.position]
forKey:@"rightMouthPosition"];
}
/** Left corner of the mouth */
FIRVisionFaceLandmark *mouthLeft =
[face landmarkOfType:FIRFaceLandmarkTypeMouthLeft];
MLKFaceLandmark *mouthLeft =
[face landmarkOfType:MLKFaceLandmarkTypeMouthLeft];
if (mouthLeft != nil) {
[resultDict setObject:[self processPoint:mouthLeft.position]
forKey:@"leftMouthPosition"];
}
/** Left eye. */
FIRVisionFaceLandmark *eyeLeft =
[face landmarkOfType:FIRFaceLandmarkTypeLeftEye];
MLKFaceLandmark *eyeLeft =
[face landmarkOfType:MLKFaceLandmarkTypeLeftEye];
if (eyeLeft != nil) {
[resultDict setObject:[self processPoint:eyeLeft.position]
forKey:@"leftEyePosition"];
}
/** Right eye. */
FIRVisionFaceLandmark *eyeRight =
[face landmarkOfType:FIRFaceLandmarkTypeRightEye];
MLKFaceLandmark *eyeRight =
[face landmarkOfType:MLKFaceLandmarkTypeRightEye];
if (eyeRight != nil) {
[resultDict setObject:[self processPoint:eyeRight.position]
forKey:@"rightEyePosition"];
}
/** Left cheek. */
FIRVisionFaceLandmark *cheekLeft =
[face landmarkOfType:FIRFaceLandmarkTypeLeftCheek];
MLKFaceLandmark *cheekLeft =
[face landmarkOfType:MLKFaceLandmarkTypeLeftCheek];
if (cheekLeft != nil) {
[resultDict setObject:[self processPoint:cheekLeft.position]
forKey:@"leftCheekPosition"];
}
/** Right cheek. */
FIRVisionFaceLandmark *cheekRight =
[face landmarkOfType:FIRFaceLandmarkTypeRightCheek];
MLKFaceLandmark *cheekRight =
[face landmarkOfType:MLKFaceLandmarkTypeRightCheek];
if (cheekRight != nil) {
[resultDict setObject:[self processPoint:cheekRight.position]
forKey:@"rightCheekPosition"];
}
/** Midpoint between the nostrils where the nose meets the face. */
FIRVisionFaceLandmark *noseBase =
[face landmarkOfType:FIRFaceLandmarkTypeNoseBase];
MLKFaceLandmark *noseBase =
[face landmarkOfType:MLKFaceLandmarkTypeNoseBase];
if (noseBase != nil) {
[resultDict setObject:[self processPoint:noseBase.position]
forKey:@"noseBasePosition"];
Expand Down Expand Up @@ -256,10 +255,10 @@ - (NSDictionary *)processBounds:(CGRect)bounds
return boundsDict;
}

- (NSDictionary *)processPoint:(FIRVisionPoint *)point
- (NSDictionary *)processPoint:(MLKVisionPoint *)point
{
float originX = [point.x floatValue] * _scaleX;
float originY = [point.y floatValue] * _scaleY;
float originX = point.x * _scaleX;
float originY = point.y * _scaleY;
NSDictionary *pointDict = @{

@"x" : @(originX),
Expand Down
6 changes: 2 additions & 4 deletions ios/RN/RNCameraManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ + (NSDictionary *)pictureSizes

+ (NSDictionary *)faceDetectorConstants
{
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
#if __has_include(<MLKitFaceDetection/MLKitFaceDetection.h>)
return [FaceDetectorManagerMlkit constants];
#else
return [NSDictionary new];
Expand All @@ -174,7 +174,7 @@ + (NSDictionary *)faceDetectorConstants

+ (NSDictionary *)barcodeDetectorConstants
{
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
#if __has_include(<MLKitBarcodeScanning/MLKitBarcodeScanning.h>)
return [BarcodeDetectorManagerMlkit constants];
#else
return [NSDictionary new];
Expand Down Expand Up @@ -304,7 +304,6 @@ + (NSDictionary *)barcodeDetectorConstants

RCT_CUSTOM_VIEW_PROPERTY(barCodeScannerEnabled, BOOL, RNCamera)
{

view.isReadingBarCodes = [RCTConvert BOOL:json];
[view setupOrDisableBarcodeScanner];
}
Expand Down Expand Up @@ -332,7 +331,6 @@ + (NSDictionary *)barcodeDetectorConstants

RCT_CUSTOM_VIEW_PROPERTY(textRecognizerEnabled, BOOL, RNCamera)
{

view.canReadText = [RCTConvert BOOL:json];
[view setupOrDisableTextDetector];
}
Expand Down
4 changes: 2 additions & 2 deletions ios/RN/RNFaceDetectorModuleMLKit.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "RNFaceDetectorModuleMLKit.h"
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
#if __has_include(<MLKitFaceDetection/MLKitFaceDetection.h>)
#import "RNFileSystem.h"
#import "RNImageUtils.h"

Expand Down Expand Up @@ -58,7 +58,7 @@ - (NSDictionary *)constantsToExport
reject(@"E_FACE_DETECTION_FAILED", [NSString stringWithFormat:@"The file does not exist. Given path: `%@`.", path], nil);
return;
}
FIRVisionFaceDetectorOptions *newOptions = [[FIRVisionFaceDetectorOptions alloc] init];
MLKFaceDetectorOptions *newOptions = [[MLKFaceDetectorOptions alloc] init];
if (options[kDetectLandmarksOptionName]) {
newOptions.landmarkMode = [options[kDetectLandmarksOptionName] integerValue];
}
Expand Down
4 changes: 2 additions & 2 deletions ios/RN/TextDetectorManager.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#if __has_include(<FirebaseMLVision/FirebaseMLVision.h>)
#import <FirebaseMLVision/FirebaseMLVision.h>
#if __has_include(<MLKitTextRecognition/MLKitTextRecognition.h>)
@import MLKitTextRecognition;
#endif
@interface TextDetectorManager : NSObject
typedef void(^postRecognitionBlock)(NSArray *textBlocks);
Expand Down
Loading

0 comments on commit 670b973

Please sign in to comment.