Skip to content

Commit

Permalink
Revert "Update ios MLKit Text Recognition to '5.6.0' (flutter#733)"
Browse files Browse the repository at this point in the history
This reverts commit e831bfb.
  • Loading branch information
andreidiaconu authored Feb 17, 2019
1 parent ef219e8 commit 88ec171
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
@interface LabelDetector : NSObject<Detector>
@end

@interface TextRecognizer : NSObject<Detector>
@interface TextDetector : NSObject<Detector>
@end
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
[FaceDetector handleDetection:image options:options result:result];
} else if ([@"LabelDetector#detectInImage" isEqualToString:call.method]) {
[LabelDetector handleDetection:image options:options result:result];
} else if ([@"TextRecognizer#detectInImage" isEqualToString:call.method]) {
[TextRecognizer handleDetection:image options:options result:result];
} else if ([@"TextDetector#detectInImage" isEqualToString:call.method]) {
[TextDetector handleDetection:image options:options result:result];
} else {
result(FlutterMethodNotImplemented);
}
Expand Down
116 changes: 116 additions & 0 deletions packages/firebase_ml_vision/ios/Classes/TextDetector.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#import "FirebaseMlVisionPlugin.h"

@implementation TextDetector
static FIRVisionTextDetector *textDetector;

+ (void)handleDetection:(FIRVisionImage *)image
options:(NSDictionary *)options
result:(FlutterResult)result {
if (textDetector == nil) {
FIRVision *vision = [FIRVision vision];
textDetector = [vision textDetector];
}

[textDetector
detectInImage:image
completion:^(NSArray<id<FIRVisionText>> *_Nullable features, NSError *_Nullable error) {
if (error) {
[FLTFirebaseMlVisionPlugin handleError:error result:result];
return;
} else if (!features) {
result(@[]);
return;
}

NSMutableArray *blocks = [NSMutableArray array];
for (id<FIRVisionText> feature in features) {
NSMutableDictionary *blockData = [NSMutableDictionary dictionary];
if ([feature isKindOfClass:[FIRVisionTextBlock class]]) {
FIRVisionTextBlock *block = (FIRVisionTextBlock *)feature;

[blockData addEntriesFromDictionary:[self getTextData:block.frame
cornerPoints:block.cornerPoints
text:block.text]];
blockData[@"lines"] = [self getLineData:block.lines];
} else if ([feature isKindOfClass:[FIRVisionTextLine class]]) {
// We structure the return data to have the line be inside a FIRVisionTextBlock.
FIRVisionTextLine *line = (FIRVisionTextLine *)feature;

[blockData addEntriesFromDictionary:[self getTextData:line.frame
cornerPoints:line.cornerPoints
text:line.text]];
NSArray<FIRVisionTextLine *> *lines = @[ line ];
blockData[@"lines"] = [self getLineData:lines];
} else if ([feature isKindOfClass:[FIRVisionTextElement class]]) {
// We structure the return data to have the element inside a FIRVisionTextLine
// that is inside a FIRVisionTextBlock.
FIRVisionTextElement *element = (FIRVisionTextElement *)feature;

[blockData addEntriesFromDictionary:[self getTextData:element.frame
cornerPoints:element.cornerPoints
text:element.text]];

NSMutableDictionary *lineData = [NSMutableDictionary dictionary];
[lineData addEntriesFromDictionary:[self getTextData:element.frame
cornerPoints:element.cornerPoints
text:element.text]];

NSArray<FIRVisionTextElement *> *elements = @[ element ];
lineData[@"elements"] = [self getElementData:elements];

blockData[@"lines"] = lineData;
}

[blocks addObject:blockData];
}

result(blocks);
}];
}

+ (NSDictionary *)getTextData:(CGRect)frame
cornerPoints:(NSArray<NSValue *> *)cornerPoints
text:(NSString *)text {
__block NSMutableArray<NSArray *> *points = [NSMutableArray array];

for (NSValue *point in points) {
[points addObject:@[ @(((__bridge CGPoint *)point)->x), @(((__bridge CGPoint *)point)->y) ]];
}

return @{
@"text" : text,
@"left" : @((int)frame.origin.x),
@"top" : @((int)frame.origin.y),
@"width" : @((int)frame.size.width),
@"height" : @((int)frame.size.height),
@"points" : points,
};
}

+ (NSMutableArray *)getLineData:(NSArray<FIRVisionTextLine *> *)lines {
NSMutableArray *lineDataArray = [NSMutableArray array];

for (FIRVisionTextLine *line in lines) {
NSMutableDictionary *lineData = [NSMutableDictionary dictionary];
[lineData addEntriesFromDictionary:[self getTextData:line.frame
cornerPoints:line.cornerPoints
text:line.text]];
lineData[@"elements"] = [self getElementData:line.elements];
[lineDataArray addObject:lineData];
}

return lineDataArray;
}

+ (NSMutableArray *)getElementData:(NSArray<FIRVisionTextElement *> *)elements {
NSMutableArray *elementDataArray = [NSMutableArray array];

for (FIRVisionTextElement *element in elements) {
[elementDataArray addObject:[self getTextData:element.frame
cornerPoints:element.cornerPoints
text:element.text]];
}

return elementDataArray;
}
@end
102 changes: 0 additions & 102 deletions packages/firebase_ml_vision/ios/Classes/TextRecognizer.m

This file was deleted.

10 changes: 5 additions & 5 deletions packages/firebase_ml_vision/ios/firebase_ml_vision.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ An SDK that brings Google's machine learning expertise to Android and iOS apps i
s.public_header_files = 'Classes/**/*.h'
s.dependency 'Flutter'
s.dependency 'Firebase/Core'
s.dependency 'Firebase/MLVision', '5.6.0'
s.dependency 'Firebase/MLVisionBarcodeModel', '5.6.0'
s.dependency 'Firebase/MLVisionFaceModel', '5.6.0'
s.dependency 'Firebase/MLVisionLabelModel', '5.6.0'
s.dependency 'Firebase/MLVisionTextModel', '5.6.0'
s.dependency 'Firebase/MLVision', '5.0.1'
s.dependency 'Firebase/MLVisionBarcodeModel', '5.0.1'
s.dependency 'Firebase/MLVisionFaceModel', '5.0.1'
s.dependency 'Firebase/MLVisionLabelModel', '5.0.1'
s.dependency 'Firebase/MLVisionTextModel', '5.0.1'
s.ios.deployment_target = '8.0'
s.static_framework = true
end

0 comments on commit 88ec171

Please sign in to comment.