Object recognition

By calling yolov4-tiny through opencv for object recognition detection, most object names can be recognized.

1. Code path

Code path:

2. File configuration

First, we need to import the yolov4-tiny network model structure cfg file, network weights file, and the txt file of the classification name of the COCO dataset. (Here we directly use the yolo4 official dataset and model)

3. Target detection

3.1 Build model network structure

First, use the cv2.dnn.readNet() function to construct the CSPDarknet53 network structure, pass in the model structure cfg file, and the network weights file. Opencv provides several methods for supporting image classification, detection, and segmentation for neural network modules, automatically implementing pre-processing and post-processing of input images. Here, the target detection module cv2.dnn_DetectionModel() is used to pass in the network model.

3.2 Target detection method

Parameters:

frame: Input image

confThreshold: Confidence threshold used to filter the selection box, minimum confidence for target detection

numsThreshold: Custom threshold in non-maximum suppression

Return value:

classIds: Category index

confidences: Confidence, the probability that the detection box belongs to a certain category

boxes: Detection box information, upper left corner coordinates (x, y), box width and height (w, h)

3.3 Set model input parameters

size means scaling the input image to the specified size. The larger the size, the better the detection effect, but the slower the detection speed. scale means the scaling size of the pixel value.

4. Main code

Import various libraries and model files

Object recognition function

Object name list:

image-20240226184059024

Main thread

Program Click the Run Entire Program button on the Jupyterlab toolbar, then scroll to the bottom to see the camera component display.

202309010001

At this time, put the recognizable object into the camera screen, and the object name can be framed and displayed.

image-20240226184408178

image-20241008181734622

If you need to exit the program, please click the [Exit] button.