An industrial QR code positioning method based on YOLOv5
Through the industrial QR code positioning method based on YOLOv5, using data enhancement and new loss functions, the problem of low QR code positioning accuracy in the existing technology is solved, and real-time accurate identification and rapid positioning in an industrial environment are achieved.
Patent Information
- Application Number
- CN202210228300.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-08
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2042-03-08
AI Technical Summary
When the existing QR code positioning method is implemented on a low-resource chip, the model is simple, the training strategy and the evaluation strategy are single, resulting in low positioning accuracy, unable to achieve end-to-end positioning, and unable to meet industrial-level needs.
The industrial QR code positioning method based on YOLOv5 is used to collect and preprocess images, generate labels, perform data enhancement, train the qrbox_yolov5s neural network, and use new loss functions and regression prediction methods to reason about the QR code position in real time and improve positioning accuracy. .
It achieves real-time and accurate recognition of QR codes in an industrial environment, improves the accuracy and speed of the qrbox_yolov5s neural network, and meets industrial-level positioning needs.
Smart Images

Figure CN114693908B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and in particular to an industrial two-dimensional code positioning method based on YOLOv5. Background Art
[0002] Artificial intelligence is a new technical science that studies and develops theories, methods, technologies and application systems that can simulate, extend and expand human intelligence. The purpose of the research is to enable intelligent machines to listen (speech recognition, machine translation, etc.), see (image recognition, text recognition, etc.), speak (speech synthesis, human-computer dialogue, etc.), think (human-computer chess, theorem proving, etc.), learn (machine learning, knowledge representation, etc.), and act (robots, self-driving cars, etc.). The existing QR code positioning is implemented on low-resource chips. The model used is simple, and the training strategy and evaluation strategy are single, which makes the accuracy of QR code positioning low; the existing QR code positioning method adopts a two-stage positioning method of coarse positioning followed by fine positioning, which cannot achieve end-to-end positioning effect and cannot meet industrial-level requirements. Summary of the invention
[0003] The present application provides an industrial two-dimensional code positioning method based on YOLOv5, which aims to solve at least one of the technical problems caused by the defects of the prior art in the above-mentioned background technology.
[0004] In order to solve the above technical problems, the technical solution of the present invention is as follows:
[0005] The present invention provides an industrial two-dimensional code positioning method based on YOLOv5, comprising:
[0006] S1: Collect photos containing QR code images, pre-process the photos, and obtain processed QR code images;
[0007] S2: Generate corresponding labels for the processed QR code images using a label generator;
[0008] S3: Perform data enhancement processing on the QR code image in the label to obtain a data set of enhanced images;
[0009] S4: In the QRBOX_YOLOv5 industrial QR code locator, the data-enhanced images are used to train the constructed qrbox_yolov5s neural network to generate weights and test them, and the training weights with the best test results are used as the trained QRBOX_YOLOv5 industrial QR code locator;
[0010] S5: Input the QR code image data to be identified into the QRBOX_YOLOv5 industrial QR code locator, perform real-time reasoning on the QR code to be located, and obtain the positioning result.
[0011] Preferably, in the step S1, photos containing QR code images are collected, specifically including: when taking QR code images, the industrial scene is used as the background of the picture, the actual industrial application scene is simulated for shooting, and the QR code image is read according to the actual industrial application scene. The pixel of the shooting camera is selected according to the camera pixel in actual application; according to the diversity of QR code types and industrial scenes in actual application, multiple QR code images are taken in multiple scenes; the QR code is identified by conventional methods, and the QR code images that cannot be identified even if the positioning is successful are cleared; the number of QR code photos taken is between 1000 and 10000.
[0012] Only when the industrial QR code images selected in the early stage are in line with the actual situation of the industrial scene, the final positioning of the QR code will be more accurate.
[0013] Preferably, in step S2, the specific steps of generating a corresponding label for the image are:
[0014] S2.1: Write a label generator to generate a label file for each image. A label file contains all the QR code information in the image. You can draw a rotated rectangle in the image and calculate the coordinates of the four vertices of the rectangle relative to the image.
[0015] S2.2: Draw a frame for the QR code in the image in the label generator, and convert the coordinates of the four vertices of the rectangular frame into the width and height w and h of the rectangular frame relative to the entire image, the coordinates of the center point x and y, and two parameters m and n.
[0016] S2.3: Normalize the width and height w and h, the center point coordinates x and y, and the two parameters m and n related to the angle.
[0017] Preferably, in the step S2.1, a label generator is written, and a label file is generated from one picture. One label file contains all the QR code information in the picture, and a rotating rectangular box can be drawn in the picture, specifically including: the label file is in txt text format, one label file contains all the QR code information in the picture, each QR code information occupies one line, each rectangular box represents a QR code, and each QR code information contains 7 parameters. The first parameter is the type of QR code, and different categories are represented by different numbers; the second and third parameters are the center point coordinates of the QR code rectangular box, represented by x and y; the fourth and fifth parameters are the width and height of the QR code, represented by w and h; the sixth and seventh parameters are m and n.
[0018] The generated label is related to the entire QR code locator. A QR code in the label is represented by 7 parameters, namely the category, the coordinates of the center point of the QR code in the image, the width and height of the QR code, and m and n related to the rotation angle of the QR code. Accurate label information is important for more accurate prediction of QR code information.
[0019] Preferably, in step S2.2, the coordinates of the four vertices of the rectangular frame are converted into the coordinates x and y of the center point of the two-dimensional code of the rectangular frame relative to the entire image, the width and height w and h of the two-dimensional code, and two parameters m and n related to the angle in the label generator. The specific conversion method is: the right side of the image is the positive direction of the x-axis, and the bottom is the positive direction of the y-axis. Find the bottom point of the rectangle, and derive a dotted line from this point to the positive direction of the x-axis. This dotted line forms an angle with one side of the rectangle. Record this side of the rectangle as w, and the other side connected to it as h. Record the coordinates of the two vertices of this side, the point on the left (x1, y1) and the point on the right (x2, y2), m = x2-x1, n = y2-y1.
[0020] The four vertex coordinates are converted into 6 parameters. The x and y, w and h, m and n are very closely related to each other, which greatly improves the accuracy of the prediction and makes the positioning more accurate.
[0021] Preferably, in step S2.3, the width and height w and h, the center point coordinates x and y, and the two parameters m and n are normalized, specifically including: w, x, m are divided by the image width, and h, y, n are divided by the image height.
[0022] The normalized parameters will not skip the optimal solution due to excessive gradient descent during training, thus improving the training accuracy.
[0023] Preferably, in step S3, the specific steps of processing the two-dimensional code image to obtain the processed data set are:
[0024] S3.1: The background part of the image remains unchanged, the QR code is rotated, and the images before and after the operation are saved;
[0025] S3.2: The background part of the image remains unchanged, the QR code is translated, and the images before and after the operation are saved;
[0026] S3.3: Generate corresponding labels for the newly synthesized images and use them as datasets;
[0027] S3.4: Split the dataset into training and test sets in a ratio of 3:1.
[0028] Preferably, in step S3.1, the background in the image remains stationary and the QR code is rotated, specifically comprising: recording the coordinates of the center point of the QR code in the image; cutting out the QR code in the image and saving the pure QR code image and the background image after cutting, using Poisson fusion to make the blank part of the background image after cutting out the QR code consistent with the background image, and generating a background image after cutting with only the background; rotating the cut QR code at a random angle, and pasting the rotated QR code into the background image through the coordinates of the original image corresponding to the center point of the QR code.
[0029] Perform rotation data enhancement on the QR code, add several enhanced training images, increase the scale and complexity of the training data, and improve the accuracy of angle-related information prediction.
[0030] Preferably, in step S3.2, the background in the image remains stationary, and the QR code is translated, specifically comprising: cutting out the QR code in the image and saving the pure QR code image and the background image after cutting, using Poisson fusion to make the blank part of the background image after the QR code is cut out consistent with the background image, and generating a background image after cutting that only has the background; and translating the cut QR code up, down, left, and right for a certain distance in the background image without moving out of the image.
[0031] Perform translation data enhancement on the QR code, add several enhanced training images, increase the scale and complexity of the training data, and improve the accuracy of the center point coordinate prediction.
[0032] Preferably, in step S3.3, a corresponding label is generated for the newly synthesized image.
[0033] Generate labels for data-augmented images, expand the data set, increase the scale and complexity of training data, and improve the accuracy of center point coordinate prediction.
[0034] Preferably, in step S3.4, the data set is divided into a training set and a test set at a ratio of 3:1.
[0035] The data set must be divided reasonably. The reasonable division of 3:1 into training set and test set makes the training and testing effects more convincing.
[0036] Preferably, in step S5, the QR code image data to be identified is input into the QRBOX_YOLOv5 industrial QR code locator, and the QR code to be located is inferred in real time to obtain a positioning result.
[0037] Compared with the prior art, the technical solution of the present invention has the following beneficial effects:
[0038] The present invention first processes the industrial QR code images, and uses the processed data sets as training sets and test sets, which increases the scale and complexity of the data, and helps to improve the accuracy of the qrbox_yolov5s neural network during training; the constructed qrbox_yolov5s neural network is a lightweight network with fast speed; a new loss function and regression prediction method are used, and six interrelated parameters are used to represent the position information of the QR code, which can realize real-time and accurate recognition of objects in an industrial environment. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Figure 1 The industrial two-dimensional code positioning method based on YOLOv5 described in Example 1;
[0040] Figure 2 It is a simplified structural diagram of the qrbox_yolov5s neural network described in Example 1;
[0041] Figure 3 This is a structural diagram of the QRBOX_YOLOv5 industrial QR code locator described in Example 1. DETAILED DESCRIPTION
[0042] The drawings are for illustrative purposes only and should not be construed as limiting the present patent;
[0043] In order to better illustrate the present embodiment, some parts in the drawings may be omitted, enlarged or reduced, and do not represent the size of the actual product;
[0044] It is understandable to those skilled in the art that some well-known structures and their descriptions may be omitted in the drawings.
[0045] The technical solution of the present invention is further described below in conjunction with the accompanying drawings and embodiments.
[0046] Example 1
[0047] The present invention provides an industrial two-dimensional code positioning method based on YOLOv5, such as Figure 1 As shown, including:
[0048] S1: Collect photos containing QR code images, pre-process the photos, and obtain processed QR code images;
[0049] In this embodiment, based on the diversity of QR code types and industrial scenarios in actual applications, multiple QR code images are taken in multiple industrial scenarios; conventional methods are used to locate and identify the QR codes, and QR code images that cannot be identified even if the positioning is successful are cleared; and the number of QR code photos taken is 1,000.
[0050] S2: Generate corresponding labels for the processed QR code images using a label generator;
[0051] include:
[0052] S2.1: Write a label generator to generate a label file for each image. A label file contains all the QR code information in the image. You can draw a rotated rectangle in the image and calculate the coordinates of the four vertices of the rectangle relative to the image.
[0053] In this embodiment, a label generator is written, which can generate a label file for each input image. The label file format is txt text format, and each label file corresponds to a picture; each line in the label file represents a QR code in the picture; a line has 7 parameters, which are: class, x, y, w, h, m, n, respectively, and the 7 parameters represent the category of the QR code, the coordinates of the center point of the QR code relative to the whole picture, the width and height of the QR code relative to the whole picture, and the rotation angle of the QR code relative to the whole picture.
[0054] S2.2: Draw a frame for the QR code in the image in the label generator, and convert the coordinates of the four vertices of the rectangular frame into the width and height w and h of the rectangular frame relative to the entire image, the coordinates of the center point x and y, and two parameters m and n related to the angle;
[0055] In this embodiment, in the label generator, a minimum frame is drawn to surround each pair of QR codes to generate a line of label information; the right side of the picture is taken as the positive direction of the x-axis, and the bottom side is taken as the positive direction of the y-axis; the lowest point of the rectangle is found, and a dotted line is derived from this point in the positive direction of the x-axis, and this dotted line forms an angle with one side of the rectangle; this side of the rectangle is recorded as w, and the other side connected to it is h, and the coordinates of the two vertices of this side are recorded, the point on the left (x1, y1) and the point on the right (x2, y2), m = x2-x1, n = y2-y1.
[0056] S2.3: Normalize the width and height w and h, the center point coordinates x and y, and the two parameters m and n related to the angle;
[0057] In this embodiment, the normalization processing method is: w, x, and m are divided by the image width, and h, y, and n are divided by the image height.
[0058] S3: Perform data enhancement processing on the QR code image in the label to obtain a data set of enhanced images;
[0059] include:
[0060] S3.1: The background part of the image remains unchanged, the QR code is rotated, and the images before and after the operation are saved;
[0061] In this embodiment, the coordinates of the center point of the QR code in the picture are recorded; the QR code in the picture is cut out and the pure QR code picture after cutting, the background picture after cutting and the original picture are saved, and the blank part of the background picture after cutting out the QR code is used for Poisson fusion to make it consistent with the background picture, so as to generate a background picture after cutting out with only the background; the cut QR code is rotated at a random angle, and the rotated QR code is pasted into the background picture according to the coordinates of the original picture corresponding to the center point of the QR code; 7 parameters of the rotated QR code are calculated and saved in the label;
[0062] The operation is performed three times to generate three additional images; at this time, there are a total of 4000 images and 4000 label files;
[0063] S3.2: The background part of the image remains unchanged, the QR code is translated, and the images before and after the operation are saved;
[0064] In this embodiment, the QR code in the image is cut out and the pure QR code image after cutting, the background image after cutting and the original image are saved. The blank part of the background image after cutting out the QR code is made consistent with the background image by using Poisson fusion to generate a background image after cutting out only the background; the cut QR code is translated up and down and left and right for a certain distance in the background image and cannot be moved out of the background image; the seven parameters of the translated QR code are calculated and saved in the label;
[0065] The operation is performed twice, and two additional images are generated; at this time, there are a total of 12,000 images and 12,000 label files;
[0066] S3.3: Generate corresponding labels for the newly synthesized images and use them as datasets.
[0067] In this embodiment, this step has been integrated into the previous two steps;
[0068] S3.4: Split the dataset into training and test sets in a ratio of 3:1;
[0069] In this embodiment, the data set is randomly divided into a training set and a test set in a ratio of 3:1.
[0070] S4: In the QRBOX_YOLOv5 industrial QR code locator, the constructed qrbox_yolov5s neural network is trained with data-enhanced images to generate weights and tested, and the training weights with the best test results are used as the trained QRBOX_YOLOv5 industrial QR code locator.
[0071] In this embodiment, the qrbox_yolov5s neural network is a lightweight neural network built based on the yolov5s network;
[0072] like Figure 2As shown, the qrbox_yolov5s neural network is roughly divided into four layers: input layer, Backbone layer, Neck layer, and Prediction layer.
[0073] S4.1: The data has been rotated and translated in the data processing part. In the QRBOX_YOLOv5 industrial QR code locator, there is no need to use rotation and translation for data enhancement again. Only cutting and splicing in the detector are used for data enhancement.
[0074] In this embodiment, rotation and translation have been done in step 3, and the QRBOX_YOLOv5 industrial QR code locator is modified based on YOLOv5. The rotation data enhancement is not suitable for rotating targets and will conflict with translation after use. It is not necessary to do it again, and other data enhancement methods can be used to continue to expand the size of the data set.
[0075] S4.2: Add attention mechanism to qrbox_yolov5s network.
[0076] In this example, the yolov5s.yaml file is modified, and the C3 module is changed to the module CBAMC3 with the attention mechanism added to enhance the accuracy of small QR code target detection.
[0077] S4.3: Use a new loss function in the prediction layer.
[0078] In this example, modify the loss.py file and use a new loss function, loss = SmoothL1 + (m 2 +n 2 -w 2 ) 2 The loss function as a prediction parameter.
[0079] S4.4: In the regression process, the sigmoid function is used to map m, and the tanh function is used to map n.
[0080] In this example, modify the loss.py and yolo.py files, use the sigmoid function to map m, and use the tanh function to map n during the parameter regression process.
[0081] S4.5: When training, set the batch-size to 4 or 2, and the input image size should be a multiple of 32 and not less than 3 times the actual image size.
[0082] In this example, the batch-size is set to 4 during training, and the input image size is 1024, which is a multiple of 32.
[0083] S4.6: Use the training set of processed image data to train the constructed qrbox_yolov5s neural network.
[0084] In this example, the train.py file is used to train the training set data to obtain the trained weights.
[0085] S4.7: Use the trained weights to input the test set into the qrbox_yolov5s neural network for testing.
[0086] In this example, the test.py file is used to test the test set data and its performance.
[0087] S5: Input the QR code image data to be identified into the QRBOX_YOLOv5 industrial QR code locator, perform real-time reasoning on the QR code to be located, and obtain the positioning result.
[0088] In this example, the QRBOX_YOLOv5 industrial QR code locator is based on YOLOv5, which has a very fast positioning speed and improves the positioning accuracy through methods such as new loss functions;
[0089] like Figure 3 As shown in the figure, the QRBOX_YOLOv5 industrial QR code locator is mainly divided into three modules: data processing module, network training and testing module, and industrial QR code positioning module. The data is processed in the data processing module, and the processed training set data is used for training in the network training and testing module. The weights generated by the training are used for testing in the network training and testing module to obtain the tested network weights.
[0090] In the actual operation process, python 3.7 and above are installed to build the development platform; JetBrains PyCharm 2019.1.1x64 is installed for easier development; after obtaining the tested weights, the tested weights are used to read the image through the industrial QR code positioning module to accurately locate the QR code to be located in real time.
Claims
1. An industrial two-dimensional code positioning method based on YOLOv5, characterized in that: include: S1: Collect photos containing QR code images, pre-process the photos, and obtain processed QR code images; S2: Generate corresponding labels for the processed QR code images using a label generator; In step S2, a corresponding label is generated for the processed two-dimensional code image, including: S2.1: Write a label generator to generate a label file for each image. A label file contains all the QR code information in the image. Draw a rotated rectangle in the image and calculate the coordinates of the four vertices of the rectangle relative to the image. S2.2: Draw a frame for the QR code in the image in the label generator, and convert the coordinates of the four vertices of the rectangular frame into the width and height w and h of the rectangular frame relative to the entire image, the coordinates of the center point x and y, and two parameters m and n; S2.3: Normalize the width and height w and h, the center point coordinates x and y, and the two parameters m and n related to the angle; S3: Perform data enhancement processing on the QR code image in the label to obtain a data set of enhanced images; S4: In the QRBOX_YOLOv5 industrial QR code locator, the constructed qrbox_yolov5s neural network is trained with the data-enhanced images to generate weights and tested. The training weights with the best test results are used as the trained QRBOX_YOLOv5 industrial QR code locator. The qrbox_yolov5s neural network is divided into four layers: input layer, Backbone layer, Neck layer and Prediction layer. The step S4 comprises: S4.1: The data has been rotated and translated in the data processing part. In the QRBOX_YOLOv5 industrial QR code locator, there is no need to use rotation and translation for data enhancement again. Only the cutting and splicing in the detector are used for data enhancement; S4.2: Add attention mechanism to qrbox_yolov5s network; S4.3: Use a new loss function at the prediction layer, the formula is as follows: loss=SmoothL1+(m 2 +n 2 -w 2 ) 2 ; S4.4: In the regression process, the sigmoid function is used to map m, and the tanh function is used to map n; S4.5: When training, set the batch-size to 4 or 2, and the input image size should be a multiple of 32 and not less than 3 times the real image size; S4.6: Use the training set of processed image data to train the constructed qrbox_yolov5s neural network; S4.7: Use the trained weights to input the test set into the qrbox_yolov5s neural network for testing; S5: Input the QR code image data to be identified into the trained QRBOX_YOLOv5 industrial QR code locator, perform real-time reasoning on the QR code to be located, and obtain the positioning result.
2. The industrial two-dimensional code positioning method based on YOLOv5 according to claim 1, characterized in that: In the step S1, collecting a photo containing a QR code image specifically includes: When collecting QR code images, the industrial scene is used as the background of the image, and the actual industrial application scene is simulated for shooting. The QR code image is read according to the actual industrial application scene. The pixel of the shooting camera is selected according to the camera pixel in actual application. According to the diversity of QR code types and industrial scenes in actual application, multiple QR code images are taken in multiple scenes. The QR code is recognized using conventional methods, and QR code images that cannot be recognized even if the positioning is successful are cleared. The number of QR code photos taken is between 1,000 and 10,000.
3. The industrial two-dimensional code positioning method based on YOLOv5 according to claim 1, characterized in that: In step S2.1, a label generator is written, and a label file is generated for each image. A label file contains all the QR code information in the image, and a rotating rectangular frame is drawn in the image, including: The label file is in txt text format. A label file contains all the QR code information in the image. Each QR code information occupies one line. Each rectangular box represents a QR code. Each QR code information contains 7 parameters. The first parameter is the type of QR code. Different types are represented by different numbers; the second and third parameters are the center point coordinates of the QR code rectangular box, represented by x and y; the fourth and fifth parameters are the width and height of the QR code, represented by w and h; the sixth and seventh parameters are m and n.
4. The industrial two-dimensional code positioning method based on YOLOv5 according to claim 1, characterized in that: In step S2.2, the coordinates of the four vertices of the rectangular frame are converted into the width w and height h of the rectangular frame relative to the entire image, and two parameters m and n, including: Take the right side of the image as the positive direction of the x-axis and the bottom as the positive direction of the y-axis, find the bottom point of the rectangle, and derive a dotted line from this point to the positive direction of the x-axis. This dotted line forms an angle with one side of the rectangle. Record this side of the rectangle as w, and the other side connected to it as h. Record the coordinates of the two vertices of this side, the point on the left (x1, y1) and the point on the right (x2, y2), m=x2-x1, n=y2-y1.
5. The industrial two-dimensional code positioning method based on YOLOv5 according to claim 1, characterized in that: In step S2.3, the width and height w and h, the center point coordinates x and y, and the two parameters m and n are normalized, specifically including: w, x, and m are divided by the image width, and h, y, and n are divided by the image height.
6. The industrial two-dimensional code positioning method based on YOLOv5 according to claim 1, characterized in that: In step S3, the specific steps of performing data enhancement processing on the QR code image in the label to obtain a data set of the enhanced image are: S3.1: The background part of the image remains unchanged, the QR code is rotated, and the images before and after the operation are saved; S3.2: The background part of the image remains unchanged, the QR code is translated, and the images before and after the operation are saved; S3.3: Generate corresponding labels for the newly synthesized images and use them as datasets; S3.4: Split the dataset into training set and test set in a ratio of 3:
1.
7. The industrial two-dimensional code positioning method based on YOLOv5 according to claim 6, characterized in that: In the step S3.1, the background in the image remains unchanged, and the QR code is rotated, which specifically includes: Record the coordinates of the center point of the QR code in the image; cut out the QR code in the image and save the pure QR code image and the background image after cutting; use Poisson fusion to make the blank part of the background image after cutting out the QR code consistent with the background image, and generate a background image after cutting with only the background; rotate the cut QR code at a random angle, and paste the rotated QR code into the background image through the coordinates of the original image corresponding to the center point of the QR code.
8. The industrial two-dimensional code positioning method based on YOLOv5 according to claim 6, characterized in that: In the step S3.2, the background in the image remains stationary, and the QR code is translated, specifically including: cutting out the QR code in the image and saving the pure QR code image and the background image after cutting, using Poisson fusion to make the blank part of the background image after the QR code is cut out consistent with the background image, and generating a background image after cutting that only has the background; and making the cut QR code translate a certain distance up, down, left, and right in the background image without moving out of the image.
9. The industrial two-dimensional code positioning method based on YOLOv5 according to claim 6, characterized in that: Generating a corresponding label for the newly synthesized image includes: Generate labels for data-augmented images and expand the dataset.
