Coffee powder foreign matter detection method and device based on background reconstruction, medium and product

By employing a dual-branch structure that combines background reconstruction and target detection in parallel, and utilizing an autoencoder model to generate a clean background image, combined with a spatial residual attention weight map, the false alarm and false negative issues in foreign object detection in coffee powder are resolved, achieving high-precision foreign object identification.

CN121767982AInactive Publication Date: 2026-03-31BEIJING MILAN GOLD COFFEE CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-12-19
Publication Date
2026-03-31
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

In existing technologies, methods based on shallow visual features are easily affected by complex stacked texture backgrounds when detecting foreign objects such as metals and plastics in coffee powder, leading to false alarms or missed alarms, making it difficult to achieve high-precision non-destructive testing.

Method used

A background reconstruction-based approach is adopted, which generates a clean background prediction image through an autoencoder model and combines spatial residual attention weight map and multi-scale feature fusion to achieve adaptive differentiation between foreign objects and background.

Benefits of technology

It improves the accuracy and stability of foreign object detection in complex environments, reduces the false detection rate, and is suitable for online foreign object detection of coffee powder and other granular foods.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121767982A_ABST
    Figure CN121767982A_ABST
Patent Text Reader

Abstract

The invention provides a ground coffee foreign matter detection method and device based on background reconstruction, a medium and a product, and the method comprises the following steps: obtaining an original transmission image of to-be-detected ground coffee, and carrying out gray normalization to obtain a standard input image; respectively inputting a standard input image into a background reconstruction branch and a target detection branch; in the background reconstruction branch, generating a pure background prediction image by using a self-encoding model, and calculating a gray difference between the pure background prediction image and the standard input image to obtain a foreign matter residual image; convolutional features are extracted from the foreign matter residual image, and a spatial residual attention weight map is generated; in the target detection branch, performing multi-layer convolution on the standard input image, and performing weighted fusion in combination with the attention weight map to obtain a foreign matter sensitive feature map; and outputting a foreign matter detection result through prediction head network classification and regression decoding. By implementing the technical scheme provided by the invention, the detection sensitivity of low-contrast and low-density foreign matters is improved, and meanwhile, the false detection rate caused by complex powder textures is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer vision technology, and in particular to a method, device, medium, and product for detecting foreign objects in coffee powder based on background reconstruction. Background Technology

[0002] With the intelligent upgrading of the food industry and the rapid expansion of the consumer market, the production capacity of powdered foods such as coffee powder continues to expand, and the production process is becoming increasingly refined. How to perform high-precision, automated, and non-destructive testing and removal of various foreign objects such as metals, plastics, and glass mixed in during the production process has become a core requirement for ensuring food safety and maintaining brand reputation.

[0003] In existing technologies, foreign object detection based on X-ray images typically employs traditional machine vision methods, such as grayscale clustering or edge gradient calculation to locate food edges and thus assist in foreign object detection. However, these methods heavily rely on the measured object having a regular geometric shape (such as block-shaped tofu or biscuits) and clear edge gradients. Since the random stacking texture of coffee powder particles can easily be confused with the imaging characteristics of foreign objects such as plastic and rubber, methods based on shallow visual features in practical applications face the risk of false positives or false negatives for low-density foreign object signals and high-frequency background noise against a complex stacked texture background. Summary of the Invention

[0004] In view of this, this application provides a method, device, medium, and product for detecting foreign objects in coffee powder based on background reconstruction, in order to solve the above problems.

[0005] Firstly, a method for detecting foreign objects in coffee powder based on background reconstruction is provided, the method comprising:

[0006] The original transmission image of the coffee powder to be detected is obtained, and the original transmission image is subjected to grayscale normalization to obtain a standard input image. The standard input image is then input into the preset background reconstruction branch and the preset target detection branch, respectively.

[0007] In the background reconstruction branch, a pre-defined autoencoder model is used to perform feature compression and decoding reconstruction on the standard input image to generate a clean background prediction image.

[0008] Calculate the pixel grayscale difference between the standard input image and the clean background prediction image at corresponding locations to obtain the foreign object residual image composed of the pixel grayscale difference;

[0009] Convolutional feature extraction is performed on the foreign object residual image to generate a spatial residual attention weight map;

[0010] In the object detection branch, multi-layer convolution operations are performed on the standard input image to obtain the original semantic feature map;

[0011] The original semantic feature map is weighted and fused using the spatial residual attention weight map to obtain the foreign object sensitivity feature map;

[0012] The foreign object detection results are obtained by performing target classification and bounding box regression decoding on the foreign object sensitive feature map through a pre-set prediction head network.

[0013] The above technical solution achieves synergy between background modeling and target recognition by inputting standard input images in parallel into the background reconstruction branch and the target detection branch. The background reconstruction branch uses an autoencoder model to generate a clean background prediction image and calculates a residual signal reflecting only anomalous regions by comparing the pixel grayscale difference with the original image, thus removing texture noise caused by coffee powder stacking. The target detection branch extracts semantic features and combines them with a spatial attention weight map generated from the residual to weight and enhance the features of foreign object regions. This mechanism enables the system to adaptively distinguish between foreign objects and normal textures in complex stacked backgrounds, effectively suppressing high-frequency noise interference and improving the detection accuracy and stability of low-density foreign objects.

[0014] Optionally, the original transmission image of the coffee powder to be detected is acquired, and the original transmission image is subjected to grayscale normalization to obtain a standard input image, specifically including:

[0015] The original transmission image is calculated using morphological closing operations, and the background envelope is extracted as a global thickness trend map.

[0016] A flat-field correction operation is performed on the original transmission image based on the global thickness trend map to obtain a thickness-uniformed image.

[0017] Using the thickness homogenized image as a guide image, the original transmission image is subjected to guide filtering to obtain the intermediate image;

[0018] Calculate the global grayscale mean and global standard deviation of the intermediate image, and based on the global grayscale mean and global standard deviation, convert the grayscale value of each pixel in the intermediate image into a standard score;

[0019] Replace the values ​​in the standard scores that are greater than the preset upper threshold with the upper threshold, and replace the values ​​that are less than the preset lower threshold with the lower threshold to obtain the truncated standard scores.

[0020] Normalize the truncated standard scores to obtain a standard input image with pixel values ​​distributed within a preset standard input range.

[0021] The above technical solution compensates for differences in illumination and transmission caused by uneven coffee powder accumulation through morphological closing operations and flat-field correction. Combined with guided filtering and grayscale normalization, the grayscale distribution of the input image is unified to a standard range, correcting for background brightness variations. This preprocessing step provides stable input for subsequent background reconstruction, reduces the interference of random textures and thickness differences on feature extraction, and thus improves the reliability of identifying low-density foreign objects.

[0022] Optionally, in the background reconstruction branch, a pre-defined autoencoder model is used to perform feature compression and decoding reconstruction on the standard input image to generate a clean background prediction image, specifically including:

[0023] The standard input image is fed into the encoder subnet of the autoencoder model. Multiple sets of feature maps of the standard input image are extracted through multiple dilated convolutional layers with different dilation rates in the encoder subnet.

[0024] Multiple feature maps are stacked and stitched along the channel dimension of the multiple feature maps to generate an aggregated feature map;

[0025] The aggregated feature map is convolved by the first point convolutional layer in the encoder subnet, which compresses the number of channels in the aggregated feature map to a preset dimension, thus obtaining a low-dimensional latent feature map.

[0026] The global feature distribution based on the low-dimensional latent feature map generates weight coefficients for the corresponding channel dimensions, and the low-dimensional latent feature map is weighted and summed based on the weight coefficients to obtain the weighted latent feature map.

[0027] The weighted latent feature map is input into the decoder subnet of the autoencoder model. The subpixel convolutional layer in the decoder subnet upsamples and restores the features of the weighted latent feature map to generate a reconstructed feature map.

[0028] By using a preset activation function, the pixel values ​​of the reconstructed feature map are mapped to a target grayscale range that is consistent with the grayscale range of the standard input image, thus generating a clean background prediction image.

[0029] The above technical solution extracts multi-scale texture features through the dilated convolutional structure of an autoencoder model and strengthens the main background components using a channel weighting mechanism, generating a predicted image that closely approximates the real coffee powder background in terms of spatial structure and grayscale distribution. Sub-pixel convolutional layers then achieve high-fidelity upsampling, improving the detail reproduction of the reconstructed image. This mechanism ensures clear contrast in foreign object regions within the residual image, mitigating the problem of confusion between foreign objects and complex textured backgrounds.

[0030] Optionally, convolutional feature extraction is performed on the foreign object residual image to generate a spatial residual attention weight map, specifically including:

[0031] By performing multi-layer convolution operations on the foreign object residual image using multiple preset convolution kernels, the spatial features of the foreign object residual image are extracted to obtain a multi-channel difference feature map.

[0032] The kernel weights in the pre-set second-point convolutional layer are used as weighting factors to perform weighted summation on multiple feature values ​​distributed along the channel at each spatial pixel position in the multi-channel difference feature map, so as to obtain the fused feature value of each spatial pixel position, and a single-channel feature map is constructed based on the fused feature values ​​of all spatial pixel positions.

[0033] By mapping each pixel value in the single-channel feature map using an activation function, the pixel values ​​are normalized to a preset value range, generating a spatial residual attention weight map.

[0034] The above technical solution extracts spatial difference features from foreign object residual images through multi-layer convolution and uses point convolution to fuse channel responses to generate a spatial residual attention weight map. This enables the model to highlight foreign object regions with significant local gray-level differences in the spatial dimension. This weight map enhances the saliency of the foreign object signal and suppresses the texture noise response caused by random coffee powder particles, thereby improving the detectability of salient foreign object regions in complex backgrounds.

[0035] Optionally, in the object detection branch, multiple convolution operations are performed on the standard input image to obtain the original semantic feature map, specifically including:

[0036] A progressive downsampling convolution operation is performed on the standard input image to extract multi-scale intermediate feature maps with different resolution levels. The multi-scale intermediate feature maps include a first resolution feature map and a second resolution feature map. The resolution level of the first resolution feature map is lower than that of the second resolution feature map, and the resolution of the first resolution feature map is higher than that of the second resolution feature map.

[0037] The second resolution feature map is upsampled, and the upsampled second resolution feature map is fused with the first resolution feature map to generate a fused feature map.

[0038] Convolution operations are performed on the fused feature map to obtain the original semantic feature map containing multi-scale contextual information.

[0039] The above technical solution extracts and fuses multi-scale feature maps at different resolution levels through a multi-layer downsampling and upsampling fusion structure, enabling the model to maintain fine-grained texture while possessing stronger semantic expressive power. This structure can take into account the feature responses of both small and large foreign objects, thereby maintaining the stability and scale adaptability of detection in irregular texture backgrounds.

[0040] Optionally, the original semantic feature map can be weighted and fused using a spatial residual attention weight map to obtain a foreign object sensitivity feature map, specifically including:

[0041] Obtain the total number of channels contained in the original semantic feature map;

[0042] The spatial residual attention weight map is copied along the channel dimension until the number of copied channels equals the total number of channels, generating an expanded weight map with the same size as the original semantic feature map.

[0043] Construct a global traversal index set containing channel dimension coordinates and spatial plane coordinates, which covers every pixel in the extended weight map;

[0044] The first pixel value in the extended weight map and the second pixel value in the original semantic feature map are extracted based on the target index, where the target index is any index in the globally traversed index set.

[0045] Multiply the first pixel value by the second pixel value to obtain the weighted pixel value corresponding to the target index, and generate a weighted feature map containing all weighted pixel values ​​based on the global traversal of the index set.

[0046] The weighted feature map is added to the pixel values ​​at the target index of the original semantic feature map to obtain the fused pixel values ​​corresponding to the target index. Then, a foreign object sensitive feature map containing all fused pixel values ​​is generated based on a global traversal of the index set.

[0047] The above technical solution expands the spatial residual attention weight map and weights it channel-by-channel to the original semantic feature map, enabling the model to automatically enhance the feature response of the salient area of ​​the foreign object while weakening the features of the background area. This fusion mechanism allows the model to focus more on the feature expression of the area where the foreign object is located, maintaining a high signal-to-noise ratio even under strong interference from the coffee powder stacking texture, thereby improving detection accuracy.

[0048] Optionally, a pre-defined prediction head network is used to perform target classification and bounding box regression decoding on the foreign object sensitive feature map to obtain the foreign object detection result, specifically including:

[0049] The foreign object sensitive feature map is convolved by the classification subnetwork in the prediction head network, and the output is a category prediction map containing the confidence score of the presence of foreign objects at each grid point;

[0050] The foreign object sensitive feature map is convolved by the regression subnetwork in the prediction head network, and the bounding box prediction map containing the coordinate offset and size offset of the foreign object center at each grid point is output.

[0051] The center coordinate offset is superimposed on the preset grid reference coordinates by the coordinate decoding unit in the prediction head network to obtain the absolute center coordinates, and the size offset is applied to the preset anchor frame reference size to obtain the absolute size;

[0052] Construct a predicted bounding box based on the original image coordinate system using the absolute center coordinates and absolute dimensions;

[0053] The predicted bounding boxes are sorted from high to low confidence scores to generate a sequence of candidate bounding boxes, and an empty list of retained results is created.

[0054] Repeat the following filtering steps until the candidate bounding box sequence is empty, and output the list of retained results as the foreign object detection results:

[0055] Within the current screening period, the bounding box with the highest confidence score is selected from the candidate bounding box sequence as the baseline target box, and the baseline target box is transferred from the candidate bounding box sequence to the retained results list.

[0056] Calculate the intersection-union ratio (IUR) between each bounding box in the candidate bounding box sequence and the reference bounding box, excluding the reference bounding box.

[0057] Remove bounding boxes from the candidate bounding box sequence whose intersection-union ratio (IU) is greater than a preset overlap threshold, and obtain an updated candidate bounding box sequence to enter the next filtering cycle.

[0058] The above technical solution outputs foreign object confidence and bounding box location through the classification and regression subnetworks of the prediction head network, respectively. Combined with an intersection-over-union (IoU) filtering mechanism, overlapping and false detection boxes are eliminated, resulting in accurate and unique foreign object localization. This structure effectively avoids interference from false targets in complex texture backgrounds, ensuring the accuracy and consistency of the output bounding boxes, ultimately achieving high-precision detection of foreign objects in coffee powder.

[0059] In a second aspect, an electronic device is provided, including a processor, a memory, a user interface, and a network interface, wherein the memory is used to store instructions, the user interface and the network interface are both used to communicate with other devices, and the processor is used to execute the instructions stored in the memory to cause the electronic device to perform the method as described in any of the above.

[0060] Thirdly, a computer-readable storage medium is provided that stores instructions which, when executed, perform the method as described in any of the preceding descriptions.

[0061] Fourthly, a computer program product containing instructions is provided, which, when run on a server, causes the server to perform the method described in the first aspect and any possible implementation thereof.

[0062] Understandably, the electronic device provided in the second aspect, the computer-readable storage medium provided in the third aspect, and the computer program product provided in the fourth aspect are all used to execute the method provided in this application. Therefore, the beneficial effects they can achieve can be referred to the beneficial effects in the corresponding methods, and will not be repeated here.

[0063] In summary, implementing one or more technical solutions provided in this application has at least the following technical effects or advantages:

[0064] By introducing a dual-branch structure based on parallel background reconstruction and object detection, the model can simultaneously learn the statistical regularities of the coffee powder background and the salient features of foreign objects during the detection process. This achieves cross-layer fusion from low-level grayscale differences to high-level semantic expressions, thus maintaining stable discrimination capabilities even against complex textured backgrounds. Utilizing the clean background image generated by autoencoder reconstruction, the system can adaptively construct a reference baseline, achieving accurate separation of anomalous regions without relying on fixed thresholds or manual feature settings. Combining spatial residual attention mechanisms and multi-scale feature fusion, the model maintains robust performance under different particle sizes, lighting conditions, and stacking densities. Overall, this application not only improves the detection sensitivity of low-contrast, low-density foreign objects but also reduces the false detection rate caused by complex powder textures. It can be widely applied to online foreign object detection scenarios for coffee powder, cocoa powder, and other granular foods, achieving higher automated detection accuracy and industrial adaptability. Attached Figure Description

[0065] Figure 1 This is an exemplary system architecture diagram of a background reconstruction-based foreign object detection method for coffee powder disclosed in this application;

[0066] Figure 2 This is a flowchart illustrating a background reconstruction-based method for detecting foreign objects in coffee powder disclosed in this application.

[0067] Figure 3 This is a schematic diagram of the structure of an electronic device disclosed in this application.

[0068] Explanation of reference numerals in the attached figures: 100, System architecture; 101, First terminal device; 102, Second terminal device; 103, Third terminal device; 104, Network; 105, Server; 301, Processor; 302, Communication bus; 303, User interface; 304, Network interface; 305, Memory. Detailed Implementation

[0069] To enable those skilled in the art to better understand the technical solutions in this specification, the technical solutions in the embodiments of this specification will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments.

[0070] In the description of the embodiments of this application, the words "for example" or "for instance" are used to indicate examples, illustrations, or explanations. Any embodiment or design that is described as "for example" or "for instance" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or design options. Rather, the use of the words "for example" or "for instance" is intended to present the relevant concepts in a specific manner.

[0071] In the description of the embodiments of this application, the term "multiple" means two or more. For example, multiple systems means two or more systems, and multiple screen terminals means two or more screen terminals. Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the indicated technical features. Thus, a feature defined with "first" or "second" may explicitly or implicitly include one or more of that feature. The terms "comprising," "including," "having," and variations thereof all mean "including but not limited to," unless otherwise specifically emphasized.

[0072] Figure 1 An exemplary system architecture diagram is shown, illustrating an embodiment of a background reconstruction-based foreign object detection method for coffee powder that can be applied according to this application.

[0073] like Figure 1 As shown, the system architecture 100 may include a first terminal device 101, a second terminal device 102, a third terminal device 103, a network 104, and a server 105. The network 104 is used as a medium to provide communication links between the terminal devices 101, 102, 103, and the server 105. The network 104 may include various connection types, such as wired or wireless communication links or fiber optic cables, etc.

[0074] Users can use terminal devices 101, 102, and 103 to interact with server 105 via network 104 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 101, 102, and 103, such as model training applications, video recognition applications, web browser applications, social platform software, etc.

[0075] Terminal devices 101, 102, and 103 can be either hardware or software. When terminal devices 101, 102, and 103 are hardware, they can be various electronic devices with displays, including but not limited to smartphones, tablets, e-book readers, MP3 (Moving Picture Experts Group Audio Layer III) players, MP4 (Moving Picture Experts Group Audio Layer IV) players, laptops, and desktop computers, etc. When terminal devices 101, 102, and 103 are software, they can be installed in the aforementioned electronic devices. They can be implemented as multiple software programs or software modules (e.g., multiple software programs or software modules used to provide distributed services) or as a single software program or software module. No specific limitations are imposed here.

[0076] When terminals 101, 102, and 103 are hardware devices, video capture devices can also be installed on them. These video capture devices can be various devices capable of capturing video, such as cameras, sensors, etc. Users can use the video capture devices on terminals 101, 102, and 103 to capture video.

[0077] Server 105 can be a server that provides various services, such as a backend server for processing data displayed on terminal devices 101, 102, and 103. The backend server can analyze and process the received data and can feed back the processing results (such as recognition results) to the terminal devices.

[0078] It should be noted that a server can be either hardware or software. When the server is hardware, it can be implemented as a distributed server cluster consisting of multiple servers, or as a single server. When the server is software, it can be implemented as multiple software programs or software modules (e.g., multiple software programs or software modules used to provide distributed services), or as a single software program or software module. No specific limitations are made here.

[0079] It should be understood that Figure 1 The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included. In particular, if the target data does not need to be obtained remotely, the above system architecture may exclude the network and include only terminal devices or servers.

[0080] Figure 2This is a flowchart illustrating a background reconstruction-based method for detecting foreign objects in coffee powder, as described in this application. This method can be implemented using a computer program or a microcontroller. The computer program can be integrated into an application or run as a standalone utility application. The specific steps of this background reconstruction-based method for detecting foreign objects in coffee powder are described in detail below.

[0081] S201: Obtain the original transmission image of the coffee powder to be detected, perform grayscale normalization on the original transmission image to obtain the standard input image, and input the standard input image into the preset background reconstruction branch and the preset target detection branch respectively.

[0082] For example, this step is mainly responsible for cleaning and standardizing the raw X-ray images collected on the production line to eliminate environmental interference and adapt to the model input requirements. Specifically, the system receives the transmission image of coffee powder taken by the X-ray machine, and performs targeted correction and filtering on the background brightness fluctuations (low-frequency interference) caused by uneven powder accumulation and the random noise (high-frequency interference) of the imaging equipment itself, thereby obtaining an image with a uniform background and clear details; the pixel gray values ​​of the image are uniformly mapped to a fixed standard value range through statistical methods to eliminate the differences in data distribution, and the processed standard image is simultaneously distributed to the subsequent background reconstruction and target detection network branches for parallel use.

[0083] In one possible implementation, the original transmission image of the coffee powder to be detected is acquired, and grayscale normalization is performed on the original transmission image to obtain a standard input image. Specifically, this includes: calculating the background envelope as a global thickness trend map by performing morphological closing operations on the original transmission image; performing flat-field correction operations on the original transmission image based on the global thickness trend map to obtain a thickness-homogenized image; using the thickness-homogenized image as a guide map, performing guided filtering on the original transmission image to obtain an intermediate image; calculating the global grayscale mean and global standard deviation of the intermediate image, and converting the grayscale value of each pixel in the intermediate image into a standard score based on the global grayscale mean and global standard deviation; replacing values ​​in the standard score that are greater than a preset upper threshold with the upper threshold, and replacing values ​​that are less than a preset lower threshold with the lower threshold, to obtain a truncated standard score; and performing normalization operations on the truncated standard scores to obtain a standard input image with pixel values ​​distributed within a preset standard input range.

[0084] In this embodiment, morphological closing operation refers to a shape-based nonlinear filtering operation in digital image processing. It is typically defined as a combination of dilation and erosion operations on an image. It represents a geometrical processing method capable of filling holes smaller than the structural elements within an image object, connecting adjacent fractured regions, and maintaining the overall macroscopic contour of the object. For example, in processing X-ray images of stacked coffee powder, morphological closing operation functions similarly to laying a layer of elastic film with a certain tension on the uneven powder surface. This film does not get stuck in the tiny gaps between particles but adheres to the top of the particle cluster to form a smooth, continuous surface, thereby filtering out high-frequency particle texture details and retaining the background morphology reflecting the overall stack thickness variation.

[0085] Specifically, the original transmission image of the coffee powder to be detected is acquired using an X-ray detection device. A morphological closing operation is performed on this original transmission image using a structuring element (e.g., a rectangular or circular kernel with dimensions of 15×15 or 21×21) larger than the average pixel diameter of a single coffee powder particle in the image. This operation fills the microscopic gaps between coffee powder particles through a process of dilation followed by erosion, thereby extracting a smooth background envelope covering the coffee powder stacked area. This envelope serves as a global thickness trend map reflecting the overall thickness variation trend. Based on this global thickness trend map, a flat-field correction operation is performed on the original transmission image. Low-frequency grayscale fluctuations caused by uneven coffee powder stacking thickness are eliminated in the original image through mathematical difference or ratio calculations, resulting in a thickness-homogenized image with uniform background brightness. A guided filtering algorithm is then introduced. This smooth and structurally clear thickness-homogenized image is used as a guide map to filter the original transmission image, which contains rich details. The structural information of the guide map guides the filtering process, effectively suppressing random quantum noise from the detector while maximizing the preservation of edge gradient information of small foreign objects, resulting in an intermediate image with improved signal-to-noise ratio. Based on this, the global grayscale mean and global standard deviation of all pixels in the intermediate image are statistically calculated. Then, according to the Z-score calculation formula, the grayscale value of each pixel in the intermediate image is converted into a dimensionless standard score to eliminate baseline differences between different batches of imaging. To prevent a few extreme outliers (such as strong reflections from metallic foreign objects or detector defects) from stretching the subsequent data distribution range, values ​​in the calculated standard scores greater than a preset upper threshold (e.g., 3 times the standard deviation) are forcibly replaced with this upper threshold, and values ​​less than a preset lower threshold are forcibly replaced with this lower threshold, resulting in a truncated standard score with outliers removed. A linear normalization operation (e.g., min-max normalization) is performed on this truncated standard score, linearly mapping and compressing its values ​​to the preset standard input range required by the model (e.g., between 0 and 1 or -1 and 1), thus obtaining a standard input image with normalized data distribution and suppressed background interference.

[0086] S202: In the background reconstruction branch, the standard input image is subjected to feature compression and decoding reconstruction through a preset autoencoder model to generate a clean background prediction image.

[0087] For example, this step utilizes a deep generative network to model and reconstruct the texture distribution of normal coffee powder. An autoencoder architecture compresses high-dimensional image data into a low-dimensional latent space, allowing the model to ignore anomalous details (i.e., foreign object signals) that do not conform to overall statistical regularities at information bottlenecks, retaining only the common texture features of normal coffee powder. Based on these core features, inverse decoding and upsampling are performed to reconstruct an ideal background image that matches the original image's lighting and texture distribution but eliminates foreign objects, thus achieving effective separation of foreground foreign objects from background texture.

[0088] To ensure the autoencoder model possesses accurate background reconstruction capabilities, a specific strategy was employed during the model training phase: a clean dataset containing only normal coffee powder X-ray images without foreign objects was constructed for training. During training, a joint loss function (L...) was used. total =λ1L MSE +λ2L SSIM ) is optimized, where L total λ represents the total loss value used for backpropagation to update network weights; λ1 represents the weight coefficient of the mean squared error loss term (e.g., 0.8), used to adjust the proportion of pixel-level reconstruction error in the total loss; λ2 represents the weight coefficient of the structural similarity loss term (e.g., 0.2), used to adjust the proportion of texture structure error in the total loss; L MSE (Mean squared error) is used to constrain pixel-level reconstruction accuracy, L SSIM (Structural similarity loss) is used to constrain the structural consistency of coffee powder particle texture. By minimizing this joint loss, the model is forced to learn the statistical distribution patterns of normal coffee powder.

[0089] In one possible implementation, in the background reconstruction branch, a pre-defined autoencoder model is used to perform feature compression and decoding reconstruction on the standard input image to generate a clean background prediction image. Specifically, this includes: inputting the standard input image into the encoder subnet of the autoencoder model; extracting multiple sets of feature maps from the standard input image through multiple dilated convolutional layers with different dilation rates in the encoder subnet; stacking and concatenating the multiple sets of feature maps along their channel dimensions to generate an aggregated feature map; and performing convolution operations on the aggregated feature map through the first point convolutional layer in the encoder subnet to increase the number of channels in the aggregated feature map. The low-dimensional latent feature map is obtained by compressing it to a preset dimension. Weight coefficients for the corresponding channel dimensions are generated based on the global feature distribution of the low-dimensional latent feature map, and a weighted summation operation is performed on the low-dimensional latent feature map based on these weight coefficients to obtain a weighted latent feature map. The weighted latent feature map is input into the decoder subnet of the autoencoder model. The subpixel convolutional layers in the decoder subnet upsample and restore the features of the weighted latent feature map to generate a reconstructed feature map. A preset activation function maps the pixel values ​​of the reconstructed feature map to a target grayscale range consistent with the grayscale range of the standard input image, generating a clean background prediction image.

[0090] In the embodiments of this application, a dilated convolutional layer refers to a deep learning convolutional structure that injects holes (i.e., zero values) between the elements of a standard convolutional kernel to expand the coverage of the kernel. This represents a technique that significantly increases the receptive field of a neural network without increasing the number of parameters or computational cost. For example, in a coffee powder texture reconstruction task, a typical 3×3 convolution can only extract localized, minute particle features, while a dilated convolutional layer with an inflation rate of 2 can act like a magnifying glass, capturing a larger range of powder stacking textures and macroscopic distribution patterns. This allows the model to simultaneously understand microscopic particle details and macroscopic background undulations.

[0091] Specifically, the system inputs the preprocessed standard input image into the encoder subnet of a predefined autoencoder model. During the encoding phase, multiple dilated convolutional layers with different dilation rates (e.g., 1, 2, and 5) configured within the encoder subnet extract features from the image in parallel or sequentially, capturing texture information at different scales to generate multiple sets of feature maps containing rich multi-scale contextual information. These multiple sets of feature maps are stacked and concatenated along the channel dimension to form an aggregated feature map containing information from all scales. To remove redundant information and extract core features, the first point convolutional layer (i.e., a 1×1 convolution) in the encoder subnet performs a convolution operation on this aggregated feature map. Through linear combination of channels, the number of channels is compressed to a predefined dimension, resulting in a highly abstract low-dimensional latent feature map. A global average pooling operation is performed on the low-dimensional latent feature map in its spatial dimension, compressing the spatial dimension of the feature map to 1×1, resulting in a feature vector containing only channel information. Weight coefficients representing the importance of each channel are generated based on these coefficients, and a weighted summation operation is performed on the low-dimensional latent feature map according to these weight coefficients to enhance the response to normal coffee powder background texture and suppress anomalous signals, resulting in a weighted latent feature map. This weighted latent feature map is input into the decoder subnetwork, where subpixel convolutional layers perform upsampling operations. By periodically rearranging the pixels in the channel dimension to the spatial dimension, high-fidelity texture details are reconstructed while restoring image resolution, generating a reconstructed feature map. A preset activation function (such as Sigmoid or Tanh) is used to non-linearly map the pixel values ​​of the reconstructed feature map, forcing them to be within a grayscale range completely consistent with the standard input image (e.g., between 0 and 1), thus outputting the final clean background prediction image.

[0092] S203: Calculate the pixel grayscale difference between the standard input image and the clean background prediction image at corresponding positions to obtain the foreign object residual image composed of the pixel grayscale difference.

[0093] In this embodiment, the foreign object residual image refers to a two-dimensional data matrix generated through mathematical difference operations, specifically used to characterize the degree to which the input image deviates from the normal background distribution. It represents abnormal texture or structural regions in the original image that cannot be reconstructed by the background reconstruction model. For example, when there is a plastic sheet in the original image that cannot be reconstructed as coffee powder texture, this plastic sheet region will appear as a bright or high-value region in the foreign object residual image, while the normal coffee powder background region will appear as a low-value region approaching zero because it has been successfully canceled out.

[0094] Specifically, the system calls the preprocessed standard input image cached in memory, and the clean background prediction image output by the background reconstruction branch. Since both have identical spatial resolution and channel count, the system performs pixel-by-pixel difference calculations on these two images. For each coordinate position in the image matrix, the system reads the true grayscale value of the standard input image and the predicted grayscale value of the clean background prediction image at that point, and calculates the difference between them (usually using absolute values ​​to simultaneously capture foreign objects brighter and darker than the background). This value is the pixel grayscale difference, which quantifies the degree to which the current pixel deviates from the statistical regularity of the normal coffee powder texture. The pixel grayscale differences calculated for all coordinate points are recombinated according to the original spatial arrangement to construct a complete two-dimensional grayscale image, thus obtaining the foreign object residual image composed of pixel grayscale differences. In this image, most pixels belonging to the normal coffee powder background have differences approaching zero because the input and predicted values ​​are highly similar, thus achieving physical stripping of the background texture; while foreign object pixels retain larger differences due to reconstruction failure, forming high-contrast signal features.

[0095] S204: Perform convolutional feature extraction on the foreign object residual image to generate a spatial residual attention weight map.

[0096] For example, this step aims to transform the physical difference signals in the foreign object residual image into a probability weight matrix with spatial guidance. A deep convolutional network is used to encode high-dimensional features of the residual image, capturing potential foreign object morphology and edge information. Subsequently, a channel compression mechanism aggregates the multidimensional features into a single-channel spatial saliency distribution map. Finally, a nonlinear activation function quantizes these saliency values ​​into probability coefficients reflecting the likelihood of the foreign object's presence, thereby providing precise spatial attention guidance for subsequent object detection networks.

[0097] In one possible implementation, convolutional feature extraction is performed on the foreign object residual image to generate a spatial residual attention weight map. Specifically, this includes: performing multi-layer convolution operations on the foreign object residual image using multiple preset convolution kernels to extract the spatial features of the foreign object residual image and obtain a multi-channel difference feature map; using the weights of the convolution kernels in the preset second-point convolutional layer as weighting factors, performing a weighted summation operation on multiple feature values ​​distributed along the channel at each spatial pixel position of the multi-channel difference feature map to obtain the fused feature value of each spatial pixel position, and constructing a single-channel feature map based on the fused feature values ​​of all spatial pixel positions; mapping each pixel value in the single-channel feature map through an activation function to normalize the pixel values ​​to a preset value range, thereby generating the spatial residual attention weight map.

[0098] In this embodiment, the spatial residual attention weight map refers to a two-dimensional matrix generated by a deep learning network, with values ​​distributed within a specific range (typically 0 to 1), used to represent the contribution or importance probability of each spatial location in the input image to the final foreign object detection task. For example, in the attention weight map of an X-ray image of coffee powder containing tiny rubber particles, the pixel values ​​of the coordinate region where the rubber particles are located will approach 1, indicating that the network should focus on the features of that region; while the pixel values ​​of the large area of ​​coffee powder background will approach 0, indicating that the network should ignore the interference of this irrelevant information.

[0099] Specifically, the system receives the foreign object residual image calculated in the previous step, calls the pre-trained feature extraction network parameters in memory, and performs multi-layer convolution operations on the foreign object residual image using multiple preset convolution kernels (e.g., 3x3 or 5x5 kernels). This process aims to deeply mine the potential spatial structure information in the residual image, capture the subtle differences between the foreign object and the background at the residual level (such as edge gradients, texture abrupt changes, etc.), thereby mapping the original residual data to a high-dimensional feature space and obtaining a multi-channel difference feature map containing rich spatial detail information. To compress and transform these feature information scattered in high-dimensional channels into a single attention signal, the kernel weights learned in the preset second-point convolutional layer (i.e., a 1x1 convolutional layer) are used as weighting factors. It should be noted that the kernel weight parameters of this point convolutional layer are automatically learned through backpropagation during the end-to-end training of the network. It can automatically identify which channels contain key information about foreign objects (such as edges and density abrupt changes) and assign these channels larger positive weights, while assigning smaller or negative weights to channels containing noise, thereby achieving information focusing from multi-dimensional features to a single-dimensional attention map. For each spatial pixel location (i.e., each coordinate point on the image plane) in the multi-channel difference feature map, multiple feature values ​​distributed along the channel are extracted. These feature values ​​are multiplied one by one with the corresponding weight factors and accumulated, performing a weighted summation operation to calculate the fused feature value for each spatial pixel location. The fused feature values ​​calculated for all coordinate points are rearranged and combined according to their original spatial locations to form a single-channel feature map that reflects the spatial saliency distribution. To convert these continuous feature values ​​into probability coefficients that can be used for weighting, a preset activation function (such as the sigmoid function) is used to perform a non-linear mapping on each pixel value in the single-channel feature map, forcing all pixel values ​​to be normalized to a preset value range (usually a closed interval between 0 and 1), generating a spatial residual attention weight map that can accurately guide the subsequent object detection network to focus on suspected foreign object regions.

[0100] S205: In the object detection branch, multi-layer convolution operations are performed on the standard input image to obtain the original semantic feature map.

[0101] For example, this step utilizes a deep convolutional neural network to construct a multi-scale feature pyramid architecture to capture visual information at different levels in the image. A feature extraction backbone network acquires hierarchical features with different receptive fields, and a feature fusion mechanism is used to complementaryly integrate deep, high-level semantic abstractions with shallow, high-resolution detail information. This generates an original semantic feature map that contains both macroscopic structural information and retains microscopic edge features, ensuring that subsequent networks can simultaneously address the identification and localization needs of foreign objects of different sizes.

[0102] To address the challenges of diverse foreign object morphologies and scarce samples in coffee powder, Mosaic data augmentation was employed during the training phase of the object detection network. This involved randomly cropping and stitching four training images to enrich the combination of background and target scenes. Simultaneously, Focal Loss was used to balance the significant difference in the number of foreign object samples (positive samples) and background samples (negative samples), thereby enhancing the model's ability to learn from difficult samples.

[0103] In one possible implementation, in the object detection branch, multi-layer convolution operations are performed on the standard input image to obtain the original semantic feature map. Specifically, this includes: performing progressive downsampling convolution operations on the standard input image to extract multi-scale intermediate feature maps with different resolution levels, wherein the multi-scale intermediate feature maps include a first resolution feature map and a second resolution feature map, the resolution level of the first resolution feature map is lower than that of the second resolution feature map, and the resolution of the first resolution feature map is higher than that of the second resolution feature map; performing an upsampling operation on the second resolution feature map, and performing a feature fusion operation between the upsampling second resolution feature map and the first resolution feature map to generate a fused feature map; and performing a convolution operation on the fused feature map to obtain the original semantic feature map containing multi-scale contextual information.

[0104] In the embodiments of this application, multi-scale intermediate feature maps refer to the set of feature data with different spatial resolutions and semantic abstractions output from different depths of the network during feature extraction in a deep convolutional neural network. These feature maps are used to represent the visual information representation of image data at different receptive field scales. For example, in the backbone architecture of an object detection network, multi-scale intermediate feature maps typically include feature maps located in the shallow layers of the network with higher resolution but weaker semantic information (mainly preserving the geometric edges and texture details of foreign objects, suitable for locating small targets), and feature maps located in the deep layers of the network with lower resolution but containing strong semantic category information (mainly aggregating the overall structural features of foreign objects, suitable for category judgment).

[0105] Specifically, the system invokes a pre-defined feature extraction backbone network (such as ResNet or Darknet) to perform progressively downsampling convolution operations (such as convolution or pooling operations with a stride of 2) on the standard input image. As the network depth increases, the spatial size of the feature maps gradually decreases while the number of channels gradually increases, thereby extracting a series of multi-scale intermediate feature maps with different resolution levels. In this process, the system clearly identifies and acquires the first-resolution feature maps (e.g., feature layers with a downsampling factor of 8) located in shallower layers of the network and having higher spatial resolution, as well as the second-resolution feature maps (e.g., feature layers with a downsampling factor of 16 or 32) located in deeper layers of the network and having lower spatial resolution but higher semantic level. A top-down feature enhancement pathway is constructed, and upsampling operations (such as nearest neighbor interpolation or bilinear interpolation) are performed on the smaller second-resolution feature maps to enlarge their spatial size to be completely consistent with the first-resolution feature maps. The magnified second-resolution feature map is then fused with the first-resolution feature map using a feature fusion operation (such as concatenation along the channel dimension or element-wise addition). This injects deep, high-level semantic information into the shallow, high-resolution features, generating a fused feature map. To eliminate aliasing effects that may be introduced by upsampling interpolation and feature stacking, a smooth convolution operation is performed on the fused feature map using a convolution kernel (usually a 3x3 convolution). The result is an original semantic feature map containing multi-scale contextual information, which combines high-resolution localization capabilities with strong semantic classification capabilities.

[0106] S206: The original semantic feature map is weighted and fused using the spatial residual attention weight map to obtain the foreign object sensitive feature map.

[0107] For example, this step utilizes a spatial attention mechanism to achieve deep fusion and enhancement of multi-source features. The difference weights generated by the background reconstruction branch are used as a spatial prior guide and projected into the semantic feature space extracted by the target detection branch. Through point-by-point feature modulation, invalid activation regions representing normal coffee powder background in the feature map are suppressed in a targeted manner, while the feature response intensity of potential foreign object regions is significantly amplified. At the same time, a residual connection structure is introduced to superimpose and fuse the enhanced features with the original semantic features, maximizing the saliency of the foreign object signal while maintaining feature integrity, thereby generating a foreign object sensitive feature map specifically for high-precision identification.

[0108] In one possible implementation, a foreign object sensitive feature map is obtained by weighted fusion of the original semantic feature map using a spatial residual attention weight map. Specifically, this includes: obtaining the total number of channels in the original semantic feature map; copying the spatial residual attention weight map along the channel dimension until the number of copied channels equals the total number of channels, generating an expanded weight map with the same size as the original semantic feature map; constructing a global traversal index set containing channel dimension coordinates and spatial plane coordinates, the global traversal index set covering every pixel in the expanded weight map; extracting the first pixel value from the expanded weight map and the second pixel value from the original semantic feature map based on a target index, where the target index is any index in the global traversal index set; multiplying the first pixel value and the second pixel value to obtain the weighted pixel value corresponding to the target index, and generating a weighted feature map containing all weighted pixel values ​​based on the global traversal index set; adding the weighted feature map to the pixel value at the target index in the original semantic feature map to obtain the fused pixel value corresponding to the target index, and generating a foreign object sensitive feature map containing all fused pixel values ​​based on the global traversal index set.

[0109] In this embodiment, the foreign object sensitive feature map refers to a high-dimensional data representation containing highly saliency target features, modulated by an attention mechanism and enhanced by residual connections. It represents a set of feature data that, while preserving the semantic information of the original image, significantly amplifies the intensity of the foreign object signal and suppresses background texture interference through mathematical weighting. For example, in this feature map, pixel regions representing plastic pieces or pebbles will exhibit stronger activation response values ​​than the surrounding coffee powder background, thus facilitating accurate identification by subsequent detection networks.

[0110] Specifically, the system reads the metadata of the original semantic feature map generated by the object detection branch to accurately obtain the total number of channels contained in the original semantic feature map (e.g., 64 or 128 channels). To apply the two-dimensional single-channel spatial residual attention weight map to the three-dimensional multi-channel feature map, a broadcast mechanism is used to copy the spatial residual attention weight map along the channel dimension, i.e., continuously stacking the same weight map in the depth direction until the number of copied channels equals the total number of channels, thereby generating an extended weight map with the same height, width, and number of channels as the original semantic feature map. To achieve pixel-level fine-grained computation, a global traversal index set containing channel dimension coordinates and spatial plane coordinates is constructed. This global traversal index set logically completely covers every pixel in the data matrix of the extended weight map (and the original semantic feature map). During the calculation process, based on the target index (i.e., any specific three-dimensional coordinate point) in this set, the first pixel value (i.e., the attention weight coefficient corresponding to that point) in the extended weight map and the second pixel value (i.e., the original feature response value corresponding to that point) in the original semantic feature map are extracted from memory, where the target index is any index in the global traversal index set. A feature intensity modulation operation is performed by multiplying the first pixel value by the second pixel value. Weights are used to amplify the feature response of key regions or suppress the response of background regions, resulting in a weighted pixel value corresponding to the target index. To prevent the loss of original feature information and maintain the stability of network gradient propagation, a residual learning paradigm is used for fusion, mathematically expressed as Y = X + X⊙W, where Y represents the foreign object sensitive feature map generated after fusion, X is the original semantic feature map, W is the expanded attention weight map, and ⊙ represents element-wise multiplication. This design not only highlights foreign object features using the weight map (X⊙W term) but also preserves the background context information in the original feature map (X term) through short-circuit connections, effectively avoiding the gradient vanishing problem caused by increasing network depth. Based on a global traversal of the index set, the values ​​calculated at all positions are recombined to generate a weighted feature map containing all weighted pixel values. To prevent the loss of original feature information and maintain the stability of network gradient propagation, the idea of ​​residual learning is adopted. The pixel values ​​at the target index are added to the weighted feature map and the original semantic feature map, and the feature is superimposed and fused to obtain the fused pixel values ​​corresponding to the target index. Then, these fused values ​​are integrated again based on the global traversal of the index set to finally generate a foreign object sensitive feature map containing all fused pixel values.

[0111] S207: The foreign object detection result is obtained by performing target classification and bounding box regression decoding on the foreign object sensitive feature map through a preset prediction head network.

[0112] For example, this step serves as the decision output terminal of the entire detection model, responsible for transforming the abstract semantic information enhanced by feature engineering into intuitive detection conclusions. Utilizing a decoupled prediction mechanism, it performs parallel probabilistic assessment (classification) of the foreign object category and geometric correction (regression) of its spatial location. Subsequently, the relative offset output by the model is mapped back to the absolute coordinate system of the original image, and redundant predictions are eliminated from a massive pool of candidate results through a post-processing filtering mechanism based on confidence and overlap, ultimately locking in the precise boundaries and category labels of the foreign object.

[0113] In one possible implementation, a foreign object detection result is obtained by performing target classification and bounding box regression decoding on the foreign object sensitive feature map through a preset prediction head network. Specifically, this includes: performing a convolution operation on the foreign object sensitive feature map through a classification subnetwork in the prediction head network to output a category prediction map containing a confidence score indicating the presence of a foreign object at each grid point; performing a convolution operation on the foreign object sensitive feature map through a regression subnetwork in the prediction head network to output a bounding box prediction map containing the center coordinate offset and size offset of the foreign object at each grid point; using a coordinate decoding unit in the prediction head network, superimposing the center coordinate offset onto a preset grid reference coordinate to obtain the absolute center coordinate, and applying the size offset to a preset anchor frame reference size to obtain the absolute size; and based on the absolute center coordinate and absolute... The system constructs predicted bounding boxes based on the original image coordinate system. These predicted bounding boxes are then sorted by confidence score from highest to lowest to generate a candidate bounding box sequence, and an empty list of retained results is created. The following filtering steps are performed iteratively until the candidate bounding box sequence is empty, and the retained results list is output as the foreign object detection result: Within the current filtering period, the bounding box with the highest confidence score is selected from the candidate bounding box sequence as the baseline target box, and this baseline target box is transferred from the candidate bounding box sequence to the retained results list. The intersection-union ratio (IUR) of each bounding box in the candidate bounding box sequence (excluding the baseline target box) with the baseline target box is calculated. Bounding boxes with an IUR greater than a preset overlap threshold are removed from the candidate bounding box sequence, resulting in an updated candidate bounding box sequence for the next filtering period.

[0114] In this embodiment, the prediction head network refers to the terminal functional component in the deep learning object detection model architecture, located after the feature extraction backbone network and the feature fusion layer. It represents the output layer structure responsible for mapping highly abstract feature maps into specific target category probabilities and location geometric coordinates. For example, in the coffee powder foreign object detection scenario, the prediction head network acts like the decision area of ​​an experienced quality inspector's brain after clearly seeing the details of an image. It receives attention-enhanced feature maps and calculates the probability (i.e., confidence) that the current region contains a foreign object (such as a piece of plastic) and the precise boundary range (i.e., bounding box) of the foreign object in the image through its internal parallel branching structure, thereby completing the transformation from feature understanding to result output.

[0115] Specifically, the system utilizes an independent classification subnetwork (typically composed of several convolutional layers) within the prediction head network to perform convolution operations on the input foreign object sensitive feature map. This performs binary or multi-class classification predictions on each spatial grid point on the feature map, outputting a category prediction map containing the confidence score of the presence of a foreign object at each grid point. Simultaneously, a parallel regression subnetwork performs convolution operations on the same foreign object sensitive feature map, predicting the geometric deformation parameters of the foreign object relative to a preset anchor frame, outputting a bounding box prediction map containing the center coordinate offset and size offset of the foreign object at each grid point. The coordinate decoding unit in the prediction head network is then called to restore the physical coordinates of these relative offsets. Specifically, the sigmoid function is used to process the center coordinate offset, and it is added to the coordinates of the top-left corner of the current grid cell to obtain the absolute center coordinates of the foreign object in the image. An exponential operation is performed on the size offset, and it is multiplied by the width and height reference values ​​of the preset anchor frame to obtain the absolute dimensions (width and height) of the foreign object. It is important to note that the preset anchor frame reference dimensions are not arbitrarily selected but are derived from statistical analysis of historical coffee powder foreign object data. Specifically, by performing K-means clustering analysis on the width and height data of a large number of labeled foreign object samples' true bounding boxes, several sets of aspect ratios and scales that best represent the morphology of typical foreign objects (such as thin metal wires, pebbles, and sheet-like plastic) were determined. These were then embedded into the network as preset anchor box parameters to improve regression convergence speed and accuracy. Based on these calculated absolute center coordinates and absolute dimensions, predicted bounding boxes directly corresponding to the original image coordinate system were constructed. To remove redundant boxes from repeated detections, all generated predicted bounding boxes were sorted from high to low confidence scores, generating an ordered candidate bounding box sequence, and an initially empty list of retained results was created. A cyclical filtering process then commences: the following filtering steps are executed repeatedly until the candidate bounding box sequence is empty. Within each current filtering cycle, the bounding box with the highest confidence score is first selected from the candidate bounding box sequence as the baseline target box, and this baseline target box is transferred from the candidate bounding box sequence to the retained results list. Then, the intersection-union ratio (IUR) of each bounding box in the candidate bounding box sequence (excluding the baseline target box) with the current baseline target box is calculated. Based on the calculation results, bounding boxes with an intersection-union ratio (IU) greater than a preset overlap threshold are removed from the candidate bounding box sequence (these boxes are considered repeated predictions of the same target). An updated candidate bounding box sequence is then obtained to enter the next filtering cycle. When the cycle ends, the retained result list is output as the final foreign object detection result.

[0116] This embodiment also discloses an electronic device, referring to... Figure 3The electronic device may include: at least one processor 301, at least one communication bus 302, user interface 303, network interface 304, and at least one memory 305.

[0117] The communication bus 302 is used to enable communication between these components.

[0118] The user interface 303 may include a display screen and a camera. Optionally, the user interface 303 may also include a standard wired interface and a wireless interface.

[0119] The network interface 304 may optionally include a standard wired interface or a wireless interface (such as a Wi-Fi interface).

[0120] The processor 301 may include one or more processing cores. The processor 301 connects to various parts of the server using various interfaces and lines, and performs various server functions and processes data by running or executing instructions, programs, code sets, or instruction sets stored in memory 305, and by calling data stored in memory 305. Optionally, the processor 301 may be implemented using at least one hardware form of Digital Signal Processing (DSP), Field-Programmable Gate Array (FPGA), or Programmable Logic Array (PLA). The processor 301 may integrate one or a combination of several of the following: Central Processing Unit (CPU), Graphics Processing Unit (GPU), and modem. The CPU primarily handles the operating system, user interface, and applications; the GPU is responsible for rendering and drawing the content required for display; and the modem handles wireless communication. It is understood that the modem may also not be integrated into the processor 301 and may be implemented as a separate chip.

[0121] The memory 305 may include random access memory (RAM) or read-only memory. Optionally, the memory 305 may include a non-transitory computer-readable storage medium. The memory 305 can be used to store instructions, programs, code, code sets, or instruction sets. The memory 305 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for at least one function (such as touch function, sound playback function, image playback function, etc.), instructions for implementing the above-described method embodiments, etc.; the data storage area may store data involved in the above-described method embodiments, etc. Optionally, the memory 305 may also be at least one storage device located remotely from the aforementioned processor 301. Figure 3 As shown, the memory 305, which serves as a computer storage medium, may include an operating system, a network communication module, a user interface module, and an application program for a coffee powder foreign object detection method based on background reconstruction.

[0122] exist Figure 3 In the electronic device shown, the user interface 303 is mainly used to provide an input interface for the user and to obtain the user input data; while the processor 301 can be used to call an application stored in the memory 305 for a coffee powder foreign object detection method based on background reconstruction. When executed by one or more processors 301, the electronic device executes one or more methods as described in the above embodiments.

[0123] It should be noted that, for the sake of simplicity, the foregoing method embodiments are all described as a series of actions. However, those skilled in the art should understand that this application is not limited to the described order of actions, as some steps may be performed in other orders or simultaneously according to this application. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are preferred embodiments, and the actions and modules involved are not necessarily essential to this application.

[0124] In the above embodiments, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions in other embodiments.

[0125] In some embodiments of this application, a computer-readable storage medium is provided, including instructions that, when executed on the electronic device, cause the electronic device to perform a background reconstruction-based coffee powder foreign object detection method according to an embodiment of this application.

[0126] In some embodiments of this application, a computer program product is also provided, which, when run on an electronic device, causes the electronic device to execute a background reconstruction-based coffee powder foreign object detection method according to an embodiment of this application.

[0127] In the several embodiments provided in this application, it should be understood that the disclosed apparatus can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the shown or discussed mutual couplings or direct couplings or communication connections may be through some service interfaces; indirect couplings or communication connections between apparatuses or units may be electrical or other forms.

[0128] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0129] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.

[0130] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage device (CMD). Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a memory 305 and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned memory 305 includes various media capable of storing program code, such as a USB flash drive, external hard drive, magnetic disk, or optical disk.

[0131] The foregoing description is merely an exemplary embodiment of this disclosure and should not be construed as limiting the scope of this disclosure. Any equivalent changes and modifications made in accordance with the teachings of this disclosure shall still fall within the scope of this disclosure. Those skilled in the art will readily conceive of other embodiments of this disclosure upon considering the disclosure in this specification. This application is intended to cover any variations, uses, or adaptations of this disclosure that follow the general principles of this disclosure and include common knowledge or customary techniques in the art not described in this disclosure. The specification and embodiments are considered exemplary only, and the scope of this application is defined by the claims.

Claims

1. A method for detecting foreign objects in coffee powder based on background reconstruction, characterized in that, The method includes: The original transmission image of the coffee powder to be detected is obtained, and the original transmission image is subjected to grayscale normalization to obtain a standard input image. The standard input image is then input into a preset background reconstruction branch and a preset target detection branch, respectively. In the background reconstruction branch, the standard input image is subjected to feature compression and decoding reconstruction processing through a preset autoencoder model to generate a clean background prediction image; Calculate the pixel grayscale difference between the standard input image and the clean background prediction image at corresponding positions to obtain the foreign object residual image composed of the pixel grayscale difference; Convolutional feature extraction is performed on the foreign object residual image to generate a spatial residual attention weight map; In the target detection branch, multi-layer convolution operations are performed on the standard input image to obtain the original semantic feature map; The original semantic feature map is weighted and fused using the spatial residual attention weight map to obtain the foreign object sensitive feature map; The foreign object detection result is obtained by performing target classification and bounding box regression decoding on the foreign object sensitive feature map through a preset prediction head network.

2. The method according to claim 1, characterized in that, The process of acquiring the original transmission image of the coffee powder to be detected and performing grayscale normalization on the original transmission image to obtain a standard input image specifically includes: The original transmission image is calculated using morphological closing operations, and the background envelope is extracted as a global thickness trend map. Based on the global thickness trend map, a flat field correction operation is performed on the original transmission image to obtain a thickness homogenized image. Using the thickness homogenized image as a guide image, the original transmission image is subjected to guided filtering to obtain an intermediate image; Calculate the global grayscale mean and global standard deviation of the intermediate image, and based on the global grayscale mean and global standard deviation, convert the grayscale value of each pixel position in the intermediate image into a standard score; The standard scores are truncated by replacing the values ​​that are greater than the preset upper threshold with the upper threshold and replacing the values ​​that are less than the preset lower threshold with the lower threshold. The truncated standard scores are normalized to obtain the standard input image whose pixel values ​​are distributed within a preset standard input range.

3. The method according to claim 1, characterized in that, In the background reconstruction branch, the standard input image is subjected to feature compression and decoding reconstruction processing using a preset autoencoder model to generate a clean background prediction image, specifically including: The standard input image is input into the encoder subnet of the autoencoder model, and multiple sets of feature maps of the standard input image are extracted through multiple dilated convolutional layers with different dilation rates in the encoder subnet. The multiple sets of feature maps are stacked and stitched along the channel dimension of the multiple sets of feature maps to generate an aggregated feature map; The aggregated feature map is convolved by the first point convolutional layer in the encoder subnet to compress the number of channels of the aggregated feature map to a preset dimension, thereby obtaining a low-dimensional latent feature map. Based on the global feature distribution of the low-dimensional latent feature map, weight coefficients corresponding to the channel dimensions are generated, and the low-dimensional latent feature map is weighted and summed based on the weight coefficients to obtain a weighted latent feature map. The weighted latent feature map is input into the decoder subnet of the autoencoder model. The weighted latent feature map is upsampled and its features are restored by the subpixel convolutional layer in the decoder subnet to generate a reconstructed feature map. The pixel values ​​of the reconstructed feature map are mapped to a target grayscale range that is consistent with the grayscale range of the standard input image by a preset activation function, thereby generating the pure background prediction image.

4. The method according to claim 3, characterized in that, The step of extracting convolutional features from the foreign object residual image to generate a spatial residual attention weight map specifically includes: The foreign object residual image is subjected to multi-layer convolution operation by multiple preset convolution kernels to extract the spatial features of the foreign object residual image and obtain a multi-channel difference feature map. Using the pre-set kernel weights in the second point convolutional layer as weighting factors, the multi-channel difference feature map is weighted and summed along the channel at each spatial pixel position to obtain the fused feature value of each spatial pixel position, and a single-channel feature map is constructed based on the fused feature values ​​of all spatial pixel positions. The activation function maps each pixel value in the single-channel feature map to normalize the pixel values ​​to a preset range, thereby generating the spatial residual attention weight map.

5. The method according to claim 1, characterized in that, In the target detection branch, multi-layer convolution operations are performed on the standard input image to obtain the original semantic feature map, specifically including: The standard input image is subjected to progressive downsampling convolution operation to extract multi-scale intermediate feature maps with different resolution levels. The multi-scale intermediate feature maps include a first resolution feature map and a second resolution feature map. The resolution level of the first resolution feature map is lower than that of the second resolution feature map, and the resolution of the first resolution feature map is higher than that of the second resolution feature map. The second resolution feature map is upsampled, and the upsampled second resolution feature map is fused with the first resolution feature map to generate a fused feature map. The fused feature map is convolved to obtain the original semantic feature map containing multi-scale contextual information.

6. The method according to claim 5, characterized in that, The step of performing weighted fusion processing on the original semantic feature map using the spatial residual attention weight map to obtain the foreign object sensitivity feature map specifically includes: Obtain the total number of channels contained in the original semantic feature map; The spatial residual attention weight map is copied along the channel dimension until the number of copied channels equals the total number of channels, thereby generating an extended weight map with the same size as the original semantic feature map. Construct a global traversal index set containing channel dimension coordinates and spatial plane coordinates, wherein the global traversal index set covers every pixel in the extended weight map; The first pixel value in the extended weight map and the second pixel value in the original semantic feature map are extracted based on the target index, where the target index is any index in the global traversal index set. Multiply the first pixel value by the second pixel value to obtain the weighted pixel value corresponding to the target index, and generate a weighted feature map containing all weighted pixel values ​​based on the global traversal index set. The weighted feature map is added to the pixel value of the original semantic feature map at the target index to obtain the fused pixel value corresponding to the target index, and the foreign object sensitive feature map containing all fused pixel values ​​is generated based on the global traversal index set.

7. The method according to claim 6, characterized in that, The step of performing target classification and bounding box regression decoding on the foreign object sensitive feature map through a preset prediction head network to obtain the foreign object detection result specifically includes: The foreign object sensitive feature map is convolved by the classification subnetwork in the prediction head network to output a category prediction map containing the confidence score of the presence of a foreign object at each grid point. The foreign object sensitive feature map is convolved by the regression subnetwork in the prediction head network to output a bounding box prediction map containing the coordinate offset and size offset of the foreign object center at each grid point. The center coordinate offset is superimposed on the preset grid reference coordinates by the coordinate decoding unit in the prediction head network to obtain the absolute center coordinates, and the size offset is applied to the preset anchor frame reference size to obtain the absolute size; Construct a predicted bounding box based on the absolute center coordinates and the absolute size, using the original image coordinate system. The predicted bounding boxes are sorted from high to low according to the confidence scores to generate a candidate bounding box sequence, and an empty list of retained results is created. The following filtering steps are performed repeatedly until the candidate bounding box sequence is empty, and the list of retained results is output as the foreign object detection result: During the current screening period, the bounding box with the highest confidence score is selected from the candidate bounding box sequence as the baseline target box, and the baseline target box is transferred from the candidate bounding box sequence to the retained results list; Calculate the intersection-union ratio (IUR) between each bounding box in the candidate bounding box sequence (excluding the reference bounding box) and the reference bounding box; Boundaries in the candidate bounding box sequence whose intersection-union ratio (IU) is greater than a preset overlap threshold are removed to obtain an updated candidate bounding box sequence for the next filtering cycle.

8. An electronic device, characterized in that, Including processor and memory; The memory is used to store computer program code, the computer program code including computer instructions, and the processor invokes the computer instructions to cause the electronic device to perform the method as described in any one of claims 1-7.

9. A computer-readable storage medium storing computer instructions, characterized in that, When the computer instructions are executed on the electronic device, the electronic device causes the electronic device to perform the method as described in any one of claims 1-7.

10. A computer program product, characterized in that, When the computer program product is run on an electronic device, it causes the electronic device to perform the method as described in any one of claims 1-7.