Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to capture color images while recognizing text #715

Open
guohuan1990 opened this issue Nov 7, 2024 · 1 comment
Open

I want to capture color images while recognizing text #715

guohuan1990 opened this issue Nov 7, 2024 · 1 comment
Labels
Text Recognition Issues corresponding to Text Recognition API

Comments

@guohuan1990
Copy link

Describe your issue. If applicable, add screenshots to help explain your problem.

_controller = CameraController(
camera,
// Set to ResolutionPreset.high. Do NOT set it to ResolutionPreset.max because for some phones does NOT work.
ResolutionPreset.high,
enableAudio: false,
imageFormatGroup: Platform.isAndroid
? ImageFormatGroup.yuv420
: ImageFormatGroup.bgra8888,
);

  InputImage? _inputImageFromCameraImage(CameraImage image) {
if (_controller == null) return null;

// get image rotation
// it is used in android to convert the InputImage from Dart to Java: https://github.com/flutter-ml/google_ml_kit_flutter/blob/master/packages/google_mlkit_commons/android/src/main/java/com/google_mlkit_commons/InputImageConverter.java
// `rotation` is not used in iOS to convert the InputImage from Dart to Obj-C: https://github.com/flutter-ml/google_ml_kit_flutter/blob/master/packages/google_mlkit_commons/ios/Classes/MLKVisionImage%2BFlutterPlugin.m
// in both platforms `rotation` and `camera.lensDirection` can be used to compensate `x` and `y` coordinates on a canvas: https://github.com/flutter-ml/google_ml_kit_flutter/blob/master/packages/example/lib/vision_detector_views/painters/coordinates_translator.dart
final camera = _cameras[_cameraIndex];
final sensorOrientation = camera.sensorOrientation;
// print(
//     'lensDirection: ${camera.lensDirection}, sensorOrientation: $sensorOrientation, ${_controller?.value.deviceOrientation} ${_controller?.value.lockedCaptureOrientation} ${_controller?.value.isCaptureOrientationLocked}');
InputImageRotation? rotation;
if (Platform.isIOS) {
  rotation = InputImageRotationValue.fromRawValue(sensorOrientation);
} else if (Platform.isAndroid) {
  var rotationCompensation =
      _orientations[_controller!.value.deviceOrientation];
  if (rotationCompensation == null) return null;
  if (camera.lensDirection == CameraLensDirection.front) {
    // front-facing
    rotationCompensation = (sensorOrientation + rotationCompensation) % 360;
  } else {
    // back-facing
    rotationCompensation =
        (sensorOrientation - rotationCompensation + 360) % 360;
  }
  rotation = InputImageRotationValue.fromRawValue(rotationCompensation);
  // print('rotationCompensation: $rotationCompensation');
}
if (rotation == null) return null;
// print('final rotation: $rotation');

// get image format
final format = InputImageFormatValue.fromRawValue(image.format.raw);
// validate format depending on platform
// only supported formats:
// * nv21 for Android
// * bgra8888 for iOS
if (format == null ||
    (Platform.isAndroid && format != InputImageFormat.yuv_420_888) ||
    (Platform.isIOS && format != InputImageFormat.bgra8888)) return null;

// since format is constraint to nv21 or bgra8888, both only have one plane
if (image.planes.isEmpty) return null;
final plane = image.planes.first;

// compose InputImage using bytes
return InputImage.fromBytes(
  bytes: plane.bytes,
  metadata: InputImageMetadata(
    size: Size(image.width.toDouble(), image.height.toDouble()),
    rotation: rotation, // used only in Android
    format: InputImageFormat.nv21, // used only in iOS
    bytesPerRow: plane.bytesPerRow, // used only in iOS
  ),
);

}

final recognizedText = await _textRecognizer.processImage(inputImage);

The format does not match at this time. What is the problem?

Steps to reproduce.

  1. recognizing text
  2. get capture color

What is the expected result?

1

Did you try our example app?

Yes

Is it reproducible in the example app?

Yes

Reproducible in which OS?

Android

Flutter/Dart Version?

No response

Plugin Version?

google_mlkit_text_recognition: ^0.14.0

@guohuan1990
Copy link
Author

E/ImageError(12845): java.lang.IllegalArgumentException
E/flutter (12845): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(InputImageConverterError, java.lang.IllegalArgumentException, null, null)
E/flutter (12845): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:648:7)
E/flutter (12845): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
E/flutter (12845):
E/flutter (12845): #2 TextRecognizer.processImage (package:google_mlkit_text_recognition/src/text_recognizer.dart:23:20)
E/flutter (12845):
E/flutter (12845): #3 _TextRecognizerViewState._processImage (package:google_ml_kit_example/vision_detector_views/text_detector_view

@fbernaly fbernaly added the Text Recognition Issues corresponding to Text Recognition API label Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Text Recognition Issues corresponding to Text Recognition API
Projects
None yet
Development

No branches or pull requests

2 participants