Зрение Flutter ML kit не распознает лица - PullRequest
0 голосов
/ 28 апреля 2020

В моем проекте я использую библиотеку firebase_ml_vision 0.9.3+8, и теперь эта библиотека не распознает лица.

Мой код, который я использую в проекте

Инициализация камеры:

CameraController _camera;
Detector _currentDetector = Detector.face;
CameraLensDirection _direction = CameraLensDirection.front;
final FaceDetector _faceDetector = FirebaseVision.instance.faceDetector(
      FaceDetectorOptions(enableLandmarks: true, enableContours: true));

Future<dynamic> Function(FirebaseVisionImage image) _getDetectionMethod() {
    return _faceDetector.processImage;
  }

Future<void> _initializeCamera() async {
    final CameraDescription description =
        await Facekeypoint.getCamera(_direction);
    _camera = CameraController(
      description,
      defaultTargetPlatform == TargetPlatform.iOS
          ? ResolutionPreset.low
          : ResolutionPreset.medium,
    );
    await _camera.initialize();
    await Future.delayed(Duration(milliseconds: 200));

    _camera.startImageStream((CameraImage image) {
      if (_isDetecting) return;

      _isDetecting = true;
      counter++;
      Facekeypoint.detect(
        image: image,
        detectInImage: _getDetectionMethod(),
        imageRotation: description.sensorOrientation,
      ).then(
        (dynamic results) {
          if (_currentDetector == null) return;
          setState(() {
            _scanResults = results;
          });
        },
      ).whenComplete(() => _isDetecting = false);
    });

    if (_camera != null && _camera.value.isInitialized) {
      fullHeight = MediaQuery.of(context).size.height -
          (MediaQuery.of(context).size.height * _camera.value.aspectRatio);
    } else {
      fullHeight = null;
    }
  }

Класс FaceKeyPoint:

class Facekeypoint {
  static const MethodChannel _channel = const MethodChannel('facekeypoint');

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  static Future<CameraDescription> getCamera(CameraLensDirection dir) async {
    return await availableCameras().then(
      (List<CameraDescription> cameras) => cameras.firstWhere(
        (CameraDescription camera) => camera.lensDirection == dir,
      ),
    );
  }

  static Future<dynamic> detect({
    @required CameraImage image,
    @required Future<dynamic> Function(FirebaseVisionImage image) detectInImage,
    @required int imageRotation,
  }) async {
    return detectInImage(
      FirebaseVisionImage.fromBytes(
        _concatenatePlanes(image.planes),
        _buildMetaData(image, _rotationIntToImageRotation(imageRotation)),
      ),
    );
  }

  static Uint8List _concatenatePlanes(List<Plane> planes) {
    final WriteBuffer allBytes = WriteBuffer();
    for (Plane plane in planes) {
      allBytes.putUint8List(plane.bytes);
    }
    return allBytes.done().buffer.asUint8List();
  }

  static FirebaseVisionImageMetadata _buildMetaData(
    CameraImage image,
    ImageRotation rotation,
  ) {
    return FirebaseVisionImageMetadata(
      rawFormat: image.format.raw,
      size: Size(image.width.toDouble(), image.height.toDouble()),
      rotation: rotation,
      planeData: image.planes.map(
        (Plane plane) {
          return FirebaseVisionImagePlaneMetadata(
            bytesPerRow: plane.bytesPerRow,
            height: plane.height,
            width: plane.width,
          );
        },
      ).toList(),
    );
  }

  static ImageRotation _rotationIntToImageRotation(int rotation) {
    switch (rotation) {
      case 0:
        return ImageRotation.rotation0;
      case 90:
        return ImageRotation.rotation90;
      case 180:
        return ImageRotation.rotation180;
      default:
        assert(rotation == 270);
        return ImageRotation.rotation270;
    }
  }
}

Поиск в Google мне не помог. В начале я пытаюсь изменить версию библиотеки (понизить версию, обновить), это не поможет мне. Но _scanResalts всегда возвращает пустой список. Извините за мой английский sh, кто-нибудь может помочь?

...