run method Null safety

  1. @override
void run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
void run() async {
  try {
    final annotatedResponses = await annotate();

    final image = Image.fromFilePath(argResults!['image-file']);

    //check for faces
    for (var annotatedResponse in annotatedResponses.responses) {
      for (var faceAnnotation in annotatedResponse.faceAnnotations) {
        GoogleVision.drawText(
            image,
            faceAnnotation.boundingPoly.vertices.first.x + 2,
            faceAnnotation.boundingPoly.vertices.first.y * image.height + 2,
            'Face - ${faceAnnotation.detectionConfidence}');
        GoogleVision.drawAnnotations(
            image, faceAnnotation.boundingPoly.vertices);
      }
    }

    //check for people
    for (var annotatedResponse in annotatedResponses.responses) {
      for (var localizedObjectAnnotation
          in annotatedResponse.localizedObjectAnnotations) {
        GoogleVision.drawText(
            image,
            (localizedObjectAnnotation
                        .boundingPoly.normalizedVertices.first.x *
                    image.width)
                .toInt(),
            (localizedObjectAnnotation
                            .boundingPoly.normalizedVertices.first.y *
                        image.height)
                    .toInt() -
                16,
            '${localizedObjectAnnotation.name} - ${localizedObjectAnnotation.score}');

        GoogleVision.drawAnnotationsNormalized(
            image, localizedObjectAnnotation.boundingPoly.normalizedVertices);
      }
    }
    await image.writeAsJpeg(argResults!['output-file']);
  } on DioError catch (err) {
    throw UsageException('API usage error:', err.usage);
  }
}