Inspecting Images for Sensitive Data

You can use the code samples below to inspect images for sensitive data. You can also feed information to the API using JSON over HTTP.

The DLP API currently supports the following image types: image/jpeg, image/png, image/gif, and image/bmp.

Given an image file as input, the DLP API detects sensitive data in the image. It outputs a JSON file that contains the found InfoTypes, the likelihood of the match, and pixel coordinates for the location of sensitive data found in the image. The top left corner of an image is (0,0).

Node.js

This sample uses the Google Cloud Client Library for Node.js. See the Node.js setup guide for information on setting up Node.js.

// Imports the Google Cloud Data Loss Prevention library
const DLP = require('@google-cloud/dlp');

// Instantiates a client
const dlp = DLP();

// The path to a local file to inspect. Can be a text, JPG, or PNG file.
// const fileName = 'path/to/image.png';

// The minimum likelihood required before returning a match
// const minLikelihood = 'LIKELIHOOD_UNSPECIFIED';

// The maximum number of findings to report (0 = server maximum)
// const maxFindings = 0;

// The infoTypes of information to match
// const infoTypes = [{ name: 'US_MALE_NAME' }, { name: 'US_FEMALE_NAME' }];

// Whether to include the matching string
// const includeQuote = true;

// Construct file data to inspect
const fileItems = [{
  type: mime.lookup(filepath) || 'application/octet-stream',
  data: Buffer.from(fs.readFileSync(filepath)).toString('base64')
}];

// Construct request
const request = {
  inspectConfig: {
    infoTypes: infoTypes,
    minLikelihood: minLikelihood,
    maxFindings: maxFindings,
    includeQuote: includeQuote
  },
  items: fileItems
};

// Run request
dlp.inspectContent(request)
  .then((response) => {
    const findings = response[0].results[0].findings;
    if (findings.length > 0) {
      console.log(`Findings:`);
      findings.forEach((finding) => {
        if (includeQuote) {
          console.log(`\tQuote: ${finding.quote}`);
        }
        console.log(`\tInfo type: ${finding.infoType.name}`);
        console.log(`\tLikelihood: ${finding.likelihood}`);
      });
    } else {
      console.log(`No findings.`);
    }
  })
  .catch((err) => {
    console.log(`Error in inspectFile: ${err.message || err}`);
  });

Java

This sample uses the Google Cloud Client Library for Java.

// Instantiates a client
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
  // The path to a local file to inspect. Can be a text, JPG, or PNG file.
  // fileName = 'path/to/image.png';

  // The minimum likelihood required before returning a match
  // minLikelihood = LIKELIHOOD_UNSPECIFIED;

  // The maximum number of findings to report (0 = server maximum)
  // maxFindings = 0;

  // The infoTypes of information to match
  // infoTypes = ['US_MALE_NAME', 'US_FEMALE_NAME'];

  // Whether to include the matching string
  // includeQuote = true;
  Path path = Paths.get(filePath);

  // detect file mime type, default to application/octet-stream
  String mimeType = URLConnection.guessContentTypeFromName(filePath);
  if (mimeType == null) {
    mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
  }
  if (mimeType == null) {
    mimeType = "application/octet-stream";
  }

  byte[] data = Files.readAllBytes(path);
  ContentItem contentItem =
      ContentItem.newBuilder().setType(mimeType).setData(ByteString.copyFrom(data)).build();

  InspectConfig inspectConfig =
      InspectConfig.newBuilder()
          .addAllInfoTypes(infoTypes)
          .setMinLikelihood(minLikelihood)
          .setMaxFindings(maxFindings)
          .setIncludeQuote(includeQuote)
          .build();

  InspectContentRequest request =
      InspectContentRequest.newBuilder()
          .setInspectConfig(inspectConfig)
          .addItems(contentItem)
          .build();
  InspectContentResponse response = dlpServiceClient.inspectContent(request);

  for (InspectResult result : response.getResultsList()) {
    if (result.getFindingsCount() > 0) {
      System.out.println("Findings: ");
      for (Finding finding : result.getFindingsList()) {
        if (includeQuote) {
          System.out.print("Quote: " + finding.getQuote());
        }
        System.out.print("\tInfo type: " + finding.getInfoType().getName());
        System.out.println("\tLikelihood: " + finding.getLikelihood());
      }
    } else {
      System.out.println("No findings.");
    }
  }
} catch (Exception e) {
  e.printStackTrace();
  System.out.println("Error in inspectFile: " + e.getMessage());
}

Protocol

See the JSON quickstart for more information on using JSON.

Sample Input:

[
    {
        "items": [
          {
            "type": "image/jpeg",
            "data": "[YOUR_BASE64_ENCODED_IMAGE_HERE]",
          },
        ],
      }
  ]
URL:
  POST https://dlp.googleapis.com/v2beta1/content:inspect
Sample Output:
[
  {
    "quote": "(123) 456-7890",
    "infoType": {
      "name": "PHONE_NUMBER"
    },
    "likelihood": "POSSIBLE",
    "location": {
      "imageBoxes": [
        {
          "top": 3,
          "left": 272,
          "width": 5,
          "height": 21
        },
        {
          "top": 4,
          "left": 284,
          "width": 39,
          "height": 18
        },
        {
          "top": 3,
          "left": 328,
          "width": 5,
          "height": 21
        },
        {
          "top": 4,
          "left": 353,
          "width": 109,
          "height": 18
        }
      ]
    },
    "createTime": "1970-01-18T05:28:25.239434Z"
  }
]

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Data Loss Prevention API