跪拜 Guibai
← Back to the summary

Before Upload: On-Device Scan Flags ID Cards, Faces, and Phone Numbers in Images

On-device Image Privacy Pre-check

Many apps ask users to upload images: reporting issues, submitting materials, sending a screenshot into a conversation.

These images are often not just scenery. An ID card gets captured in the frame, phone numbers in chat records are still there, and facial features are present.

❝

The user clicks upload, but the app receives an original image carrying private information.

One check is still missing before the image is uploaded:

  1. First recognize the ID numbers, phone numbers, and faces in the image
  2. Remind the user that there is private information here; whether to blur it or continue sending is the sender's decision

❝

This step cannot hand the original image over to the cloud; text, faces, and entities all have to be read on the device.

HarmonyOS has built-in text recognition, face detection, and entity recognition capabilities. I packed these three into a complete component, @ohos/ai-privacy-scan.

Before uploading an image, run a privacy information pre-check, recognize sensitive information on the device, box its location, and hand the result to the caller.

GitHub: https://github.com/liyufengrex/ohos_ai_privacy_scan

GitCode: https://gitcode.com/liyufengrex/ohos_ai_privacy_scan

privacy.gif

The three capabilities used

The check happens locally, relying on three capabilities already present in HarmonyOS.

Capability library Capability What it does in this pre-check
Core Vision Kit (@kit.CoreVisionKit) General text recognition textRecognition Reads text from the image, with positions for text blocks, lines, and words
Core Vision Kit (@kit.CoreVisionKit) Face detection faceDetector Finds the face rectangle and confidence
Natural Language Kit (@kit.NaturalLanguageKit) Entity recognition textProcessing.getEntity Extracts phone numbers, ID numbers, emails, URLs, names, locations, times, etc. from the recognized text

General text recognition

Reads all the text on the image. It returns the full text and the positions of that text on the image. The phone numbers, ID cards, bank cards, emails, and URLs that come later are all judged from this text. The ability to draw a box around a particular line comes from the positions left here.

Face detection

Does not read text. It directly gives the top-left corner, width, height, and confidence of each face. Profile faces, faces that are too small, and blurry faces may have no rectangle. When you only want faces, you do not have to read the whole image as text first.

Entity recognition

Follows the text. Names, locations, times, verification codes, express tracking numbers, flight numbers, and that category of ID numbers in the system all come from getEntity. When the device does not have this capability, the results already determined by rules earlier are still kept.

Component usage

First add the dependency in the host module's oh-package.json5. For local development, adjust the path according to your directory:

{
  "dependencies": {
    "@ohos/ai-privacy-scan": "file:../../library"
  }
}

Reference it in a page like this:

import { PrivacyScan, PrivacyType } from '@ohos/ai-privacy-scan';
import type { RiskItem } from '@ohos/ai-privacy-scan';

Call PrivacyScan.check in the page. The image is first decoded into a PixelMap in RGBA_8888 format, and the file is closed after decoding completes. types decides what to check, and showWindow decides whether the user should see a confirmation window first. After verification completes, both usage modes pass the hit list to onConfirm. If this run did not finish, only onError is called; it will not hand over an empty list pretending it was "already checked".

Selectable types

Enum Description
PrivacyType.ALL All
PrivacyType.FACE Face
PrivacyType.PHONE Phone
PrivacyType.LOCATION Location
PrivacyType.DATETIME Time
PrivacyType.IDCARD ID card number
PrivacyType.EMAIL Email
PrivacyType.BANKCARD Bank card
PrivacyType.URL URL
PrivacyType.NAME Name
PrivacyType.OTP Verification code
PrivacyType.EXPRESS Express tracking number
PrivacyType.FLIGHT Flight number
PrivacyType.PASSPORT Passport

types must contain at least one item, otherwise the onError message is "Please select at least one pre-check type". When ALL is in the array, all built-in types are checked. When only FACE is selected, text recognition is not run; when FACE is absent, face detection is not run. The Chinese name of a type can be obtained with privacyLabel(type).

Show the confirmation window

When showWindow is true and there are hits in the image, a confirmation window pops up first. The title is "The image contains sensitive information", boxes are drawn over the image, and the bottom list can be scrolled. After the user taps "Confirm", the window closes and onConfirm receives RiskItem[]. Tapping the mask does not close it. When there are no hits, no window appears; as soon as recognition ends, an empty list is handed over.

import { PrivacyScan, PrivacyType } from '@ohos/ai-privacy-scan';
import type { RiskItem } from '@ohos/ai-privacy-scan';

PrivacyScan.check(this.getUIContext(), {
  types: [PrivacyType.PHONE, PrivacyType.FACE, PrivacyType.IDCARD],
  image: pixelMap,
  showWindow: true,
  onConfirm: (items: RiskItem[]): void => {
    // User confirmed. items is the list of hits in this image.
  },
  onError: (message: string): void => {
    // This scan did not finish.
  }
});

Without the popup

When showWindow is false, no confirmation window appears. As soon as recognition completes, onConfirm is called, with the same hit list as its argument. The page can display it itself, or use it directly to decide on blurring and uploading.

PrivacyScan.check(this.getUIContext(), {
  types: [PrivacyType.FACE],
  image: pixelMap,
  showWindow: false,
  onConfirm: (items: RiskItem[]): void => {
    // No confirmation window. items is also the hit list in this image.
  },
  onError: (message: string): void => {
    // This scan did not finish.
  }
});

The list received by the caller

The argument to onConfirm is RiskItem[]. An empty array means this verification completed and there are no hits in the image. Each item has type, text, and level; items that can be located also have box. box's x and y are the top-left corner of the original image, and width and height are dimensions, in pixels. Items that cannot be matched to a position have no box.

The difference between showing and not showing the window is only delivery timing: with the window, it waits for the user to tap "Confirm"; without it, delivery happens as soon as recognition is done. In both cases the list is the hit result for this image.

Anyone interested is welcome to come play with HarmonyOS together.