Defect detection method, system, computer device and readable storage medium

By generating a workpiece foreground mask and contour as a two-dimensional alignment template, the training samples for industrial defect detection technology are aligned and segmented in two dimensions. This solves the problems of insensitivity to small defects and large background interference in existing technologies, and achieves efficient and accurate defect detection and visualization.

CN122243870APending Publication Date: 2026-06-19SHIYAN INTELLIGENT TECH (GUANGZHOU) CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
SHIYAN INTELLIGENT TECH (GUANGZHOU) CO LTD
Filing Date
2026-02-06
Publication Date
2026-06-19

AI Technical Summary

Technical Problem

Existing defect detection technologies are insensitive to small defects in high-resolution images, suffer from significant background interference, require large amounts of computation, lack ROI information utilization, have fragmented training and inference processes, lack visualization and traceability capabilities, and are difficult to implement stably in industrial systems with complex backgrounds and multiple camera poses.

Method used

By generating a workpiece foreground mask and/or contour as a 2D alignment template, 2D alignment and block processing of training sample images are performed to generate a training dataset. The defect detection model is trained using only normal samples for parameter updates. Combined with the discriminator, block-level anomaly scores are output to generate pixel-level anomaly heatmaps.

Benefits of technology

It improves the sensitivity to local defects such as small scratches and bubbles, reduces computational load and background interference, and enables fine-grained defect localization and visualization, making it easy to deploy in industrial environments.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122243870A_ABST
    Figure CN122243870A_ABST
Patent Text Reader

Abstract

This application discloses a defect detection method, system, computer device, and readable storage medium. The defect detection method includes: acquiring a template reference image; processing the template reference image; performing two-dimensional alignment processing on a set of normal training sample images to obtain corresponding two-dimensional transformation parameters; mapping a block layout template to each normal training sample image according to the two-dimensional transformation parameters, and slicing to extract a set of training image blocks to form a training dataset; training a defect detection model based on the training dataset; performing two-dimensional alignment processing on an image of the workpiece to be tested to obtain corresponding two-dimensional transformation parameters, and mapping the block layout template to the image of the workpiece to be tested according to the corresponding two-dimensional transformation parameters, and slicing to extract a set of image blocks to be tested; inputting the set of image blocks to be tested into the trained defect detection model for inference and outputting defects. This application can improve defect detection accuracy, reduce computational load, and stabilize model performance.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of visual recognition technology, and in particular to defect detection methods, systems, computer equipment, and readable storage media. Background Technology

[0002] In the production process of industrial products such as glass substrates, metal parts, plastic parts, and composite material workpieces, it is generally necessary to conduct full inspection of the appearance of the workpieces to detect defects such as scratches, breakage, missing edges, contamination points, and coating defects. Automated optical inspection (AOI) and machine vision inspection technologies are widely used in defect detection.

[0003] In existing technologies, there is a class of defect detection methods based on unsupervised or one-class learning, which are trained using only normal samples. For example, they utilize autoencoders, generative adversarial networks, or feature memories to model the distribution of normal samples in the latent space and regard regions that deviate from this distribution as anomalies. These methods are more suitable for industrial quality inspection. With the development of pre-trained visual models, more and more methods are trying to model the distribution of normal samples in the multi-layer feature space of large-scale pre-trained backbone networks. They use pre-trained convolutional networks or visual backbone networks to extract features from multiple layers of the input image, divide the feature map into local patches, and construct a statistical distribution model of normal sample features in the feature space. During the inference stage, an anomaly score is calculated for each patch, and pixel-level heatmaps are obtained through interpolation and upsampling to achieve anomaly region localization. During the training stage, only normal samples are used and no manual annotation of defect regions is required.

[0004] However, existing technologies have several problems. First, high-resolution images typically require overall scaling or cropping, making them insensitive to small defects in high-resolution workpieces. Scaling or coarse-grained cropping can cause small scratches, bubbles, and other defects to be averaged out or buried in the feature space, resulting in over-smoothing of small defects and blurred pixel-level heatmap boundaries. Second, they do not fully utilize ROI information, generally assuming the entire image is the region of interest, failing to effectively exclude background areas, leading to high false alarm rates and high computational costs. Third, they lack coordination with upstream 2D ​​matching or pose estimation modules, failing to fully utilize existing foreground masks and workpiece positioning information, resulting in significant background interference and severe computational redundancy, making it difficult to stably implement in industrial systems with complex backgrounds, repetitive scenes, and multi-camera poses. Fourth, the training and inference processes are disconnected. Some solutions use complex feature processing and data augmentation during the training phase, while simplifying the process during the inference phase, leading to unstable performance and difficulties in parameter tuning. Fifth, there is a lack of complete visualization and traceability capabilities. There is a lack of systematic storage and visualization mechanisms for intermediate results (foreground mask, block layout, anomaly score, heat map). Once the detection results are abnormal, it is difficult for engineers to locate the source of the problem in a timely manner. Summary of the Invention

[0005] To overcome one or more technical problems existing in the prior art, the embodiments of this application provide a defect detection method, system, computer equipment, and readable storage medium to improve defect detection accuracy, reduce computational load, and stabilize model performance.

[0006] In a first aspect, this application provides a defect detection method, comprising: Obtain a set of normal training sample images corresponding to the type of workpiece to be inspected, and select at least one reference defect-free image as the template baseline image; The template reference image is processed to obtain the workpiece foreground mask and / or workpiece outline; Using the workpiece foreground mask and / or workpiece contour as a two-dimensional alignment template, perform two-dimensional alignment processing on each image in the normal training sample image set to obtain the corresponding two-dimensional transformation parameters. Generate a block layout template based on the workpiece foreground mask and / or workpiece outline; Based on the two-dimensional transformation parameters, the block layout template is mapped to each normal training sample image, and the slice processing is used to extract the training image block set to form a training dataset. The defect detection model is trained based on the training dataset, enabling the defect detection model to learn the normal feature distribution of the type of workpiece to be detected; Two-dimensional alignment processing is performed on the image of the workpiece under test using a two-dimensional alignment template to obtain the corresponding two-dimensional transformation parameters. The block layout template is then mapped to the image of the workpiece under test according to the two-dimensional transformation parameters corresponding to the image of the workpiece under test and sliced ​​to extract the set of image blocks under test. The set of image blocks to be tested is input into the trained defect detection model for inference, and the abnormal score and abnormal area are output to determine whether there are defects in the image of the workpiece to be tested.

[0007] One implementation of the first aspect involves processing the template reference image, including: Determine the region of interest for the workpiece in the template reference image; A template reference image containing the region of interest is input into the segmentation model for inference to generate a workpiece foreground mask. And / or input a template reference image containing the region of interest into the contour extraction model for inference to generate the workpiece contour.

[0008] One implementation of the first aspect involves performing two-dimensional alignment processing on normal training sample images, including: The normal training sample images are matched with the two-dimensional alignment template to estimate the two-dimensional transformation parameters, which include at least translation parameters and rotation angles. Based on the two-dimensional transformation parameters, the workpiece foreground mask and / or workpiece contour of the template reference image are mapped to the corresponding normal training sample image to generate the aligned foreground mask of the normal training sample image. Performing two-dimensional alignment processing on the image of the workpiece to be tested includes: The image of the workpiece to be tested is matched with a two-dimensional alignment template to obtain the corresponding two-dimensional transformation parameters. The two-dimensional transformation parameters include at least translation parameters and rotation angles. Based on the two-dimensional transformation parameters corresponding to the image of the workpiece to be tested, the workpiece foreground mask and / or workpiece contour of the template reference image are mapped to the image of the workpiece to be tested, thereby generating the aligned foreground mask and / or aligned contour of the image of the workpiece to be tested.

[0009] Preferably, the block mapping and slicing process of the normal training sample images after two-dimensional alignment includes: The orientation bounding box is calculated based on the workpiece foreground mask and / or workpiece contour of the template reference image, and a block mesh is generated within the orientation bounding box to form a block layout template. Based on the two-dimensional transformation parameters, the block layout template is mapped to each normal training sample image, and rotation, translation and boundary checks are performed to extract the training image block set to form a training dataset.

[0010] Preferably, the block mapping and slicing processing of the image of the workpiece to be tested includes: Call the block layout template; Based on the two-dimensional transformation parameters of the image of the workpiece to be tested, the block layout template is mapped to the image of the workpiece to be tested, and rotation, translation, and boundary checks are performed to extract the set of image blocks to be tested.

[0011] One implementation of the first aspect is that the defect detection model includes a feature extraction module, a feature processing module, and an anomaly scoring module; The feature extraction module is used to extract multi-layer features from the input training image patches to form multi-scale feature maps; The feature processing module is used to block-scale feature maps and aggregate the features within each block to obtain block-level aggregated features. The anomaly scoring module includes a discriminator and an anomaly generation unit; The discriminator is used to receive block-level aggregated features and output block-level anomaly scores; The anomaly generation unit is used to generate anomaly detection maps based on block-level anomaly scores.

[0012] One implementation of the first aspect is that, when training the defect detection model, only normal training sample images are used to update the model parameters, while defective sample images are used for threshold setting or performance evaluation and are not involved in the model parameter update.

[0013] Secondly, this application provides a defect detection system, including a training dataset generation module, an offline training module, and an online detection module; The training dataset generation module is used to obtain a set of normal training sample images corresponding to the type of workpiece to be detected, and select at least one reference defect-free image as a template reference image. The template reference image is processed to obtain the workpiece foreground mask and / or workpiece contour. The workpiece foreground mask and / or workpiece contour is used as a two-dimensional alignment template. Two-dimensional alignment processing is performed on each image in the set of normal training sample images to obtain the corresponding two-dimensional transformation parameters. A block layout template is generated based on the workpiece foreground mask and / or workpiece contour. The block layout template is mapped to each normal training sample image according to the two-dimensional transformation parameters. The slice processing is used to extract the training image block set to form the training dataset. The offline training module is used to train the defect detection model based on the training dataset, enabling the defect detection model to learn the normal feature distribution of the type of workpiece to be detected; The online detection module is used to perform two-dimensional alignment processing on the image of the workpiece under test using a two-dimensional alignment template to obtain the corresponding two-dimensional transformation parameters. The block layout template is then mapped to the image of the workpiece under test according to the two-dimensional transformation parameters and sliced ​​to extract the set of image blocks to be tested. The set of image blocks to be tested is then input into the trained defect detection model for inference, and the model outputs anomaly scores and anomaly regions to determine whether there are defects in the image of the workpiece under test.

[0014] Thirdly, this application provides a computer device, including a memory and a processor; the memory stores a computer program that can be executed by the processor, and when the processor executes the computer program, it is used to implement the defect detection method of any of the foregoing.

[0015] Fourthly, this application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is used to implement the defect detection method of any of the foregoing embodiments.

[0016] Compared with existing technologies, the defect detection method, system, computer equipment, and readable storage medium of this application have the following advantages: The defect detection model uses only normal training samples to update model parameters in order to learn the normal feature distribution of the workpiece in the feature space. The defect detection model outputs block-level anomaly scores through a discriminator, thereby having higher sensitivity to local defects such as small scratches and bubbles. Since no real defect samples are required to participate in parameter updates during the training phase, the cost of defect collection and annotation can be significantly reduced. The standardization and reuse of the detection area can be achieved through the generation of training datasets, enabling fine-grained defect localization. The detection range is limited to the workpiece body area, reducing background interference and computational load. Through a unified training and inference data organization method, the training process, evaluation process, model export and online inference interface can be completed under the same framework, which is convenient for containerized deployment in industrial environments. Attached Figure Description

[0017] Figure 1 This is a flowchart illustrating one of the defect detection methods in this application.

[0018] Figure 2 for Figure 1 A schematic diagram of the network structure of the defect detection model in the defect detection method.

[0019] Figure 3 for Figure 1 A schematic diagram illustrating the process of processing the template reference image in a defect detection method.

[0020] Figure 4 for Figure 1 A schematic diagram of the process of two-dimensional alignment of normal training sample images in the defect detection method.

[0021] Figure 5 for Figure 4 A schematic diagram illustrating the process of block mapping and slicing of normal training sample images after 2D alignment.

[0022] Figure 6 for Figure 1 A schematic diagram of the process for two-dimensional alignment of the workpiece image in the defect detection method.

[0023] Figure 7 for Figure 6 A schematic diagram of the process of block mapping and slicing the image of the workpiece to be tested after 2D alignment.

[0024] Figure 8 for Figure 1 The diagram illustrates the process of using a defect detection model to detect defects in a workpiece online.

[0025] Figure 9 This is a schematic diagram of the framework structure of one of the defect detection systems in this application. Detailed Implementation

[0026] Please refer to the diagrams, where the same component symbols represent the same components. The principles of this application are illustrated by way of example implementation in a suitable operating environment. The following description is based on the specific embodiments of this application exemplified, and should not be construed as limiting other specific embodiments not detailed herein.

[0027] In the following description, specific embodiments of this application will be illustrated with reference to the steps and symbols of operations performed by one or more computers, unless otherwise stated. Therefore, it will be understood that these steps and operations, which are mentioned several times as being performed by a computer, include manipulation by a computer processing unit representing electronic signals of data in a structured format. This manipulation transforms the data or maintains it at a location in the computer's memory system, which can be reconfigured or otherwise alter the operation of the computer in a manner well known to those skilled in the art. The data structure maintained by the data is the physical location of the memory, which has specific characteristics defined by the data format. However, the principles of this application are described in the foregoing text, which is not intended to be limiting, and those skilled in the art will understand that many of the steps and operations described below can also be implemented in hardware.

[0028] As used herein, the terms “component,” “module,” “system,” “interface,” “process,” etc., generally refer to computer-related entities: hardware, a combination of hardware and software, software, or software in execution. For example, a component can be, but is not limited to, a process running on a processor, a processor, an object, an executable application, an executing thread, a program, and / or a computer. As illustrated, both an application running on a controller and the controller itself can be components. One or more components may reside within an executing process and / or thread, and components may be located on a single computer and / or distributed across two or more computers.

[0029] This document provides various operations of the embodiments. In one and / or more embodiments, one or more of the operations may constitute computer-readable instructions stored on one or more computer-readable media, which, when executed by an electronic device, will cause a computing device to perform the operations. The order in which some or all of the operations are described should not be construed as implying that these operations must be sequentially related. Those skilled in the art will understand alternative orderings that have the benefits of this specification. Moreover, it should be understood that not all operations are required to be present in every embodiment provided herein.

[0030] The first embodiment of this application is described below.

[0031] Please refer to Figures 1 to 8 The defect detection method in this embodiment includes the following steps.

[0032] S1, the stage of generating the training dataset.

[0033] In step S1, during the generation of the training dataset, the foreground mask and / or workpiece contour and the block layout template are mapped to the normal training sample image set through two-dimensional alignment based on the template reference image, and the training image block set is extracted to form the training dataset. The specific process is as follows.

[0034] S11. Collect the set of normal training sample images corresponding to the type of workpiece to be inspected.

[0035] The normal training sample image set focuses on defect-free workpieces of the same specifications and material type. These images are acquired by industrial cameras under production line background conditions that closely resemble actual online inspection. Meanwhile, to enhance appearance diversity, external texture libraries / datasets can be introduced as a source of texture enhancement in the normal training sample images. These textures are used in conjunction with the normal training sample images acquired by the industrial cameras for subsequent data augmentation processing. However, external textures cannot replace the acquisition of normal training sample images.

[0036] S12. From the normal training sample image set, select at least one reference defect-free image that can represent the standard appearance and posture of the workpiece as the template reference image, and select multiple high-quality reference defect-free images for template stability verification and parameter selection.

[0037] The selection of reference defect-free images must meet the following conditions: the workpiece outline is complete, there is no occlusion, no obvious motion blur, no abnormal highlights, no shadow interference, and other process inspections confirm that there are no appearance defects.

[0038] S13. Process the template reference image to generate a workpiece foreground mask and / or workpiece contour to characterize features such as workpiece contour and foreground region.

[0039] Reference Figure 3 As shown, the process of generating the workpiece foreground mask and / or workpiece contour includes: S131. Mark the region of interest (ROI) of the workpiece on the template reference image; S132. Input the template reference image containing the region of interest of the workpiece into the segmentation model for inference, output the workpiece mask and form the workpiece foreground mask; And / or input a template reference image containing the region of interest into the contour extraction model for inference to generate the workpiece contour.

[0040] In other implementations, the segmentation model can be semantic segmentation, threshold segmentation, GrabCut, or other interactive segmentation models; the annotation of the region of interest and the foreground mask can be generated once on the template reference image and reused in subsequent two-dimensional alignment, block mapping, and data augmentation constraints to reduce background interference and improve processing efficiency; in this embodiment, the SAM model is used to infer the foreground mask of the workpiece from the region of interest of the workpiece, and the workpiece contour is inferred using edge detection algorithms, etc.

[0041] S14. Use the workpiece foreground mask and / or workpiece contour of the template reference image as a two-dimensional alignment template to perform two-dimensional alignment on the workpiece in the normal training sample image to eliminate translation and rotation deviations introduced by production line shooting.

[0042] Considering the potential for workpiece offset relative to the worktable, slight posture changes, and imaging timing differences in actual production, it is necessary to perform two-dimensional alignment processing on the normal training sample images; refer to Figure 4 As shown, the two-dimensional alignment process includes: S141. Perform template matching between the normal training sample image and the two-dimensional alignment template to estimate the two-dimensional transformation parameters; the two-dimensional transformation parameters include at least translation parameters and rotation angles. S142. Based on the two-dimensional transformation parameters, the workpiece foreground mask and / or workpiece contour of the template reference image are mapped to the corresponding normal training sample image to obtain the aligned foreground mask and / or aligned contour of the normal training sample image; optionally, a small number of normal training sample images can also be used for verification and correction using the generated mask and / or contour. S143. Output the normal training sample image after two-dimensional alignment and its aligned foreground mask and / or aligned contour; optionally, the aligned foreground mask and / or aligned contour are superimposed on the normal training sample image before being output for quality inspection and visualization.

[0043] The workpiece foreground mask and / or workpiece contour are masks and / or contours obtained based on the template reference image, and the aligned foreground mask and / or aligned contour are masks and / or contours corresponding to the workpiece foreground mask and / or workpiece contour after being mapped and propagated on the image.

[0044] S15. Perform block mapping and slicing processing on the normal training sample images after two-dimensional alignment, and extract the training image block set as the training dataset.

[0045] Reference Figure 5 As shown. The process of block mapping and slicing includes: S151. Calculate the oriented bounding box (OBB) based on the workpiece foreground mask and / or workpiece contour of the template reference image, and generate a block mesh within the oriented bounding box to form a block layout template; optionally, optimize the block size according to a preset target, and filter the effectiveness of the blocks based on the foreground proportion threshold. The block layout template includes parameters such as block size, block position and orientation, and effective block filtering threshold. S152. For normal training sample images, the block layout template is mapped onto the corresponding normal training sample images according to the two-dimensional transformation parameters obtained in step S141. After performing slicing processing such as rotation transformation, coordinate transformation, translation positioning, and boundary checking in sequence, the training image block set is extracted, and the entire training image block set corresponding to all normal training sample images is used as the training dataset.

[0046] S2, Offline training of the defect detection model using the training dataset.

[0047] In step S2, during the offline training of the defect detection model, the defect detection model is trained offline based on the training dataset generated in step S1, so that the defect detection model learns the normal feature distribution and obtains model parameters. The model parameters are updated only based on normal training samples. Optionally, samples containing defects are introduced as validation / test data for threshold setting, model selection, or performance evaluation. Samples containing defects do not participate in the model parameter update.

[0048] In step S2, before training, data augmentation is performed on the training image block set corresponding to each normal training sample image. Optionally, samples containing defects can also be configured as verification / test data for threshold setting, model selection, or performance evaluation. Samples containing defects do not participate in the model parameter update during the training process. After the training image blocks are data augmented, they are then input into the defect detection model to be trained.

[0049] Reference Figure 2 As shown. In step S2, the defect detection model uses a discriminator-based anomaly detection algorithm to pre-train multi-layer features and unsupervised anomaly detection to learn the feature distribution of normal samples; the network structure of the defect detection model includes a feature extraction module, a feature processing module, and an anomaly scoring module connected in sequence.

[0050] The feature extraction module is used to extract multi-layer features from the input training image blocks to form multi-scale feature maps; the feature processing module is used to block and aggregate the multi-scale feature maps to obtain block-level aggregated features; the anomaly scoring module is used to score the block-level aggregated features, output the block-level anomaly score, and generate anomaly regions and anomaly scores.

[0051] The feature extraction module can use a convolutional neural network or other deep feature extraction network as the backbone network to output multi-layer feature maps. In one implementation, a residual network-type backbone can be selected and its intermediate layer features can be extracted to balance receptive field and detail representation. In another implementation, a preprocessing module can be set before the feature extraction module to adjust the size of the training image patch to match the input size requirements of the feature extraction module.

[0052] The feature processing module includes a block processing unit and a feature aggregation unit. The block processing unit is used to divide the multi-scale feature map into block-level features according to a preset block size. The feature aggregation unit is used to pool and aggregate the features within the block to obtain block-level aggregated features for block-level anomaly scoring. The pooling method of the feature aggregation unit can be adaptive average pooling or adaptive max pooling, etc.

[0053] The anomaly scoring module includes a discriminator and an anomaly generation unit.

[0054] The discriminator is used to receive block-level aggregated features and output block-level anomaly scores, which can be based on anomaly probabilities.

[0055] During the training phase, the discriminator's model parameters are updated only based on the block-level aggregated features corresponding to the training image patches. To improve the ability to distinguish subtle anomalies, synthetic negative features can be constructed in the neighborhood of normal features as comparison samples during training. For example, spherical neighborhood sampling can be performed on normal features and Gaussian perturbation can be superimposed to generate synthetic negative features, making them similar to but not the same as normal features. The output layer of the discriminator can use the Sigmoid function, and the loss function can use a binary classification loss function with a focusing mechanism to improve the ability to distinguish hard examples. In one implementation, validation / test data can be used to set and evaluate the threshold. The validation or test data can include normal samples and samples containing defects. The threshold can be determined according to the preset false positive rate or false negative rate requirements. Samples containing defects are only used for threshold setting and performance evaluation and do not participate in the model parameter update during the training phase.

[0056] The anomaly generation unit is used to aggregate block-level anomaly scores according to the corresponding spatial mapping relationship, and generate anomaly regions corresponding to the input training image block set through interpolation mapping or back projection processing; optionally, the anomaly detection map is smoothed to improve visualization stability.

[0057] In step S2, the specific process is as follows.

[0058] S21. Perform data augmentation on the training image blocks corresponding to the normal training sample images.

[0059] In step S21, data augmentation includes geometric perturbation, texture enhancement, color perturbation, and normalization. Geometric perturbation is limited to the range that does not change the spatial correspondence between multiple training image patches. Texture enhancement can introduce an external texture library / texture dataset as a texture source and use it in conjunction with normal training sample images to increase appearance diversity.

[0060] S22. Input the augmented training image blocks into the defect detection model for training and obtain the corresponding model parameters.

[0061] S23. Export the trained defect detection model into a general model format for deployment to online detection equipment.

[0062] In step S2, after the defect detection model has been trained, the optimal model parameter weights are saved and exported in ONNX format so that the model can be subsequently migrated to online detection equipment for deployment.

[0063] S3, Online Detection Phase.

[0064] In step S3, the trained defect detection model is used to perform online detection on the image of the workpiece to be tested. The image of the workpiece to be tested is obtained by an industrial camera from the workpiece production line. In the online detection stage, the two-dimensional alignment template and block layout template in the training dataset generation stage of step S1 are reused. After performing two-dimensional alignment, block mapping and slicing processing on the image of the workpiece to be tested, a set of image blocks to be tested is generated and then input into the trained defect detection model for inference and output of detection results. The specific process is as follows.

[0065] Reference Figure 8 As shown. The specific process in step S3 is as follows.

[0066] S31. Perform two-dimensional alignment on the captured image of the workpiece to be tested to obtain the two-dimensional transformation parameters of the image of the workpiece to be tested relative to the two-dimensional alignment template. The two-dimensional alignment template used in this step is the same as the two-dimensional alignment template in step S14. In one embodiment, when the matching confidence of the two-dimensional alignment template is lower than a preset threshold, an alignment failure flag can be output and a retake or manual review can be triggered to avoid false detection and missed detection caused by alignment error.

[0067] Please refer to Figure 6 As shown, the two-dimensional alignment process in step S31 includes: S311. Match the image of the workpiece to be tested with the two-dimensional alignment template used in the stage of generating the training dataset, and estimate the two-dimensional transformation parameters. The two-dimensional transformation parameters include at least translation parameters and rotation angles. S312. Based on the two-dimensional transformation parameters, map the workpiece foreground mask and / or workpiece contour of the template reference image to the workpiece image to be tested, and generate the aligned foreground mask and / or aligned contour of the workpiece image to be tested; optionally, superimpose the aligned foreground mask and / or aligned contour with the workpiece image to be tested and output it for visualization and quality verification.

[0068] S32. Based on the block layout template, perform block mapping and slicing processing on the image of the workpiece to be tested to generate a set of image blocks to be tested. The block layout template used in this step is the same as the block layout template in step S15.

[0069] Reference Figure 7 As shown. In this embodiment, the block mapping and slicing process in step S32 includes: S321. Call the block layout template used in the training dataset generation stage; the block layout template includes parameters such as block size, block position and orientation, and effective block filtering threshold. S322. Based on the two-dimensional transformation parameters obtained in step S31, the block layout template is mapped to the image of the workpiece to be tested. The slicing process of rotation transformation, coordinate mapping, translation positioning, and boundary checking is performed in sequence. The blocks are then filtered for validity based on the alignment foreground mask and / or alignment contour of the image of the workpiece to be tested, and the set of image blocks to be tested is extracted.

[0070] S33. Input the set of image blocks to be tested into the defect detection model that has been trained.

[0071] S34. The trained defect detection model performs inference on the input set of image blocks to be tested, outputs the anomaly score of each image block to be tested, and generates the corresponding anomaly detection map and / or anomaly region mask and / or pixel-level anomaly heatmap.

[0072] S35. The inference results of each image block to be tested are back-projected to the coordinate system of the image of the workpiece to be tested according to the corresponding block mapping relationship. The overlapping areas are aggregated to obtain the overall image anomaly detection result. Then, based on the judgment result of whether the overall image anomaly score exceeds the set threshold, the corresponding workpiece defect status is output, and the defect location result and visualization result can be output optionally.

[0073] In this embodiment, all data involved can be publicly available or non-public data obtained through legal means.

[0074] Compared with existing technologies, this embodiment of the defect detection model based on pre-trained multi-layer features and unsupervised anomaly detection mechanism only uses normal training samples to update model parameters, so as to learn the normal feature distribution of the workpiece in the feature space in the template reference image; the defect detection model outputs block-level anomaly scores through the discriminator and can generate pixel-level anomaly heatmaps, thus having higher sensitivity to local defects such as small scratches and bubbles and clearer defect boundaries; since a large number of real defect samples are not required to participate in parameter updates during the training stage, the cost of defect collection and annotation can be significantly reduced, making it suitable for industrial inspection scenarios where defect samples are scarce and varied.

[0075] Compared to existing technologies, this embodiment achieves standardization and reuse of the detection area through training dataset generation: a workpiece foreground mask and / or workpiece contour are generated based on the template reference image, and a block layout template is established on the template reference image; then, based on the two-dimensional transformation parameters obtained by two-dimensional alignment, the block layout template is reused on all workpiece images to be trained or detected through geometric mapping, thereby achieving accurate alignment and reuse of the region of interest; the defect detection model can generate a pixel-level anomaly heatmap from block-level anomaly scoring, and output the mask and / or contour of the defect area from the online detection stage to achieve fine-grained defect localization; by combining the constraints of the foreground mask and / or contour with block-level evaluation, the detection range is limited to the workpiece body area, reducing background interference and reducing computational load.

[0076] Compared to existing technologies, this embodiment, through a unified training and inference data organization method, can complete the training process, evaluation process, model export, and online inference interface docking within the same framework, facilitating containerized deployment in industrial environments. By reusing workpiece foreground masks and / or contour constraints with block layout templates, the defect detection model can be integrated with production line acquisition equipment, and templates and interfaces can be reused across multiple inspection items. In scenarios with fixed or weakly changing camera poses, block layout templates can be reused to support batch inspection on the production line, making maintenance and expansion more convenient.

[0077] The second embodiment of this application is described below.

[0078] Please refer to Figure 9 . Figure 9 This is a structural framework diagram of a defect detection system. The defect detection system in this embodiment includes a training dataset generation module, an offline training module, and an online detection module.

[0079] The training dataset generation module is used to generate the training dataset. The processing steps of the training dataset generation module are as follows: Obtain a set of normal training sample images corresponding to the type of workpiece to be detected, and select at least one reference defect-free image as the template reference image. Process the template reference image to obtain the workpiece foreground mask and / or workpiece contour. Use the workpiece foreground mask and / or workpiece contour as a two-dimensional alignment template. Perform two-dimensional alignment processing on each image in the normal training sample image set to obtain the corresponding two-dimensional transformation parameters. Generate a block layout template based on the workpiece foreground mask and / or workpiece contour. Map the block layout template to each normal training sample image according to the two-dimensional transformation parameters. Perform slicing processing to extract the training image block set, forming the training dataset.

[0080] The training dataset generation module includes a normal training sample image acquisition module, a template reference image setting module, a template reference image foreground segmentation module, a normal training sample image 2D alignment module, and a normal training sample image block mapping and slicing processing module.

[0081] The normal training sample image acquisition module is used to acquire a set of normal training sample images corresponding to a type of workpiece to be detected.

[0082] The normal training sample image set focuses on defect-free workpieces of the same specifications and material type. These images are acquired by industrial cameras under production line background conditions that closely resemble actual online inspection. Meanwhile, to enhance appearance diversity, external texture libraries / datasets can be introduced as a source of texture enhancement in the normal training sample images. These textures are used in conjunction with the normal training sample images acquired by the industrial cameras for subsequent data augmentation processing. However, external textures cannot replace the acquisition of normal training sample images.

[0083] The template reference image setting module is used to select at least one reference defect-free image that can represent the standard appearance and posture of the workpiece from the normal training sample image set as the template reference image, and can select multiple high-quality reference defect-free images for template stability verification and parameter selection.

[0084] The selection of reference defect-free images must meet the following conditions: the workpiece outline is complete, there is no occlusion, no obvious motion blur, no abnormal highlights, no shadow interference, and other process inspections confirm that there are no appearance defects.

[0085] The template reference image foreground segmentation module is used to process the template reference image to generate a workpiece foreground mask and / or workpiece contour to characterize features such as workpiece contour and foreground region.

[0086] In the template reference image foreground segmentation module, the process of generating the workpiece foreground mask and / or workpiece contour includes: (a) Mark the region of interest of the workpiece on the template reference image; (b) Input the template reference image containing the region of interest of the workpiece into the segmentation model for inference, output the workpiece mask and form the workpiece foreground mask; and / or input the template reference image containing the region of interest into the contour extraction model for inference, and generate the workpiece contour.

[0087] In other implementations, the segmentation model can be semantic segmentation, threshold segmentation, GrabCut, or other interactive segmentation models; the annotation of the region of interest and the foreground mask can be generated once on the template reference image and reused in subsequent two-dimensional alignment, block mapping, and data augmentation constraints to reduce background interference and improve processing efficiency; in this embodiment, the SAM model is used to infer the foreground mask of the workpiece from the region of interest of the workpiece, and the workpiece contour is inferred using edge detection algorithms, etc.

[0088] The normal training sample image 2D alignment module is used to perform 2D alignment on the workpiece in the normal training sample image by using the workpiece foreground mask and / or workpiece contour of the template reference image as a 2D alignment template, so as to eliminate translation and rotation deviations introduced by production line shooting.

[0089] Considering the potential for workpiece offset relative to the worktable, slight posture changes, and imaging timing differences in actual production, two-dimensional alignment processing is required for normal training sample images. The two-dimensional alignment process in the normal training sample image two-dimensional alignment module includes: (c) Perform template matching between the normal training sample image and the two-dimensional alignment template to estimate the two-dimensional transformation parameters; the two-dimensional transformation parameters include at least translation parameters and rotation angles; (d) Based on the two-dimensional transformation parameters, the workpiece foreground mask and / or workpiece contour of the template reference image are mapped to the corresponding normal training sample image to obtain the aligned foreground mask and / or aligned contour of the normal training sample image; optionally, a small number of normal training sample images can also be used for verification and correction using the generated mask and / or contour. (e) Output the normal training sample image after two-dimensional alignment and its aligned foreground mask and / or aligned contour; optionally, the aligned foreground mask and / or aligned contour are superimposed on the normal training sample image before being output for quality inspection and visualization.

[0090] The normal training sample image block mapping and slicing module is used to perform block mapping and slicing on the two-dimensional aligned normal training sample images to extract a set of training image blocks as the training dataset.

[0091] In the normal training sample image block mapping and slicing module, the block mapping and slicing process includes: (f) Calculate the oriented bounding box based on the workpiece foreground mask and / or workpiece contour of the template reference image, and generate a block mesh within the oriented bounding box to form a block layout template; optionally, the block size is searched and optimized according to a preset target, and the blocks can be effectively filtered based on the foreground proportion threshold. The block layout template includes parameters such as block size, block position and orientation, and effective block filtering threshold. (g) For normal training sample images, based on the two-dimensional transformation parameters obtained by the two-dimensional alignment module of normal training sample images, the block layout template is mapped onto the corresponding normal training sample images. After performing slicing processing such as rotation transformation, coordinate transformation, translation positioning, and boundary checking in sequence, the training image block set is extracted, and the entire training image block set corresponding to all normal training sample images is used as the training dataset.

[0092] The offline training module is used to train the defect detection model offline using the training dataset. The offline training module's process is as follows: based on the training dataset generated by the training dataset generation module, data augmentation is performed on the training image patch set corresponding to each normal training sample image. This data augmentation is then applied to the defect detection model to learn the normal feature distribution and obtain model parameters. Optionally, samples containing defects can be configured as validation / test data for threshold setting, model selection, or performance evaluation. Samples containing defects do not participate in model parameter updates during the training process. After data augmentation, the training image patches are then input into the defect detection model to be trained.

[0093] The offline training module includes a data augmentation module, a defect detection model training module, and a model export module.

[0094] The data augmentation module is used to augment the training image blocks corresponding to normal training sample images.

[0095] In the data augmentation module, data augmentation processes include geometric perturbation, texture enhancement, color perturbation, and normalization. Among them, geometric perturbation is limited to the range that does not change the spatial correspondence between multiple training image patches. Texture enhancement can introduce external texture libraries / texture datasets as texture sources and use them in conjunction with normal training sample images to increase appearance diversity.

[0096] The defect detection model training module is used to input augmented training image patches into the defect detection model for training and to obtain the corresponding model parameters.

[0097] The defect detection model uses a discriminator-based anomaly detection algorithm for pre-training multi-layer features and unsupervised anomaly detection to learn the feature distribution of normal samples. The network structure of the defect detection model includes a feature extraction module, a feature processing module, and an anomaly scoring module connected in sequence.

[0098] The feature extraction module is used to extract multi-layer features from the input training image blocks to form multi-scale feature maps; the feature processing module is used to block and aggregate the multi-scale feature maps to obtain block-level aggregated features; the anomaly scoring module is used to score the block-level aggregated features, output the block-level anomaly score, and generate anomaly regions and anomaly scores.

[0099] In this embodiment, the feature extraction module may use a convolutional neural network or other deep feature extraction network as the backbone network to output multi-layer feature maps; in one implementation, a residual network-type backbone may be selected and its intermediate layer features may be extracted to balance receptive field and detail representation; in another implementation, a preprocessing module may be set before the feature extraction module, which is used to adjust the size of the training image patch to match the input size requirements of the feature extraction module.

[0100] The feature processing module includes a block processing unit and a feature aggregation unit. The block processing unit is used to divide the multi-scale feature map into block-level features according to a preset block size. The feature aggregation unit is used to pool and aggregate the features within the block to obtain block-level aggregated features for block-level anomaly scoring. The pooling method of the feature aggregation unit can be adaptive average pooling or adaptive max pooling, etc.

[0101] The anomaly scoring module includes a discriminator and an anomaly generation unit.

[0102] The discriminator is used to receive block-level aggregated features and output block-level anomaly scores, which can be based on anomaly probabilities.

[0103] During the training phase, the discriminator's model parameters are updated only based on the block-level aggregated features corresponding to the training image patches. To improve the ability to distinguish subtle anomalies, synthetic negative features can be constructed in the neighborhood of normal features as comparison samples during training. For example, to improve robustness, synthetic negative features can be generated by sampling the spherical neighborhood of normal features and superimposing Gaussian perturbations, making the synthetic negative features similar to but not identical to the normal features, thereby increasing the discrimination difficulty and improving the model's generalization ability. The discriminator's output layer can use the Sigmoid activation function, and the loss function can use a binary classification loss function with a focusing mechanism to improve the ability to distinguish hard examples. The discriminator's hidden layer uses non-linear activation functions such as LeakyReLU, and the output layer uses Sigmoid to output the anomaly probability. In one implementation, validation / test data can be used to set and evaluate the threshold, where the validation / test data can include normal samples and samples containing defects. The threshold can be determined according to the preset false positive rate or false negative rate requirements. Samples containing defects are only used for threshold setting and performance evaluation and do not participate in the model parameter updates during the training phase.

[0104] The anomaly generation unit is used to aggregate block-level anomaly scores according to the corresponding spatial mapping relationship, and generate anomaly regions corresponding to the input training image block set through interpolation mapping or back projection processing; optionally, the anomaly detection map is smoothed to improve visualization stability.

[0105] The model export module is used to export the trained defect detection model into a universal model format for subsequent deployment to online detection devices.

[0106] The model export module saves the optimal model parameter weights after the defect detection model has been trained and exports it in ONNX format so that the model can be subsequently migrated to online detection equipment for deployment.

[0107] The online detection module is used to detect whether there are defects in the workpiece under test. The processing procedure of the online detection module is as follows: a two-dimensional alignment template is used to perform two-dimensional alignment processing on the image of the workpiece under test to obtain the corresponding two-dimensional transformation parameters. Then, the block layout template is mapped to the image of the workpiece under test according to the two-dimensional transformation parameters and sliced ​​to extract the set of image blocks to be tested. The set of image blocks to be tested is input into the trained defect detection model for inference, and the model outputs anomaly scores and anomaly regions to determine whether there are defects in the image of the workpiece under test.

[0108] The image of the workpiece to be tested is captured by an industrial camera from the workpiece production line. The online detection reuses the two-dimensional alignment template and block layout template in the training data module. After performing two-dimensional alignment, block mapping and slicing processing on the image of the workpiece to be tested, a set of image blocks to be tested is generated and then input into the trained defect detection model for inference and output of detection results. The specific process is as follows.

[0109] The online inspection module includes a two-dimensional alignment module for the workpiece image, a block mapping and slicing module for the workpiece image, a defect detection model input module, a defect detection model inference module, and a detection result aggregation and judgment module.

[0110] The two-dimensional alignment module for the workpiece image under test is used to perform two-dimensional alignment on the captured workpiece image under test to obtain the two-dimensional transformation parameters of the workpiece image under test relative to the two-dimensional alignment template. In one embodiment, when the matching confidence of the two-dimensional alignment template is lower than a preset threshold, an alignment failure flag can be output and a retake or manual review can be triggered to avoid false detection and missed detection caused by alignment error.

[0111] In the two-dimensional alignment module of the workpiece image under test, the two-dimensional alignment process includes: (h) Match the image of the workpiece to be tested with the two-dimensional alignment template used in the stage of generating the training dataset, and estimate the two-dimensional transformation parameters, which include at least translation parameters and rotation angles. (i) Based on the two-dimensional transformation parameters, the workpiece foreground mask and / or workpiece contour of the template reference image are mapped to the workpiece image to be tested, generating the aligned foreground mask and / or aligned contour of the workpiece image to be tested; optionally, the aligned foreground mask and / or aligned contour are superimposed on the workpiece image to be tested and output for visualization and quality verification.

[0112] The image block mapping and slicing module for the workpiece under test is used to perform block mapping and slicing processing on the image of the workpiece under test based on the block layout template, and generate a set of image blocks to be tested.

[0113] In the image segmentation and slicing module for the workpiece under test, the segmentation and slicing process includes: (j) Call the block layout template used in the training dataset generation stage; the block layout template includes parameters such as block size, block position and orientation, and effective block filtering threshold; (k) Based on the two-dimensional transformation parameters obtained by the two-dimensional alignment module of the workpiece image under test, the block layout template is mapped to the workpiece image under test, and the rotation transformation, coordinate mapping, translation positioning and boundary checking are performed in sequence. Based on the alignment foreground mask and / or alignment contour of the workpiece image under test, the blocks are filtered for validity and the set of blocks of the image under test is extracted.

[0114] The defect detection model input module is used to input the set of image blocks to be tested into the defect detection model for training.

[0115] The defect detection model inference module is used to infer the input set of image blocks to be tested, output the anomaly score of each image block to be tested, and generate the corresponding anomaly detection map and / or anomaly region and / or anomaly region mask and / or pixel-level anomaly heatmap.

[0116] The detection result aggregation and judgment module is used to backproject the inference results of each image block to be tested onto the coordinate system of the image of the workpiece to be tested according to the corresponding block mapping relationship, aggregate the overlapping areas to obtain the overall image anomaly detection result, and then output the corresponding workpiece defect status based on the judgment result of whether the overall image anomaly score exceeds the set threshold, and optionally output the defect location result and visualization result.

[0117] Compared with existing technologies, this embodiment of the defect detection model based on pre-trained multi-layer features and unsupervised anomaly detection mechanism only uses normal training samples to update model parameters, so as to learn the normal feature distribution of the workpiece in the feature space in the template reference image; the defect detection model outputs block-level anomaly scores through the discriminator and can generate pixel-level anomaly heatmaps, thus having higher sensitivity to local defects such as small scratches and bubbles and clearer defect boundaries; since a large number of real defect samples are not required to participate in parameter updates during the training stage, the cost of defect collection and annotation can be significantly reduced, making it suitable for industrial inspection scenarios where defect samples are scarce and varied.

[0118] Compared to existing technologies, this embodiment achieves standardization and reuse of the detection area through training dataset generation: a workpiece foreground mask and / or workpiece contour are generated based on the template reference image, and a block layout template is established on the template reference image; then, based on the two-dimensional transformation parameters obtained by two-dimensional alignment, the block layout template is reused on all workpiece images to be trained or detected through geometric mapping, thereby achieving accurate alignment and reuse of the region of interest; the defect detection model can generate a pixel-level anomaly heatmap from block-level anomaly scoring, and output the mask and / or contour of the defect area from the online detection stage to achieve fine-grained defect localization; by combining the constraints of the foreground mask and / or contour with block-level evaluation, the detection range is limited to the workpiece body area, reducing background interference and reducing computational load.

[0119] Compared to existing technologies, this embodiment, through a unified training and inference data organization method, can complete the training process, evaluation process, model export, and online inference interface docking within the same framework, facilitating containerized deployment in industrial environments. By reusing workpiece foreground masks and / or contour constraints with block layout templates, the defect detection model can be integrated with production line acquisition equipment, and templates and interfaces can be reused across multiple inspection items. In scenarios with fixed or weakly changing camera poses, block layout templates can be reused to support batch inspection on the production line, making maintenance and expansion more convenient.

[0120] The third embodiment of this application is described below.

[0121] A computer device includes a memory and a processor; the memory stores a computer program that can be executed by the processor, and when the processor executes the computer program, it implements the defect detection method of the aforementioned method embodiments.

[0122] In one implementation, the computer device may be implemented using a standalone or distributed architecture; when using a distributed architecture, at least one stage of training dataset generation, offline training, and online detection may be performed by different processing nodes.

[0123] Computer equipment can be deployed in computing environments such as servers, edge computing devices, or cloud platforms, and can be implemented using centralized or distributed processing methods.

[0124] Computer programs can be structured executable code modules or sets of instructions.

[0125] In the case of the computer device of this embodiment, when its processor executes a computer program, it can issue computer-readable instructions to perform corresponding steps, which can drive the corresponding peripheral electronic devices to realize the functions of sending and receiving information data and / or processing and / or visualization.

[0126] The processor can be one or more of a CPU, GPU, system-on-a-chip, or AI acceleration chip.

[0127] The computer device in this embodiment may also be a combination of other corresponding functional modules and / or units, or an integrated processing device.

[0128] The following is the fourth embodiment of this application.

[0129] A computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is used to implement the defect detection method described in the aforementioned defect detection method embodiments.

[0130] In one embodiment, the computer-readable storage medium may be a hard disk drive, a solid-state drive, an optical disk, flash memory, or other non-transitory storage medium.

[0131] It should be understood that the aforementioned methods and steps can be appropriately adjusted according to actual application scenarios, including parallel or sequential execution, step merging or splitting, and adjustment of the execution order. As long as the technical effect of the technical solution described in this application can be achieved, it should fall within the protection scope of this application.

[0132] Although the present application has been described above with reference to specific embodiments, those skilled in the art can make various changes and modifications without departing from the spirit and scope of the present application. The scope of protection of the present application shall be determined by the scope defined in the claims.

Claims

1. A defect detection method, characterized in that, include: Obtain a set of normal training sample images corresponding to the type of workpiece to be inspected, and select at least one reference defect-free image as the template baseline image; The template reference image is processed to obtain the workpiece foreground mask and / or workpiece outline; Using the workpiece foreground mask and / or the workpiece contour as a two-dimensional alignment template, perform two-dimensional alignment processing on each image in the normal training sample image set to obtain the corresponding two-dimensional transformation parameters. Generate a block layout template based on the workpiece foreground mask and / or workpiece outline; Based on the two-dimensional transformation parameters, the block layout template is mapped to each normal training sample image, and the slice processing is used to extract the training image block set to form a training dataset; A defect detection model is trained based on the training dataset, enabling the defect detection model to learn the normal feature distribution of the type of workpiece to be detected; The two-dimensional alignment template is used to perform two-dimensional alignment processing on the image of the workpiece to be tested to obtain the corresponding two-dimensional transformation parameters. The block layout template is then mapped to the image of the workpiece to be tested according to the two-dimensional transformation parameters corresponding to the image of the workpiece to be tested and sliced ​​to extract the image block set. The set of image blocks to be tested is input into the trained defect detection model for inference, and the abnormal score and abnormal region are output to determine whether there are defects in the image of the workpiece to be tested.

2. The defect detection method according to claim 1, characterized in that, Processing the template reference image includes: Determine the region of interest for the workpiece in the template reference image; The template reference image containing the region of interest is input into the segmentation model for inference to generate a workpiece foreground mask; And / or input a template reference image containing the region of interest into the contour extraction model for inference to generate the workpiece contour.

3. The defect detection method according to claim 1, characterized in that, Performing two-dimensional alignment processing on normal training sample images includes: The normal training sample image is matched with the two-dimensional alignment template to estimate the two-dimensional transformation parameters, which include at least translation parameters and rotation angles. Based on the two-dimensional transformation parameters, the workpiece foreground mask and / or workpiece contour of the template reference image are mapped to the corresponding normal training sample image to generate the aligned foreground mask and / or aligned contour of the normal training sample image. The two-dimensional alignment processing of the image of the workpiece under test includes: The image of the workpiece to be tested is matched with a two-dimensional alignment template to obtain the corresponding two-dimensional transformation parameters. The two-dimensional transformation parameters include at least translation parameters and rotation angles. Based on the two-dimensional transformation parameters corresponding to the image of the workpiece to be tested, the workpiece foreground mask and / or workpiece contour of the template reference image are mapped to the image of the workpiece to be tested, thereby generating the aligned foreground mask and / or aligned contour of the image of the workpiece to be tested.

4. The defect detection method according to claim 3, characterized in that, The block mapping and slicing process for the normal training sample images after 2D alignment includes: Calculate the oriented bounding box based on the workpiece foreground mask and / or workpiece contour of the template reference image, and generate a block mesh within the oriented bounding box to form a block layout template; Based on the two-dimensional transformation parameters, the block layout template is mapped to each normal training sample image, and rotation, translation and boundary checks are performed to extract the training image block set to form a training dataset.

5. The defect detection method according to claim 3, characterized in that, The block mapping and slicing processing of the image of the workpiece to be tested includes: Call the block layout template; Based on the two-dimensional transformation parameters of the image of the workpiece to be tested, the block layout template is mapped to the image of the workpiece to be tested, and rotation, translation, and boundary checks are performed to extract the set of image blocks to be tested.

6. The defect detection method according to claim 1, characterized in that, The defect detection model includes a feature extraction module, a feature processing module, and an anomaly scoring module; The feature extraction module is used to extract multi-layer features from the input training image patches to form a multi-scale feature map; The feature processing module is used to block the multi-scale feature map and aggregate the features within the block to obtain block-level aggregated features; The anomaly scoring module includes a discriminator and an anomaly generation unit; The discriminator is used to receive the block-level aggregated features and output a block-level anomaly score; The anomaly generation unit is used to generate an anomaly detection map based on the block-level anomaly score.

7. The defect detection method according to claim 1, characterized in that, When training the defect detection model, only normal training sample images are used to update the model parameters, while defective sample images are used for threshold setting or performance evaluation and are not used for model parameter updates.

8. A defect detection system, characterized in that, It includes a training dataset generation module, an offline training module, and an online detection module; The training dataset generation module is used to acquire a set of normal training sample images corresponding to the type of workpiece to be detected, select at least one reference defect-free image as a template reference image, process the template reference image to obtain a workpiece foreground mask and / or workpiece contour, use the workpiece foreground mask and / or workpiece contour as a two-dimensional alignment template, perform two-dimensional alignment processing on each image in the set of normal training sample images to obtain corresponding two-dimensional transformation parameters, generate a block layout template based on the workpiece foreground mask and / or workpiece contour, map the block layout template to each normal training sample image according to the two-dimensional transformation parameters, and extract a set of training image blocks through slicing processing to form a training dataset. The offline training module is used to train a defect detection model based on the training dataset, so that the defect detection model learns the normal feature distribution of the type of workpiece to be detected; The online detection module is used to perform two-dimensional alignment processing on the image of the workpiece under test using a two-dimensional alignment template to obtain the corresponding two-dimensional transformation parameters. The block layout template is then mapped to the image of the workpiece under test according to the two-dimensional transformation parameters and sliced ​​to extract a set of image blocks. The set of image blocks is then input into the trained defect detection model for inference, and anomaly scores and abnormal regions are output to determine whether there are defects in the image of the workpiece under test.

9. A computer device, characterized in that, It includes a memory and a processor; the memory stores a computer program that can be executed by the processor, and when the processor executes the computer program, it is used to implement the defect detection method according to any one of claims 1-7.

10. A computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the defect detection method according to any one of claims 1-7.