run method

  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 {
    await initializeGoogleVision();

    final Uint8List encodedBytes =
        File(argResults!['image-file']).readAsBytesSync();

    final annotatedResponses = await annotate(encodedBytes.buffer);

    final img.Image decodedBytes = img.decodeImage(encodedBytes)!;

    for (var annotatedResponse in annotatedResponses.responses) {
      // check for faces
      for (var faceAnnotation in annotatedResponse.faceAnnotations) {
        img.drawString(
          decodedBytes,
          'Face - ${faceAnnotation.detectionConfidence}',
          font: img.arial14,
          x: faceAnnotation.boundingPoly.vertices.first.x + 2,
          y: faceAnnotation.boundingPoly.vertices.first.y *
                  decodedBytes.height +
              2,
          color: Util.convertColorNameToImageColor(argResults!['line-color']),
        );

        drawAnnotations(
          vertices: faceAnnotation.boundingPoly.vertices,
          image: decodedBytes,
          color: Util.convertColorNameToImageColor(argResults!['line-color']),
        );
      }

      var textOffset = 0;

      // check for landmarks
      for (var landmarkAnnotation in annotatedResponse.landmarkAnnotations) {
        img.drawString(
          decodedBytes,
          '${landmarkAnnotation.description} - ${landmarkAnnotation.score}',
          font: img.arial14,
          x: landmarkAnnotation.boundingPoly!.vertices.first.x + 4,
          y: landmarkAnnotation.boundingPoly!.vertices.first.y *
                  decodedBytes.height +
              4 +
              textOffset,
          color: Util.convertColorNameToImageColor(argResults!['line-color']),
        );

        // move the text down a bit
        textOffset += 16;

        drawAnnotations(
          vertices: landmarkAnnotation.boundingPoly!.vertices,
          image: decodedBytes,
          color: Util.convertColorNameToImageColor(argResults!['line-color']),
        );
      }

      // check for objects
      for (var localizedObjectAnnotation
          in annotatedResponse.localizedObjectAnnotations) {
        img.drawString(
          decodedBytes,
          '${localizedObjectAnnotation.name} - ${localizedObjectAnnotation.score}',
          font: img.arial14,
          x: (localizedObjectAnnotation
                      .boundingPoly!.normalizedVertices.first.x *
                  decodedBytes.width)
              .toInt(),
          y: (localizedObjectAnnotation
                          .boundingPoly!.normalizedVertices.first.y *
                      decodedBytes.height)
                  .toInt() -
              16,
          color: Util.convertColorNameToImageColor(argResults!['line-color']),
        );

        drawAnnotations(
            vertices: localizedObjectAnnotation.boundingPoly!.vertices,
            image: decodedBytes,
            color:
                Util.convertColorNameToImageColor(argResults!['line-color']),
            isNormalized: true);
      }
    }

    await img.encodeJpgFile(argResults!['output-file'], decodedBytes);
  } on DioException catch (err) {
    throw UsageException('API usage error:', err.usage);
  }
}