ImageEn, unit iexGoogleApi

TIEGoogleVision.ProcessImage

TIEGoogleVision.ProcessImage


Declaration

function ProcessImage(const Filename: string; SilentFailures: Boolean = False): Boolean; overload;
function ProcessImage(bmp: TIEBitmap; SilentFailures: Boolean = False): Boolean; overload;
function ProcessImage(Stream: TStream; SilentFailures: Boolean = False): Boolean; overload;


Description

Submit an image to the Google Vision web service for processing.
If SilentFailures = False, an exception will be raised on failure. Otherwise the result will be False on failure.

Specify your API key using: GoogleApiKey
Specify which features to detect using: RequestTypes
Specify the delivery size by setting SendImageWidth and SendImageHeight

Then submit, e.g. GoogleApi.ProcessImage( ImageEnView1.IEBitmap );

Access the results via:
CropHintsAnnotation
FaceAnnotations
LabelAnnotations
LandmarkAnnotations
LogoAnnotations
ObjectAnnotations
SafeSearchAnnotation
TextAnnotations
TextContent
WebDetection

Note:
You need an API key that supports the "Google Cloud Vision API". See our instructions for this.
Google recommends that images be scaled for sending by setting SendImageWidth and SendImageHeight
If you are sending a full size image, then the Filename overload will likely give best performance. If you are scaling the image and already have it loaded, then the TIEBitmap is likely best.
ProcessImage() requires Delphi/BCB 10.1 (Berlin) or newer (on older versions you can still parse content by setting ResponseJSON)


Demo

Demo  Demos\InputOutput\GoogleVisionApi\GoogleVisionApi.dpr


Example

Cursor := crHourglass;
lblSubmitting.Visible := True;
GVision := TIEGoogleVision.Create( MY_API_KEY );
try
  // Use recommended delivery size (for faster processing)
  GVision.SendImageWidth  := 640;
  GVision.SendImageHeight := 480;

  GVision.RequestTypes := [iegrWebDetection, iegrSafeSearchDetection, iegrLandmarkDetection];
  GVision.ProcessImage( edtFilename.Text );

  lbxResults.Clear();

  if Length( GVision.WebDetection.bestGuessLabels ) > 0 then
    lbxResults.Items.Add( 'This image is likely to be: ' + GVision.WebDetection.bestGuessLabels[0].webLabel );

  if GVision.HasLandmarkAnnotations then
  begin
    lbxResults.Items.Add( 'This image is the landmark: ' + GVision.LandmarkAnnotations[0].description );
    lbxResults.Items.Add( 'Latitude: '  + LatLongToStr( GVision.LandmarkAnnotations[0].latitude, True ));
    lbxResults.Items.Add( 'Longitude: ' + LatLongToStr( GVision.LandmarkAnnotations[0].longitude, False ));
  end;

  lbxResults.Items.Add( format( 'Found %d matching images on the web', [ GVision.WebDetection.fullMatchingImages.Count ]));
  lbxResults.Items.AddStrings( GVision.WebDetection.fullMatchingImages );

  lbxResults.Items.Add( format( 'Found %d pages on the web containing this image', [ GVision.WebDetection.pagesWithMatchingImages.Count ]));
  lbxResults.Items.AddStrings( GVision.WebDetection.pagesWithMatchingImages );

  if GVision.SafeSearchAnnotation.adult in [ieglLikely, ieglVeryLikely] then
    lbxResults.Items.Add( 'Likely to be an ADULT image' );

  if GVision.SafeSearchAnnotation.violence in [ieglLikely, ieglVeryLikely] then
    lbxResults.Items.Add( 'Likely to be a VIOLENT image' );

finally
  Cursor := crDefault;
  lblSubmitting.Visible := False;
  GVision.Free;
end;