flutter-ml / google_ml_kit_flutter

A flutter plugin that implements Google's standalone ML Kit
MIT License
996 stars 752 forks source link

iOS crash with "MLKInvalidImage: Input image must not be nil." after upgrading from google_ml_kit 0.7.3 to google_mlkit_barcode_scanning 0.3.0 #290

Closed jbx1 closed 1 year ago

jbx1 commented 2 years ago

Using Camera 0.9.8+1 After switching packages from google_ml_kit: ^0.7.3 to google_mlkit_barcode_scanning: ^0.3.0 and changing the parts of the code that broke, mainly:

Replaced

    final imageRotation =
        InputImageRotationMethods.fromRawValue(camera.sensorOrientation) ??
            InputImageRotation.Rotation_0deg;

  final inputImageFormat =
        InputImageFormatMethods.fromRawValue(image.format.raw) ??
            InputImageFormat.NV21;

with

    final imageRotation = InputImageRotation.rotation0deg;
    final inputImageFormat = InputImageFormat.nv21;

and

    barcodeScanner = GoogleMlKit.vision
        .barcodeScanner([BarcodeFormat.qrCode, BarcodeFormat.aztec]);

with

    barcodeScanner = BarcodeScanner(formats: [BarcodeFormat.qrCode, BarcodeFormat.aztec]);

and

    String barcodeRawValue = barcode?.value?.rawValue;
    String barcodeFormat = barcode?.value?.format?.name;

with

    String barcodeRawValue = barcode?.rawValue;
    String barcodeFormat = barcode?.format?.toString();

It works on Android but on iOS I get a crash, with the following exception.

MLKInvalidImage: Input image must not be nil.

I can't figure out what the problem is. There is no migration guide from 0.7.3 to the new version of MLKit, so I have no idea what might be missing now.

Unfortunately the stacktrace from Sentry isn't too informative (it isn't showing any part of my code).

image

nero-angela commented 2 years ago

I have the same problem sometimes with TextRecognizer.

*** Terminating app due to uncaught exception 'MLKInvalidImage', reason: 'Input image must not be nil.'
*** First throw call stack:
(0x1805fa288 0x1992f4744 0x180651390 0x105aec148 0x105b21a9c 0x105b21918 0x105b22b10 0x105b22658 0x10abf8740 0x10a6dfa08 0x18025fe6c 0x180261a30 0x18026ff48 0x18026fb98 0x1805b2800 0x18056c704 0x18057fbc8 0x19c6b3374 0x182eef648 0x182c70d90 0x104658678 0x10732dce4)
libc++abi: terminating with uncaught exception of type NSException
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x00000001b74dfb38 libsystem_kernel.dylib`__pthread_kill + 8
libsystem_kernel.dylib`__pthread_kill:
->  0x1b74dfb38 <+8>:  b.lo   0x1b74dfb58               ; <+40>
    0x1b74dfb3c <+12>: pacibsp 
    0x1b74dfb40 <+16>: stp    x29, x30, [sp, #-0x10]!
    0x1b74dfb44 <+20>: mov    x29, sp
Target 0: (Runner) stopped.
Lost connection to device.
fbernaly commented 2 years ago

@jbx1 : you are hardcoding the values for imageRotation and inputImageFormat, try this:

    final imageRotation =
        InputImageRotationValue.fromRawValue(camera.sensorOrientation);
    if (imageRotation == null) return;

    final inputImageFormat =
        InputImageFormatValue.fromRawValue(image.format.raw);
    if (inputImageFormat == null) return;
doc-rj-ebay commented 2 years ago

@fbernaly We're occasionally seeing the same crash signature in Crashlytics, using the following:

  google_mlkit_barcode_scanning: ^0.4.0
  google_mlkit_image_labeling: ^0.4.0
  google_mlkit_text_recognition: ^0.4.0

It appears to be crashing in ImageLabeling (stack trace below). We also see some related logging before the crash:

Bad state: LocalFile: '/var/mobile/Containers/Data/Application/E00088FD-418C-465F-BD90-EE2D3DEA762E/Documents/img12_1662733618579.jpg' is empty and cannot be loaded as an image.

Stack trace:

Fatal Exception: MLKInvalidImage
Input image must not be nil.

Fatal Exception: MLKInvalidImage
0  CoreFoundation                 0x99288 __exceptionPreprocess
1  libobjc.A.dylib                0x16744 objc_exception_throw
2  CoreFoundation                 0xf0390 __CFDictionaryCreateGeneric
3  Runner                         0x20bf100 -[MLKVisionImage initWithImage:]
4  Runner                         0x33e40d4 +[MLKVisionImage(FlutterPlugin) filePathToVisionImage:] + 22 (MLKVisionImage+FlutterPlugin.m:22)
5  Runner                         0x33e3fb0 +[MLKVisionImage(FlutterPlugin) visionImageFromData:] + 9 (MLKVisionImage+FlutterPlugin.m:9)
6  Runner                         0x33e4c04 -[GoogleMlKitImageLabelingPlugin handleDetection:result:] + 56 (GoogleMlKitImageLabelingPlugin.m:56)
7  Runner                         0x33e49f4 -[GoogleMlKitImageLabelingPlugin handleMethodCall:result:] + 39 (GoogleMlKitImageLabelingPlugin.m:39)
8  Flutter                        0x55ae50 (Missing UUID c8164c661c033398832ad3cbdb9a3cd8)
9  Flutter                        0x42cd0 (Missing UUID c8164c661c033398832ad3cbdb9a3cd8)
10 libdispatch.dylib              0x1e6c _dispatch_call_block_and_release
11 libdispatch.dylib              0x3a30 _dispatch_client_callout
12 libdispatch.dylib              0x11f48 _dispatch_main_queue_drain
13 libdispatch.dylib              0x11b98 _dispatch_main_queue_callback_4CF
14 CoreFoundation                 0x51800 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
15 CoreFoundation                 0xb704 __CFRunLoopRun
16 CoreFoundation                 0x1ebc8 CFRunLoopRunSpecific
17 GraphicsServices               0x1374 GSEventRunModal
18 UIKitCore                      0x514b58 -[UIApplication _run]
19 UIKitCore                      0x296090 UIApplicationMain
20 Runner                         0x7f2c main + 7 (AppDelegate.swift:7)
21 ???                            0x109069da4 (Missing)
prachishukla007 commented 3 months ago

I'm facing the same issue with google_mlkit_flutter_face_detection, here is the crash report

Terminating app due to uncaught exception 'MLKInvalidImage', reason: 'Input image must not be nil.' First throw call stack: ( 0 CoreFoundation 0x00007ff8004cd531 exceptionPreprocess + 242 1 libobjc.A.dylib 0x00007ff8000837e8 objc_exception_throw + 48 2 CoreFoundation 0x00007ff8004cd40f -[NSException initWithCoder:] + 0 3 Runner 0x00000001054d7ef9 -[MLKVisionImage initWithImage:] + 76 4 Runner 0x00000001054e27dd +[MLKVisionImage(FlutterPlugin) pixelBufferToVisionImage:] + 397 5 Runner 0x00000001054e2545 +[MLKVisionImage(FlutterPlugin) bytesToVisionImage:] + 549 6 Runner 0x00000001054e2161 +[MLKVisionImage(FlutterPlugin) visionImageFromData:] + 273 7 Runner 0x00000001054e3029 -[GoogleMlKitFaceDetectionPlugin handleDetection:result:] + 185 8 Runner 0x00000001054e2abf -[GoogleMlKitFaceDetectionPlugin handleMethodCall:result:] + 175 9 Flutter 0x000000010d383fd0 45-[FlutterMethodChannel setMethodCallHandler:]_block_invoke + 168 10 Flutter 0x000000010cd3b0e1 _ZN7flutter25PlatformMessageHandlerIos21HandlePlatformMessageENSt3_fl10unique_ptrINS_15PlatformMessageENS1_14default_deleteIS3_EEEE_block_invoke + 94 11 libdispatch.dylib 0x000000010715c29d _dispatch_call_block_and_release + 12 12 libdispatch.dylib 0x000000010715d58f _dispatch_client_callout + 8 13 libdispatch.dylib 0x000000010716d3ee _dispatch_main_queue_drain + 1362 14 libdispatch.dylib 0x000000010716ce8e _dispatch_main_queue_callback_4CF + 31 15 CoreFoundation 0x00007ff800429af4 CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 9 16 CoreFoundation 0x00007ff80042442f CFRunLoopRun + 2463 17 CoreFoundation 0x00007ff8004236ad CFRunLoopRunSpecific + 557 18 GraphicsServices 0x00007ff8103da08f GSEventRunModal + 137 19 UIKitCore 0x00007ff805cc0ad1 -[UIApplication _run] + 972 20 UIKitCore 0x00007ff805cc5551 UIApplicationMain + 123 21 Runner 0x00000001044d946f main + 63 22 dyld 0x0000000105f3a3e0 start_sim + 10 23 ??? 0x000000010cc20366 0x0 + 4509008742 ) libc++abi: terminating due to uncaught exception of type NSException

Whereas, my InputImage isn't nil

final inputImage = InputImage.fromBytes( bytes: imageBytes, metadata: InputImageMetadata( rotation: InputImageRotation.rotation0deg, format: InputImageFormat.nv21, size: Size(640, 480), bytesPerRow: 640, ), );

print('***${inputImage.metadata?.format}****');

// Initialize the face detector final faceDetector = FaceDetector( options: FaceDetectorOptions(performanceMode: FaceDetectorMode.accurate));

print('***${faceDetector.id}****');

// Process the image and detect faces final faces = await faceDetector.processImage(inputImage);

By debugging I found that this issue is coming while executing this line, final faces = await faceDetector.processImage(inputImage);

DO WE HAVE ANY SOLUTION?? :(

andresgd7 commented 3 months ago

Any news about this??