Image detection and recognition

Updated at:

Functions for object detection, image segmentation, text recognition, and subplot detection.

The functions in this topic are provided by the Python multimodal function library (VVR 11.8 or later). Before you use them, add the following statements at the beginning of the SQL deployment:

LOAD MODULE python;
USE MODULES python, core;

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

Function list

Function

Description

IMAGE_DETECT_OBJECTS

Detects objects by using YOLO

IMAGE_SEGMENT

Generates an image segmentation mask by using FastSAM

IMAGE_OCR

Extracts text from an image by using EasyOCR

IMAGE_DETECT_SUBPLOT

Determines whether an image is a composite image and returns the number of subplots

IMAGE_DETECT_OBJECTS

Detects objects by using YOLO.

IMAGE_DETECT_OBJECTS(image_input [, model [, confidence [, imgsz [, iou]]]])

Parameter

Type

Default value

Description

image_input

IMAGE

Required

The decoded image.

model

STRING NOT NULL

'yolov8n'

The name or path of the YOLO model.

Note

Only the default model is supported.

confidence

DOUBLE NOT NULL

0.05

The minimum detection confidence. Valid values: [0, 1].

imgsz

INT NOT NULL

640

The inference resolution of the model. A larger value helps detect small objects, but also increases inference latency and resource consumption.

iou

DOUBLE NOT NULL

0.5

The IoU threshold for NMS. Valid values: [0, 1].

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

Each element in the returned array represents a detection box:

Field

Description

label

The name of the object category.

x / y

The coordinates of the upper-left corner of the detection box, in pixels.

w / h

The width and height of the detection box, in pixels.

confidence

The detection confidence. Value range: [0, 1].

SELECT CARDINALITY(IMAGE_DETECT_OBJECTS(img, 'yolov8n', 0.25, 640, 0.5))
FROM images;

IMAGE_SEGMENT

Generates an image segmentation mask by using FastSAM.

IMAGE_SEGMENT(image_input [, model [, confidence [, imgsz [, iou]]]])

Parameter

Type

Default value

Description

image_input

IMAGE

Required

The decoded image.

model

STRING NOT NULL

'FastSAM-x.pt'

The name or path of the FastSAM model.

Note

Only the default model is supported.

confidence

DOUBLE NOT NULL

0.05

The minimum confidence. Valid values: [0, 1].

imgsz

INT NOT NULL

1024

The inference resolution of the model. A larger value helps preserve mask details of small objects, but also increases inference latency and resource consumption.

iou

DOUBLE NOT NULL

0.5

The IoU threshold for NMS. Valid values: [0, 1].

Return type: BYTES, an 8-bit segmentation mask encoded in PNG. A pixel value of 0 indicates the background. Values from 1 to 255 indicate the numbers of different segmentation regions. The 255th and all subsequent regions all use the value 255.

SELECT IMAGE_SEGMENT(img, 'FastSAM-x.pt', 0.05, 1024, 0.5) FROM images;

IMAGE_OCR

Extracts text from an image by using EasyOCR.

IMAGE_OCR(image_input [, lang])

Parameter

Type

Default value

Description

image_input

IMAGE

Required

The decoded image.

lang

ARRAY<STRING>

NULL

The language list.NULL indicates that the default English configuration ARRAY['en'].

The current built-in model supports English recognition only. To recognize more languages, use general-purpose invocation.

Return type: ARRAY<ROW<text STRING, confidence DOUBLE, bbox ARRAY<ARRAY<DOUBLE>>>>

Each element in the returned array represents a text region:

Field

Description

text

The recognized text content.

confidence

The OCR confidence. The value usually ranges from [0, 1].

bbox

The coordinates of the four corner points of the text region, in the form of [[x1, y1], [x2, y2], [x3, y3], [x4, y4]].

SELECT IMAGE_OCR(img, ARRAY['en']) FROM images;

IMAGE_DETECT_SUBPLOT

Determines whether an image is a composite image, a grid image, or a subplot based on edge and grid features, and returns the estimated number of subplots.

IMAGE_DETECT_SUBPLOT(image_input [, threshold])

Parameter

Type

Default value

Description

image_input

IMAGE

Required

The decoded image.

threshold

DOUBLE NOT NULL

0.5

The edge detection threshold. Valid values: [0, 1]. A larger value requires stronger edges and applies a stricter criterion.

Return type: ROW<is_subplot BOOLEAN, count INT>

Field

Description

is_subplot

Indicates whether a composite or subplot structure is detected.

count

The estimated number of subplots. Returns 1 if the image is not a subplot.

An image with any side smaller than 20 pixels directly returns is_subplot=FALSE and count=1.

SELECT IMAGE_DETECT_SUBPLOT(img).is_subplot, IMAGE_DETECT_SUBPLOT(img).count
FROM images;