Google MLKit在iOS端无法检测票据问题求助
iOS端MLKit自定义目标检测无法识别票据(Android端正常)
- 作为Google MLKit新手,我正在开发跨平台(安卓+iOS)的票据/收据检测功能,采用**目标检测(Object Detection)**技术,使用Google的Mobile Object Labeler V1模型,通过react-native-vision-camera处理检测结果。
- Android端用Java实现的逻辑完全正常,能稳定检测到票据;但iOS端使用逻辑一致的Objective-C代码时,始终无法检测到目标票据:日志输出的检测到的物体数量几乎为0,仅极少数情况会识别出键盘。
- 过去4天尝试过多种方案:更换其他TFLite模型、改用异步检测接口、检测前调整图片尺寸等,但问题依旧。
iOS端核心代码
#import <Foundation/Foundation.h> #import <VisionCamera/FrameProcessorPlugin.h> #import <VisionCamera/Frame.h> #import <MLKit.h> @interface VisionScanObjectsFrameProcessorPlugin : NSObject + (MLKObjectDetector*) objectDetector; @end @implementation VisionScanObjectsFrameProcessorPlugin + (MLKObjectDetector*) objectDetector { static MLKObjectDetector* objectDetector = nil; if (objectDetector == nil) { NSString *path = [[NSBundle mainBundle] pathForResource:@"lite-model_object_detection_mobile_object_labeler_v1_1" ofType:@"tflite"]; MLKLocalModel *localModel = [[MLKLocalModel alloc] initWithPath:path]; MLKCustomObjectDetectorOptions *options = [[MLKCustomObjectDetectorOptions alloc] initWithLocalModel:localModel]; options.detectorMode = MLKObjectDetectorModeSingleImage; options.shouldEnableClassification = YES; options.classificationConfidenceThreshold = @(0.5); options.maxPerObjectLabelCount = 3; objectDetector = [MLKObjectDetector objectDetectorWithOptions:options]; } return objectDetector; } static inline id scanObjects(Frame* frame, NSArray* arguments) { MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:frame.buffer]; image.orientation = frame.orientation; // <-- TODO: is mirrored? NSError* error; NSArray<MLKObject*>* objects = [[VisionScanObjectsFrameProcessorPlugin objectDetector] resultsInImage:image error:&error]; NSLog(@"Object detected : %ld", objects.count); NSMutableArray* results = [NSMutableArray arrayWithCapacity:objects.count]; for (MLKObject* object in objects) { NSMutableArray* labels = [NSMutableArray arrayWithCapacity:object.labels.count]; for (MLKObjectLabel* label in object.labels) { if (122 == label.index || 188 == label.index || 288 == label.index || 325 == label.index || 357 == label.index || 370 == label.index || 480 == label.index || 510 == label.index || 551 == label.index) { [labels addObject:@{ @"index": [NSNumber numberWithFloat:label.index], @"label": label.text, @"confidence": [NSNumber numberWithFloat:label.confidence] }]; } } if (labels.count != 0) { [results addObject:@{ @"width": [NSNumber numberWithFloat:object.frame.size.width], @"height": [NSNumber numberWithFloat:object.frame.size.height], @"top": [NSNumber numberWithFloat:object.frame.origin.y], @"left": [NSNumber numberWithFloat:object.frame.origin.x], @"frameRotation": [NSNumber numberWithFloat:frame.orientation], @"labels": labels }]; } } return results; } VISION_EXPORT_FRAME_PROCESSOR(scanObjects) @end
内容的提问来源于stack exchange,提问作者Gaylord.P
相关产品推荐
相关产品推荐

