DetectorDNNOpenCV

class DetectorDNNOpenCV(self)

Bases: pybind11_object

This class is a wrapper over the OpenCV DNN module and specialized to handle object detection task.

This class supports the following networks dedicated to object detection:

  • Faster-RCNN, see usage to detect objects belonging to the COCO dataset using dnn_supported_faster_rcnn network

  • SSD MobileNet, see usage to detect objects belonging to the COCO dataset using dnn_supported_mobilenet_ssd network

  • ResNet 10, see usage for dnn_usecase_face_detection

  • Yolo v3, see usage to detect objects belonging to the COCO dataset using dnn_supported_yolov3 network

  • Yolo v4, see usage to detect objects belonging to the COCO dataset using dnn_supported_yolov4 network

  • Yolo v5, see usage to detect objects belonging to the COCO dataset using dnn_supported_yolov5 network

  • Yolo v7, see usage to detect objects belonging to the COCO dataset using dnn_supported_yolov7 network

  • Yolo v8, see usage to detect objects belonging to the COCO dataset using dnn_supported_yolov8 network

This class can be initialized from a JSON file if ViSP has been compiled with NLOHMANN JSON (see soft_tool_json to see how to do it). Examples of such JSON files can be found in the tutorial folder.

Note

See tutorial-detection-dnn

Methods

__init__

detect

Overloaded function.

dnnResultsParsingTypeFromString

Cast a name of class of parsing method into a ** vpDetectorDNNOpenCV::DNNResultsParsingType ** .

dnnResultsParsingTypeToString

getAvailableDnnResultsParsingTypes

Get the list of the parsing methods / types of DNNs supported by the ** vpDetectorDNNOpenCV ** class.

getNetConfig

initFromJSON

param jsonPath:

parseClassNamesFile

Parse the designated file that contains the list of the classes the network can detect.

readNet

Read a network, see OpenCV readNet documentation for more information.

saveConfigurationInJSON

Save the network configuration in a JSON file.

setConfidenceThreshold

Set confidence threshold to filter the detections.

setDetectionFilterSizeRatio

Set the size ratio used in the filterDetection method.

setInputSize

Set dimension to resize the image to the input blob.

setMean

Set mean subtraction values.

setNMSThreshold

Set Non-Maximum Suppression threshold, used to filter multiple detections at approximatively the same location.

setNetConfig

setPreferableBackend

Set preferable backend for inference computation.

setPreferableTarget

Set preferable target for inference computation.

setScaleFactor

Set scale factor to normalize the range of pixel values.

setSwapRB

If true, swap R and B channel for mean subtraction.

Inherited Methods

Operators

__doc__

__init__

__module__

__repr__

Attributes

COUNT

FASTER_RCNN

RESNET_10

SSD_MOBILENET

USER_SPECIFIED

YOLO_V11

YOLO_V3

YOLO_V4

YOLO_V5

YOLO_V7

YOLO_V8

__annotations__

class DNNResultsParsingType(self, value: int)

Bases: pybind11_object

Enumeration listing the types of DNN for which the ** vpDetectorDNNOpenCV ** furnishes the methods permitting to parse the raw detection data into ** vpDetectorDNNOpenCV::DetectedFeatures2D ** .

Values:

  • USER_SPECIFIED: The user will give a pointer towards the parsing method to use to parse the raw data resulting from the detection step.

  • FASTER_RCNN: The ** vpDetectorDNNOpenCV ** object will use the parsing method corresponding to a Faster-RCNN DNN. See ** vpDetectorDNNOpenCV::postProcess_FasterRCNN ** for more information.

  • SSD_MOBILENET: The ** vpDetectorDNNOpenCV ** object will use the parsing method corresponding to a SSD MobileNet DNN. See ** vpDetectorDNNOpenCV::postProcess_SSD_MobileNet ** for more information.

  • RESNET_10: The ** vpDetectorDNNOpenCV ** object will use the parsing method corresponding to a ResNet 10 DNN. See ** vpDetectorDNNOpenCV::postProcess_ResNet_10 ** for more information.

  • YOLO_V3: The ** vpDetectorDNNOpenCV ** object will use the parsing method corresponding to a YoloV3 DNN. See ** vpDetectorDNNOpenCV::postProcess_YoloV3_V4 ** for more information.

  • YOLO_V4: The ** vpDetectorDNNOpenCV ** object will use the parsing method corresponding to a YoloV4 DNN. See ** vpDetectorDNNOpenCV::postProcess_YoloV3_V4 ** for more information.

  • YOLO_V5: The ** vpDetectorDNNOpenCV ** object will use the parsing method corresponding to a YoloV5 DNN. See ** vpDetectorDNNOpenCV::postProcess_YoloV5_V7 ** for more information.

  • YOLO_V7: The ** vpDetectorDNNOpenCV ** object will use the parsing method corresponding to a YoloV7 DNN. See ** vpDetectorDNNOpenCV::postProcess_YoloV5_V7 ** for more information.

  • YOLO_V8: The ** vpDetectorDNNOpenCV ** object will use the parsing method corresponding to a YoloV8 DNN. See ** vpDetectorDNNOpenCV::postProcess_YoloV8 ** for more information.

  • COUNT: The number of parsing method that come along with the ** vpDetectorDNNOpenCV ** class.

__and__(self, other: object) object
__eq__(self, other: object) bool
__ge__(self, other: object) bool
__getstate__(self) int
__gt__(self, other: object) bool
__hash__(self) int
__index__(self) int
__init__(self, value: int)
__int__(self) int
__invert__(self) object
__le__(self, other: object) bool
__lt__(self, other: object) bool
__ne__(self, other: object) bool
__or__(self, other: object) object
__rand__(self, other: object) object
__ror__(self, other: object) object
__rxor__(self, other: object) object
__setstate__(self, state: int) None
__xor__(self, other: object) object
property name : str
__init__(self)
detect(*args, **kwargs)

Overloaded function.

  1. detect(self: visp._visp.detection.DetectorDNNOpenCV, I: visp._visp.core.ImageGray, output: list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]) -> tuple[bool, list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]]

Object detection using OpenCV DNN module.

Parameters:
I

Input image.

output

Vector of detections, whichever class they belong to.

Returns:

A tuple containing:

  • false if there is no detection, true otherwise.

  • output: Vector of detections, whichever class they belong to.

  1. detect(self: visp._visp.detection.DetectorDNNOpenCV, I: visp._visp.core.ImageGray, output: dict[str, list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]]) -> bool

Parameters:
I

Input image.

output

map where the name of the class is used as key and whose value is a vector of detected 2D features that belong to the class.

Returns:

false if there is no detection.

  1. detect(self: visp._visp.detection.DetectorDNNOpenCV, I: visp._visp.core.ImageGray, output: list[tuple[str, list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]]]) -> tuple[bool, list[tuple[str, list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]]]]

Parameters:
I

Input image.

output

vector of pairs <name_of_the_class, vector_of_detections>

Returns:

A tuple containing:

  • false if there is no detection.

  • output: vector of pairs <name_of_the_class, vector_of_detections>

  1. detect(self: visp._visp.detection.DetectorDNNOpenCV, I: visp._visp.core.ImageRGBa, output: list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]) -> tuple[bool, list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]]

Object detection using OpenCV DNN module.

Parameters:
I

Input image.

output

Vector of detections, whichever class they belong to.

Returns:

A tuple containing:

  • false if there is no detection, true otherwise.

  • output: Vector of detections, whichever class they belong to.

  1. detect(self: visp._visp.detection.DetectorDNNOpenCV, I: visp._visp.core.ImageRGBa, output: dict[str, list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]]) -> bool

Object detection using OpenCV DNN module.

Parameters:
I

Input image.

output

map where the name of the class is used as key and whose value is a vector of detected 2D features that belong to the class.

Returns:

false if there is no detection.

  1. detect(self: visp._visp.detection.DetectorDNNOpenCV, I: visp._visp.core.ImageRGBa, output: list[tuple[str, list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]]]) -> tuple[bool, list[tuple[str, list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]]]]

Object detection using OpenCV DNN module.

Parameters:
I

Input image.

output

vector of pairs <name_of_the_class, vector_of_detections>

Returns:

A tuple containing:

  • false if there is no detection, true otherwise.

  • output: vector of pairs <name_of_the_class, vector_of_detections>

  1. detect(self: visp._visp.detection.DetectorDNNOpenCV, I: cv::Mat, output: list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]) -> tuple[bool, list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]]

Object detection using OpenCV DNN module.

Parameters:
I

Input image.

output

Vector of detections, whichever class they belong to.

Returns:

A tuple containing:

  • false if there is no detection.

  • output: Vector of detections, whichever class they belong to.

  1. detect(self: visp._visp.detection.DetectorDNNOpenCV, I: cv::Mat, output: dict[str, list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]]) -> bool

Object detection using OpenCV DNN module.

Parameters:
I

Input image.

output

map where the name of the class is used as key and whose value is a vector of detected 2D features that belong to the class.

Returns:

false if there is no detection.

  1. detect(self: visp._visp.detection.DetectorDNNOpenCV, I: cv::Mat, output: list[tuple[str, list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]]]) -> tuple[bool, list[tuple[str, list[visp._visp.detection.DetectorDNNOpenCV.DetectedFeatures2D]]]]

Object detection using OpenCV DNN module.

Parameters:
I

Input image.

output

vector of pairs <name_of_the_class, vector_of_detections>

Returns:

A tuple containing:

  • false if there is no detection.

  • output: vector of pairs <name_of_the_class, vector_of_detections>

static dnnResultsParsingTypeFromString(name: str) visp._visp.detection.DetectorDNNOpenCV.DNNResultsParsingType

Cast a name of class of parsing method into a ** vpDetectorDNNOpenCV::DNNResultsParsingType ** . The naming convention is the following:

  • only lowercases

static dnnResultsParsingTypeToString(type: visp._visp.detection.DetectorDNNOpenCV.DNNResultsParsingType) str
static getAvailableDnnResultsParsingTypes() str

Get the list of the parsing methods / types of DNNs supported by the ** vpDetectorDNNOpenCV ** class.

Returns:

std::string The list of the supported parsing methods / types of DNNs.

getNetConfig(self) visp._visp.detection.DetectorDNNOpenCV.NetConfig
initFromJSON(self, jsonPath: str) None
Parameters:
jsonPath: str

static parseClassNamesFile(filename: str) list[str]

Parse the designated file that contains the list of the classes the network can detect. The class names must either be indicated in an array of string in YAML format, or one name by row (without quotes) as described in NetConfig::parseClassNamesFile() .

Note

See NetConfig::parseClassNamesFile() .

Parameters:
filename: str

The path towards the file.

Returns:

std::vector<std::string> The list of class names.

readNet(self: visp._visp.detection.DetectorDNNOpenCV, model: str, config: str =, framework: str =) None

Read a network, see OpenCV readNet documentation for more information.

Parameters:
model

Path to a binary file of model containing trained weights. The following file extensions are expected for models from different frameworks:

config

Path to a text file of model containing network configuration. It could be a file with the following extensions:

framework

Optional name of an origin framework of the model. Automatically detected if it is not set.

saveConfigurationInJSON(self, jsonPath: str) None

Save the network configuration in a JSON file.

Parameters:
jsonPath: str

Path towards the output JSON file .

setConfidenceThreshold(self, confThreshold: float) None

Set confidence threshold to filter the detections.

Parameters:
confThreshold: float

Confidence threshold between [0, 1]

setDetectionFilterSizeRatio(self, sizeRatio: float) None

Set the size ratio used in the filterDetection method. If <= 0., filterDetection is not used. The detections of class c for which the bbox area does not belong to [mean_class_c(Area) * sizeRatio ; mean_class_c(Area) / sizeRatio ] are removed from the list of detections.

Parameters:
sizeRatio: float

the size ratio used in the filterDetection method. If <= 0., filterDetection is not used.

setInputSize(self, width: int, height: int) None

Set dimension to resize the image to the input blob.

Parameters:
width: int

If <= 0, blob width is set to image width

height: int

If <= 0, blob height is set to image height

setMean(self, meanR: float, meanG: float, meanB: float) None

Set mean subtraction values.

Parameters:
meanR: float

Mean value for R-channel

meanG: float

Mean value for G-channel

meanB: float

Mean value for R-channel

setNMSThreshold(self, nmsThreshold: float) None

Set Non-Maximum Suppression threshold, used to filter multiple detections at approximatively the same location.

Parameters:
nmsThreshold: float

Non-Maximum Suppression threshold between [0, 1]

setNetConfig(self, config: visp._visp.detection.DetectorDNNOpenCV.NetConfig) None
setPreferableBackend(self, backendId: int) None

Set preferable backend for inference computation. See OpenCV setPreferableBackend documentation for more information.

Parameters:
backendId: int

Backend identifier

setPreferableTarget(self, targetId: int) None

Set preferable target for inference computation. See OpenCV setPreferableTarget documentation for more information.

Parameters:
targetId: int

Target identifier

setScaleFactor(self, scaleFactor: float) None

Set scale factor to normalize the range of pixel values.

setSwapRB(self, swapRB: bool) None

If true, swap R and B channel for mean subtraction. For instance when the network has been trained on RGB image format (OpenCV uses BGR convention).