Face detection and processing

Updated at:

Functions for face detection, counting, and blurring, suitable for face detection and privacy protection scenarios.

The functions in this topic are provided by the Python multimodal function library (VVR 11.8+). Load the module at the beginning of the SQL job before use:

LOAD MODULE python;
USE MODULES python, core;

For complete examples and the IMAGE type, see Image function overview.

Function summary

Function

Description

IMAGE_FACE_DETECT

Detect faces and return bounding boxes

IMAGE_FACE_COUNT

Count faces in an image

IMAGE_FACE_BLUR

Blur detected face regions

IMAGE_FACE_DETECT

Detects faces and returns bounding boxes.

IMAGE_FACE_DETECT(image_input [, cv_classifier [, min_neighbors [, scale_factor
  [, min_size [, max_size]]]]])

Parameter

Type

Default

Description

image_input

IMAGE

Required

The decoded image.

cv_classifier

STRING NOT NULL

'haarcascade_frontalface_alt.xml'

OpenCV cascade classifier XML file.

min_neighbors

INT NOT NULL

3

Minimum number of neighbors required to retain a detection window.

scale_factor

DOUBLE NOT NULL

1.1

Scale factor for multi-scale detection.

min_size

ARRAY<INT>

NULL

Minimum face size [w, h]. NULL indicates no minimum size.

max_size

ARRAY<INT>

NULL

Maximum face size [w, h]. NULL indicates no maximum size.

Return type: ARRAY<ROW<x INT, y INT, w INT, h INT, confidence DOUBLE>>

Each element in the returned array represents a detected face bounding box:

Field

Description

x / y

Top-left coordinates of the bounding box, in pixels.

w / h

Width and height of the bounding box, in pixels.

confidence

Detection confidence. The current face detector does not return per-box probabilities, so this field is NULL.

SELECT CARDINALITY(IMAGE_FACE_DETECT(img)) AS face_count FROM images;

IMAGE_FACE_COUNT

Counts the number of faces in an image. Parameters are the same as IMAGE_FACE_DETECT.

IMAGE_FACE_COUNT(image_input [, cv_classifier [, min_neighbors [, scale_factor
  [, min_size [, max_size]]]]])

Return type: INT

NULL when the input is NULL. Returns 0 when no face is detected in a valid image.

SELECT IMAGE_FACE_COUNT(img, 'haarcascade_frontalface_alt.xml', 3, 1.1)
FROM images;

IMAGE_FACE_BLUR

Detects faces and blurs the detected face regions.

IMAGE_FACE_BLUR(image_input [, cv_classifier [, blur_type [, radius
  [, min_neighbors [, scale_factor [, min_size [, max_size]]]]]]])

Parameter

Type

Default

Description

image_input

IMAGE

Required

The decoded image.

cv_classifier

STRING NOT NULL

'haarcascade_frontalface_alt.xml'

OpenCV cascade classifier XML file.

blur_type

STRING NOT NULL

'gaussian'

Blur algorithm. Supported values: 'gaussian' or 'box'.

radius

INT NOT NULL

2

Blur radius.

min_neighbors

INT NOT NULL

3

Minimum number of neighbors required to retain a detection window.

scale_factor

DOUBLE NOT NULL

1.1

Scale factor for multi-scale detection.

min_size

ARRAY<INT>

NULL

Minimum face size [w, h]. NULL indicates no minimum size.

max_size

ARRAY<INT>

NULL

Maximum face size [w, h]. NULL indicates no maximum size.

Return type: IMAGE

The returned image preserves the original content and replaces the detected face regions with blurred regions.

SELECT IMAGE_ENCODE(IMAGE_FACE_BLUR(img, 'haarcascade_frontalface_alt.xml', 'gaussian', 15), 'JPEG')
FROM images;