detect

Detect objects using YOLO v2 object detector

Syntax

bboxes = detect(detector,I)
[bboxes,scores] = detect(detector,I)
[___,labels] = detect(detector,I)
[___] = detect(___,roi)
[___] = detect(___,Name,Value)

Description

example

bboxes = detect(detector,I) detects objects within image I using you look only once version 2 (YOLO v2) object detector. The locations of objects detected are returned as a set of bounding boxes.

When using this function, use of a CUDA®-enabled NVIDIA® GPU with a compute capability of 3.0 or higher is highly recommended. The GPU reduces computation time significantly. Usage of the GPU requires Parallel Computing Toolbox™.

[bboxes,scores] = detect(detector,I) also returns the class-specific confidence scores for each bounding box.

example

[___,labels] = detect(detector,I) returns a categorical array of labels assigned to the bounding boxes in addition to the output arguments from the previous syntax. The labels used for object classes are defined during training using the trainYOLOv2ObjectDetector function.

[___] = detect(___,roi) detects objects within the rectangular search region specified by roi. Use output arguments from any of the previous syntaxes. Specify input arguments from any of the previous syntaxes.

[___] = detect(___,Name,Value) specifies options using one or more Name,Value pair arguments in addition to the input arguments in any of the preceding syntaxes.

Examples

collapse all

Load a YOLO v2 object detector pretrained to detect vehicles.

vehicleDetector = load('yolov2VehicleDetector.mat','detector');
detector = vehicleDetector.detector;

Read a test image into the workspace.

I = imread('highway.png');

Display the input test image.

imshow(I);

Run the pretrained YOLO v2 object detector on the test image. Inspect the results for vehicle detection. The labels are derived from the ClassNames property of the detector.

[bboxes,scores,labels] = detect(detector,I)
bboxes = 1×4

    78    81    64    63

scores = single
    0.6224
labels = categorical
     vehicle 

Annotate the image with the bounding boxes for the detections.

if ~isempty(bboxes)
    detectedI = insertObjectAnnotation(I,'rectangle',bboxes,cellstr(labels));
end
figure
imshow(detectedI)

Input Arguments

collapse all

YOLO v2 object detector, specified as a yolov2ObjectDetector object. To create this object, call the trainYOLOv2ObjectDetector function with the training data as input.

Test image, specified as a real, nonsparse, grayscale, or RGB image.

The range of the test image must be same as the range of the images used to train the YOLO v2 object detector. For example, if the detector was trained on uint8 images, the test image must also have pixel values in the range [0, 255]. Otherwise, use the im2uint8 or rescale function to rescale the pixel values in the test image.

Data Types: uint8 | uint16 | int16 | double | single | logical

Search region of interest, specified as a four-element vector of form [x y width height]. The vector specifies the upper left corner and size of a region of interest in pixels.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: detect(detector,I,'Threshold',0.25)

Detection threshold, specified as a comma-separated pair consisting of 'Threshold' and a scalar in the range [0, 1]. Detections that have scores less than this threshold value are removed. To reduce false positives, increase this value.

Select the strongest bounding box for each detected object, specified as the comma-separated pair consisting of 'SelectStrongest' and either true or false.

  • true — Returns the strongest bounding box per object. The method calls the selectStrongestBboxMulticlass function, which uses nonmaximal suppression to eliminate overlapping bounding boxes based on their confidence scores.

    By default, the selectStrongestBboxMulticlass function is called as follows

     selectStrongestBboxMulticlass(bbox,scores,...
                                   'RatioType','Min',...
                                   'OverlapThreshold',0.5);

  • false — Return all the detected bounding boxes. You can then write your own custom method to eliminate overlapping bounding boxes.

Minimum region size, specified as the comma-separated pair consisting of 'MinSize' and a vector of the form [height width]. Units are in pixels. The minimum region size defines the size of the smallest region containing the object.

By default, MinSize is 1-by-1.

Maximum region size, specified as the comma-separated pair consisting of 'MaxSize' and a vector of the form [height width]. Units are in pixels. The maximum region size defines the size of the largest region containing the object.

By default, 'MaxSize' is set to the height and width of the input image, I. To reduce computation time, set this value to the known maximum region size for the objects that can be detected in the input test image.

Hardware resource on which to run the detector, specified as the comma-separated pair consisting of 'ExecutionEnvironment' and 'auto', 'gpu', or 'cpu'.

  • 'auto' — Use a GPU if it is available. Otherwise, use the CPU.

  • 'gpu' — Use the GPU. To use a GPU, you must have Parallel Computing Toolbox and a CUDA-enabled NVIDIA GPU with a compute capability of 3.0 or higher. If a suitable GPU is not available, the function returns an error.

  • 'cpu' — Use the CPU.

Output Arguments

collapse all

Location of objects detected within the input image, returned as an M-by-4 matrix, where M is the number of bounding boxes. Each row of bboxes contains a four-element vector of the form [x y width height]. This vector specifies the upper left corner and size of that corresponding bounding box in pixels.

Detection confidence scores, returned as an M-by-1 vector, where M is the number of bounding boxes. A higher score indicates higher confidence in the detection.

Labels for bounding boxes, returned as an M-by-1 categorical array of M labels. You define the class names used to label the objects when you train the input detector.

Introduced in R2019a