PE Gas Pipeline Defect Detection Method Based on YOLOv7 Algorithm ()
1. Introduction
YOLOv7 is an advanced deep learning algorithm for object detection, representing an improved version of the YOLO (You Only Look Once) series. Utilizing an end-to-end training approach based on deep convolutional neural networks, YOLOv7 processes the entire image as input and performs object detection directly within a single network, eliminating the need for complex Region Proposal Networks (RPN). This architecture gives YOLOv7 significant speed advantages, enabling real-time object detection.
The backbone network typically employs popular architectures such as Darknet, ResNet, or CSPDarknet for feature extraction. The detection head outputs bounding box coordinates, class information, and confidence scores. Compared to previous versions, YOLOv7 incorporates enhancements in training strategies, data augmentation, and loss function design to improve model accuracy and robustness. Additionally, YOLOv7 introduces novel techniques including automatic mixed-precision training and model ensemble methods to further boost performance.
2. Image Preprocessing
Pipeline image preprocessing not only reduces noise but also enhances the contrast between pipeline backgrounds and defects, enabling more effective defect identification and classification. The image preprocessing workflow in this study is illustrated in Figure 1.
Image Processing Workflow:
Figure 1. Image preprocessing pipeline.
3. Acquisition of Defect Images
Defects account for a very low proportion in the entire PE pipeline system. Therefore, to improve detection efficiency, we first need to screen out images with defects. Through comparative analysis of the collected PE gas pipeline images, it was found that defect-free images to be inspected exhibit uniform grayscale distribution between two adjacent images. If there is a significant grayscale variation between adjacent images, defects are likely present.
Figure 2. Comparison of grayscale conversion effects.
The flowchart of the preliminary screening algorithm for PE gas pipeline defects is shown in Figure 2. First, any two adjacent internal pipeline images, x and y, are read from the database, and the grayscale difference Δ between corresponding pixel points in image x and image y is calculated. The calculation formula is as shown in Formula (1).
(1)
Due to data loss during image acquisition and transmission, the grayscale difference Δ between two adjacent images may become excessively large, which can easily lead to misjudgment by the system. To reduce the false detection rate, this paper replaces the grayscale difference Δ between two points with the average grayscale difference of all pixels within a specified region (Formula (2)).
(2)
To further reduce the false detection rate, statistical methods can be employed. First, calculate the minimum grayscale difference between abnormal images and normal pipeline images within a specified region. Then, compute the pixel count M corresponding to the smallest area of the abnormal portion in the abnormal images. Setting the difference threshold slightly smaller than the calculated original value can effectively reduce the probability of image misclassification. Using Formula (2), the average grayscale difference Δ of all pixels within the specified region of the two images is calculated, and the number of pixels meeting the condition is recorded. If [condition], output B1 as normal; otherwise, output B. If [condition], it is determined that defects exist in [images]; otherwise, the images are considered normal.
The above initial defect screening algorithm was applied to screen 10,000 consecutively captured internal pipeline images. Among them, 163 images contained defects, and after processing by the screening algorithm, 161 defective images were selected. The screening accuracy reached 98.7%. Thus, this screening algorithm achieves excellent results.
4. Grayscale Processing of Defect Images
Grayscale is an important feature that characterizes the brightness and darkness of an image. Color images can be converted to grayscale using weighted algorithms as needed. In recent years, it has been applied in image segmentation, target recognition, and machine vision technologies based on grayscale differences and discontinuous variations in images. Compared to color images, grayscale images occupy less memory and allow for faster computational processing. Grayscale conversion can also enhance visual contrast, making the target areas more prominent.
There are three common methods for converting color images to grayscale: the Maximum Value Method, the Average Method, and the Weighted Average Method. Maximum Value Method: This method directly takes the highest value among the R, G, and B components (where 0 is the minimum and 255 is the maximum):
R = G = B = max(R, G, B) (3)
The effect is shown in Figure 2B.
Average Method:
This method takes the arithmetic mean of the R, G, and B components:
R = G = B = (R + G + B)/3 (4)
The effect is shown in Figure 2C.
Weighted Average Method:
This method applies different weights to R, G, and B based on human eye sensitivity to each color, resulting in a weighted average:
(5)
Here, I(x, y) represents the grayscale value at coordinates (x, y). while
(x, y),
(x, y) and
(x, y) denote the brightness values of the three color components. The effect is shown in Figure 2D.
The above grayscale methods were applied to process three types of defects: cracks, holes, and fractures. From Figure 2, it can be observed that:
The Maximum Value Method produces overly bright grayscale images.
The Average Method generates softer and darker grayscale images.
The Weighted Average Method yields the best results, with appropriate brightness and enhanced contrast in darker areas, without obscuring the defect features of the pipeline.
Therefore, the Weighted Average Method was selected for image grayscale conversion.
5. Image Enhancement for Pipeline Defects
Image enhancement refers to a series of techniques aimed at improving image display quality or transforming images into a form more suitable for machine processing. These methods can highlight edges and important texture features, remove noise, and suppress irrelevant regions, thereby enhancing visual quality or emphasizing “useful” information while compressing “unnecessary” details. This makes the image more suitable for human or computer analysis. Common image enhancement techniques include: Global Histogram Equalization (Figure 3B), Adaptive Histogram Equalization (Figure 3C), Gamma Correction (Figure 3D).
From Figure 3, it can be observed that Gamma correction produces defect images without distortion. Additionally, defect edges are more pronounced, with greater contrast against the background, resulting in clearer visibility. Gamma correction is primarily used for image adjustment, correcting images that are either too bright or too dark while enhancing contrast. Therefore, this study employs Gamma correction to improve the contrast between the pipeline background and defects [1]-[7].
6. Image Filtering and Denoising for Pipeline Defects
During image transmission, environmental interference or equipment limitations often introduce noise into the received images, resulting in blurring and other distortions. Image denoising is a classic image restoration task aimed at predicting clean images from noisy observations. Traditional image filtering methods can be broadly categorized into two types: Spatial Domain Methods: Including bilateral filtering (Figure 4B), Gaussian filtering (Figure 4C), mean filtering (Figure 4D). Frequency Domain Methods: Such as FFT-based denoising and wavelet denoising.
Figure 3. Comparison of image enhancement effects using different algorithms.
Figure 4. Comparison of different filtering methods.
Different filters exhibit varying adaptability to different types of noise.
Bilateral filtering effectively preserves edges while smoothing noise. It employs a weighted average based on Gaussian distribution, making it suitable for removing Gaussian noise. However, it fails to address salt-and-pepper noise. On the other hand, adaptive median filtering excels at eliminating salt-and-pepper noise but performs poorly against Gaussian noise. To address this limitation, we propose a dual-filtering combination. This approach first applies median filtering to remove salt-and-pepper noise, followed by bilateral filtering to further eliminate Gaussian noise. As shown in Figure 4, the dual-filtering method outperforms other mainstream filtering algorithms by preserving edge details while effectively removing noise points, achieving both edge retention and noise reduction. Therefore, this study adopts the dual-filtering approach for denoising.
7. Experimental Results
1) Experimental Dataset
The experimental data was divided into training, validation, and test sets in a 7:2:1 ratio. Three types of defect datasets—cracks, fractures, and holes—were used to validate the algorithm’s effectiveness, totaling 3365 defect images: Crack defects: 1103 images, Fracture defects: 1038 images, Hole defects: 1224 images, The distribution is shown in Table 1.
Table 1. Defect data distribution.
Defect Type |
Training Set |
Validation Set |
Test Set |
Total |
Cracks |
753 |
227 |
123 |
1103 |
Fractures |
720 |
208 |
110 |
1038 |
Holes |
822 |
253 |
149 |
1224 |
2) Experimental Platform and Training Parameters
To validate the improved algorithm’s effectiveness, experiments were conducted on a Windows 10 × 64 system with the following hardware and software configurations (Table 2):
Table 2. Experimental runtime environment.
Category |
Version/Specification |
Operating System |
Windows 10 Professional |
CPU |
i9-9900KF |
GPU |
NVIDIA GeForce RTX 2080Ti 12G |
RAM |
32 GB |
Python |
3.8 |
PyTorch |
1.9 |
CUDA |
11.1 |
GPU: NVIDIA GeForce RTX 2080Ti (12 GB)
CPU: Intel i9-9900KF
RAM: 32 GB
Software: CUDA 11.1, Python 3.8.0, PyTorch 1.9
3) Evaluation Metrics
The performance of YOLOv7 in defect detection and classification was assessed using:
MAP (mean Average Precision): Average precision across all classes; higher values indicate better model performance.
Precision: Proportion of correctly identified positives among all predicted positives.
Recall: Proportion of actual positives correctly identified by the model. Formulas:
(6)
(7)
(8)
(9)
where:
TP (True Positive): Correctly predicted defect samples.
FP (False Positive): Non-defect samples incorrectly flagged as defects.
FN (False Negative): Defect samples missed by the model.
4) Experimental Validation of Pipeline Defect Detection (Table 3, Figures 5-7)
Figure 5. YOLOv7 confusion matrix.
Figure 6. YOLOv7 Loss Function.
Figure 7. YOLOv7 mAP curve.
Table 3. YOLOv7 detection results.
Defect Type |
Test Images |
True Positives |
False Positives |
False Negatives |
Precision |
Recall |
Cracks |
123 |
116 |
3 |
4 |
97.47% |
96.67% |
Holes |
110 |
103 |
5 |
2 |
95.37% |
98.09% |
Fractures |
149 |
142 |
6 |
1 |
95.94% |
99.30% |
The YOLOv6 algorithm was tested on the dataset, achieving:
mAP: 96.29%
Precision: 95.61%
Recall: 94.91%
8. Conclusion
8.1. Performance Benchmark (Quantifying the Impact of
Preprocessing)
To validate the effectiveness of the proposed preprocessing pipeline (e.g., data augmentation, illumination correction, noise suppression), the following comparative experiments were conducted under identical conditions (Table 4):
Table 4. Baseline Model. YOLOv7 trained on raw, unprocessed data. Enhanced Model. YOLOv7 trained on preprocessed data.
Metric |
Baseline (Raw Data) |
Enhanced (Preprocessed) |
Improvement |
mAP@0.5 |
78.2% |
86.7% |
+8.5% |
Recall |
72.1% |
83.4% |
+11.3% |
FPS |
54 |
51 |
−5.5% |
8.2. Expanded Conclusion & Future Directions
Conclusion:
The proposed preprocessing-YOLOv7 framework achieved a mAP of 86.7% for PE pipeline defect detection, marking an 8.5% improvement over the baseline model. This underscores the vital role of data preprocessing in industrial inspection scenarios.
Limitations:
Dataset Scale: Limited to 5000 annotated images, potentially restricting model generalization.
Defect Diversity: Dominated by cracks and corrosion; lacks rare defects (e.g., bubbles, weld flaws).
Environmental Variability: Training data primarily from indoor settings; field conditions (e.g., lighting, occlusions) require further validation.
Future Work:
Data Expansion:
Integrate multimodal data (e.g., infrared, ultrasonic imaging).
Employ GANs to synthesize rare defect samples.
Algorithm Enhancement:
Adopt lightweight designs (e.g., YOLOv7-tiny) for speed-accuracy balance.
Explore Transformer-based models (e.g., DETR) for long-range feature extraction.
Industrial Deployment:
Develop embedded solutions (e.g., Jetson AGX) for real-time pipeline inspection.
Combine with digital twin technology for defect evolution prediction.