# Research on a Multi-Type Barcode Defect Detection Model Based on Machine Vision
## 3 Model Construction
### Barcode Localization Submodule-YOLOv8n  
In the multi-type barcode defect detection framework proposed in this paper, YOLOv8n serves as the first-stage localization network, responsible for rapid localization and category discrimination. Its output directly determines the effectiveness of subsequent feature extraction and defect classification processes. YOLOv8n follows the Backbone-Neck-Head three-tier hierarchical structure, but integrates the latest components, such as Cross-Stage-Full (C2F) and Separable Dilated Pyramid Pooling (SPPFK-k), for lightweight design. The overall structure of YOLOv8n is shown in Figure 4 and can be subdivided into the following modules. (1) Backbone: The backbone primarily consists of alternating stacks of CBS (Conv-BN-SiLU), C2F, and Bottleneck units. Multiple layers of C2F achieve feature reuse through cross-stage residuals and grouped convolutions, significantly reducing parameter scale. The Bottleneck unit constructs shortcut paths between 1 \(\times\) 1 and 3 \(\times\) 3 convolutions to mitigate gradient vanishing. The Backbone outputs feature maps P8, P16, and P32 at three scales (1/8, 1/16, and 1/32 of the original image size H \(\times\) W). (2) Neck: The neck uses an improved FPN+PAN structure, with the addition of SPPFK-5 (Separable Dilated Spatial Pyramid Pooling) after P32. It employs convolutions with different dilation rates \(\{k=5,9,13\}\) to enhance the receptive field, thus capturing the geometric features of both narrow 1D barcodes and near-square 2D barcodes. (3) Head: The head implements an anchor-free decoupled detection head, separating the bounding box regression and classification branches. The output scales are \(\{\mathrm{S=8,16,32}\}\), and for any scale \(s\in\mathrm{S}\), the feature map dimensions are \(\mathrm{H_{s}=H/S}\), \(\mathrm{W_{s}=w/S}\). The output tensor for a single scale is defined as:  
\[\mathcal{O}^{(s)}\in\mathbb{R}^{H_{\mathrm{S}}\times W_{s}\times(4+1+C)} \tag{1}\]where \"4\" represents the bounding box regression values \((\hat{\kappa},\hat{y},\hat{w},\hat{h})\), \"1\" denotes the object confidence, and C = 2 corresponds to the number of barcode categories (1D and 2D). The decoupled design optimizes the regression and classification branches independently, reducing gradient coupling and improving convergence stability. The core mechanism is modeling barcode detection as a multi-task learning problem, with the optimization objective function:  
\[\mathcal{L}=\lambda_{cls}BCE\ (p,p^{*})+\ \lambda_{obj}BCE\ (o,o^{*})+\ \lambda_{box} CloU\ (b,b^{*}) \tag{2}\]  
where BCE (\(\cdot\)) represents binary cross-entropy, and (p, o, b) correspond to class probabilities, object confidence, and bounding box predictions, respectively, with \"*\" denoting the ground truth labels. The bounding box regression uses Complete-IoU:  
\[CIoU\ =1-IoU+\frac{\left\|\ c-c^{*}\right\|^{2}}{d_{max}^{2}}+\alpha v \tag{3}\]  
where \(c\) and \(c^{*}\) are the predicted and ground truth box centers, \(d_{\max}\) is the diagonal length of the bounding boxes, and \(v\) measures aspect ratio consistency, serving as a weighting coefficient. The anchor-free scheme directly regresses the box offset from the grid center, eliminating the need for predefined prior box sets and simplifying the unified modeling of 1D objects with extreme aspect ratios and 2D near-square objects. YOLOv8n plays a critical role in the framework by handling localization and cropping. After optimization with Equation (2), the network infers the input image I, producing a set of bounding boxes:  
\[\mathfrak{B}=\left\{(b_{i},c_{i})\right\}_{i=1}^{N},\ c_{i}\in\{0,1\},\ b_{i}= \left(\ x_{i}\ y_{i}\ w_{i}\ h_{i}\right) \tag{4}\]  
Figure 4: YOLOv8n Model Architecture. Lightweight first-stage localization network for multi-type barcode detection. Features a CBS/C2F/Bottleneck backbone extracting multi-scale outputs (P8, P16, P32), an FPN+PAN neck with multi-dilation-rate SPPFK-5, and an anchor-free decoupled head.