# Text Detection and Recognition for X-ray Weld Seam Images
## 2 X-ray Weld Seam Image Text Recognition Model Framework
### Text Detection Model Based on DBNet  
DBNet is a classical text detection model based on segmentation algorithms. Its principle revolves around the introduction of a differentiable binarization method, addressing the challenge of threshold selection inherent in traditional segmentation algorithms. The network architecture of DBNet primarily consists of two modules: the Feature Pyramid Network (FPN) [20] and differentiable binarization (DB). 1. The model initially utilizes a generic backbone network to extract the image features, with ResNet18 chosen to obtain feature maps at different scales. Feature maps corresponding to 1/2, 1/4, 1/8, 1/16, and 1/32 proportions of the original image are obtained. Subsequently, a top-down 2\(\times\) downsampling is performed, and the resulting feature maps are merged with those obtained from bottom-up processing of the same size. Convolutional operations are then employed to eliminate the aliasing effects introduced during upsampling. After this FPN structure, four feature maps of sizes 1/4, 1/8, 1/16, and 1/32 of the original image are obtained. These feature maps are upsampled and unified into a 1/4-sized feature map, denoted as \(F\). 2. The feature map \(F\) undergoes a series of convolution and transposed convolution operations to obtain probability map \(P\). A similar process is applied to obtain threshold map \(T\). 3. The probability map \(P\) and the threshold map \(T\) undergo the DB operation, resulting in approximate binary map \(B\). In traditional image segmentation algorithms, after obtaining the probability map, a standard binarization method is typically employed. This method involves processing the probability map using standard binarization, where pixels below a specified threshold are set to 0, and pixels above the threshold are set to 1. The formula is as follows:  
\[B_{i,j}=\left\{ \begin{aligned} & 1,\textit{if }P_{i,j}\geq t\\ & 0,\textit{ otherwise}\end{aligned} \right. \tag{1}\]  
where \(t\) represents a pre-defined threshold, \(B\) is the binary map, \(P\) denotes the probability map, and (_i,j_) denotes the pixel coordinates. It is evident from this that standard binarization is non-differentiable, thereby precluding its integration into a network for optimization learning. Differentiable binarization is the approximation of the step function in standard binarization, and the formula is as follows:  
\[\mathcal{B}_{i,j}=\frac{1}{1+e^{-k(P_{i,j}-T_{ij})}} \tag{2}\]  
Figure 1: Weld seam image text recognition framework. where \(k\) is the dilation factor, \(\mathcal{B}\) is the approximate binary image, \(P\) is the probability map, \(T\) is the threshold map, and (\(i\),\(j\)) are the coordinates of the pixel. This form of approximated binarization function behaves similarly to a standard binarization function, but it is differentiable, allowing for optimization during the training of the segmentation networks. Differentiable binarization with adaptive thresholds not only distinguishes text regions from the background but also separates closely connected text instances. However, this approach may encounter issues of incomplete detection in long texts where the characters are closely connected. It might incorrectly detect a longer text as two or more shorter texts, thereby diminishing the performance of the detection model and adversely affecting the subsequent text recognition stages. In accordance with the characteristics of weld seam image data, multiple short text lines are primarily distributed in the upper part of the weld seam images, while the lower part typically contains only one long text line. Consequently, the issue of incomplete detection primarily arises in the lower part of the images. To address this problem, we introduces a post-processing method based on the characteristics of the dataset. It involves merging multiple detected text line boxes into a single long text detection box, thereby improving the accuracy and performance of the detection model.