Garbage identification and classification method and system based on image sample distance measurement
By using an image sample distance metric method, a garbage distance metric classification model is constructed using preprocessing and an improved ResNet model, which solves the problem of low accuracy in garbage identification and classification, and achieves efficient and accurate garbage classification.
Patent Information
- Application Number
- CN202411498111.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-25
- Publication Date
- 2025-01-24
AI Technical Summary
The accuracy of existing waste identification and classification technologies is generally low, and they are difficult to effectively classify irregularly shaped waste, resulting in a limited amount of waste sample data that can be used for modeling.
We employ an image sample distance metric-based approach, constructing a garbage distance metric classification model through preprocessing, improvement of the pre-trained ResNet model, hybrid attention mechanism, and KNN algorithm. This model includes image scaling, grayscale conversion, morphological processing, feature extraction, and Euclidean distance metric.
It improved the accuracy and efficiency of garbage sorting, reduced human intervention, enhanced image detail clarity, reduced interference from complex backgrounds, optimized model performance, and improved robustness.
Smart Images

Figure CN119360355A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of garbage identification and classification, and in particular relates to a garbage identification and classification method and system based on image sample distance measurement. Background Art
[0002] Garbage classification is an important means to achieve a resource-saving and environmentally friendly society, which helps to improve the recycling rate of resources and reduce environmental pollution. Effective garbage classification can reduce processing costs and save land resources.
[0003] However, the accuracy of current garbage recognition and classification technologies is generally low, far from meeting the standards for large-scale applications. Existing garbage recognition and classification technologies mostly use data cleaning and feature extraction to identify garbage, but due to the diversity of garbage shapes, many irregularly shaped garbage are discarded during the data cleaning stage, resulting in limited garbage sample data that can be used for modeling. Most irregularly shaped garbage is difficult to be accurately classified by existing garbage recognition and classification systems.
[0004] Therefore, there is an urgent need to develop an intelligent garbage recognition and classification system to cope with the challenges of the irregularity and complexity of garbage images. At the same time, there is an urgent need to improve the recognition accuracy of garbage recognition and classification to better meet the garbage classification needs in real life. Summary of the invention
[0005] In order to solve the above problems existing in the prior art, the present invention proposes a garbage identification and classification method and system based on image sample distance measurement.
[0006] The purpose of the present invention can be achieved through the following technical solutions:
[0007] A garbage identification and classification method based on image sample distance measurement, comprising:
[0008] Acquire garbage image data, wherein the garbage image data includes recyclable garbage images, kitchen waste images, hazardous waste images, and other garbage images;
[0009] According to the junk image data, junk image preprocessing is performed to obtain junk standard image data, and according to the junk standard image data, a junk image sample set is established by junk type labeling, wherein the junk image preprocessing includes image scaling, grayscale conversion, image enhancement, and morphological processing;
[0010] Constructing a garbage distance metric classification model through a pre-trained ResNet model according to the garbage image sample set;
[0011] Obtain garbage image data to be classified, and obtain garbage type labels according to the garbage image data to be classified through the garbage distance measurement classification model.
[0012] Preferably, the obtaining of junk image data comprises:
[0013] Using high-definition cameras to collect common garbage images in life at multiple angles and under different lighting conditions;
[0014] The recyclable garbage images include paper images, can images, and plastic bottle images;
[0015] The kitchen waste images include potatoes, fruit peels, peppers, bananas, tomatoes, and green vegetables;
[0016] The hazardous waste includes batteries and medicines;
[0017] The other garbage includes broken porcelain pieces, broken glass, and broken bricks.
[0018] Preferably, obtaining junk image standard data through junk image preprocessing according to the junk image data comprises:
[0019] The image scaling is to uniformly scale the junk image data to 224×224 pixels;
[0020] The grayscale conversion includes converting the garbage image data into a garbage grayscale image by a weighted average method;
[0021] The image enhancement includes removing noise by Gaussian blurring according to the garbage grayscale image and enhancing contrast by histogram equalization;
[0022] The morphological processing includes normalizing the garbage shape through dilation and erosion operations.
[0023] Preferably, constructing a garbage distance metric classification model through a pre-trained ResNet model according to the garbage image sample set includes:
[0024] The garbage feature extractor is obtained by improving the model structure based on the pre-trained ResNet model;
[0025] Extracting a multidimensional feature set of junk images through the junk feature extractor according to the junk image sample set;
[0026] Converting the garbage image multidimensional feature set into a garbage feature vector set through a global average pooling layer, wherein the global average pooling layer is a global average pooling layer added after the garbage feature extractor;
[0027] A garbage distance measurement classification model is constructed according to the garbage feature vector set using a KNN algorithm.
[0028] Preferably, the garbage feature extractor obtained by improving the model structure according to the pre-trained ResNet model includes:
[0029] The model structure improvement includes removing the last fully connected layer of the pre-trained ResNet model, retaining the convolutional layer and the pooling layer, and introducing a hybrid attention mechanism in the residual block of the pre-trained ResNet model, wherein the hybrid attention mechanism combines the channel attention and the spatial attention mechanism.
[0030] Preferably, the constructing a garbage distance measurement classification model according to the garbage feature vector set by using a KNN algorithm includes:
[0031] According to the garbage feature vector set, a garbage training feature vector set and a garbage test feature vector set are obtained by randomly dividing the garbage feature vector set;
[0032] A junk feature vector distance set is obtained by calculating the junk training feature vector set and the junk test feature vector set through the Euclidean distance metric;
[0033] The mathematical expression of the Euclidean distance metric is:
[0034]
[0035] X=[X1,X2,…,X n ]
[0036] Y=[Y1,Y2,…,Y n ]
[0037] Where X is the garbage training feature vector set, Y is the garbage test feature vector set, X i is the eigenvalue of the garbage training feature vector in the i-th dimension, Y i is the eigenvalue of the garbage test feature vector in the i-th dimension, and n is the dimension of the garbage feature vector;
[0038] Determine the junk test prediction label by majority voting strategy according to the junk feature vector distance set;
[0039] Obtaining the optimal K value by a grid search method according to the predefined K value range;
[0040] A garbage distance measurement classification model is obtained according to the optimal K value.
[0041] Preferably, determining the junk test prediction label by majority voting strategy according to the junk feature vector distance set includes:
[0042] According to the garbage feature vector distance set, K garbage training feature vector samples with the smallest garbage feature vector distance are selected as the garbage nearest neighbor sample set;
[0043] A garbage test prediction label is obtained according to the garbage nearest neighbor sample set through a majority voting strategy. The majority voting strategy includes counting the number of occurrences of each garbage type for the garbage nearest neighbor sample set of each garbage test feature vector, and the garbage type label with the largest number of occurrences is the garbage test prediction label.
[0044] A garbage identification and classification system based on image sample distance measurement that applies the above-mentioned garbage identification and classification method based on image sample distance measurement includes a garbage image acquisition and processing module, a garbage sample set construction module, a garbage feature extraction module, and a garbage type identification module;
[0045] The garbage image acquisition and processing module is used to acquire garbage image data and perform image scaling, grayscale conversion, image enhancement and morphological processing to obtain garbage image data;
[0046] The garbage sample set building module is used to build a garbage image sample set according to the garbage standard image data by garbage type labeling;
[0047] The garbage feature extraction module is used to obtain a garbage feature extractor by improving the model structure according to the pre-trained ResNet model, thereby extracting a multi-dimensional feature set of garbage images and obtaining a garbage feature vector set through a global average pooling layer;
[0048] The garbage type identification module is used to construct a garbage distance measurement classification model according to the garbage feature vector set through the KNN algorithm, obtain garbage image data to be classified, perform feature extraction and distance measurement through the garbage distance measurement classification model, and output a garbage type label.
[0049] An electronic device comprises a memory, a processor and a computer program stored in the memory and executable on the processor. When the processor executes the program, the garbage identification and classification method based on image sample distance measurement is implemented.
[0050] A storage medium containing computer executable instructions, wherein the computer executable instructions are used to execute the above-mentioned garbage identification and classification method based on image sample distance measurement when executed by a computer processor.
[0051] The beneficial effects of the present invention are:
[0052] (1) Through garbage image preprocessing, garbage feature extractor construction, and garbage distance measurement classification model construction, garbage image automatic recognition and classification can be achieved, reducing manual intervention and improving garbage classification efficiency and accuracy.
[0053] (2) Gaussian blur is used to effectively reduce image noise, and histogram equalization is used to enhance image contrast, making image details clearer. Dilation and erosion operations are used to remove small holes and protrusions in the image, making the shape of the garbage more standardized and facilitating subsequent feature extraction.
[0054] (3) The garbage feature extractor is obtained by improving the pre-trained ResNet model structure. The hybrid attention mechanism is introduced to focus on the key features of the image and reduce the interference of complex background, thereby achieving more accurate feature extraction and improving the accuracy of the garbage distance metric classification model.
[0055] (4) The garbage distance metric classification model is constructed through the KNN algorithm. The feature vector distance is calculated through the Euclidean distance metric, which can effectively reflect the difference between feature vectors. The optimal K value is obtained through the grid search method, which can optimize the model performance. The predicted type label is determined through the majority voting strategy, which reduces the impact of a single incorrect match on the classification result and improves the robustness of garbage classification. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] In order to facilitate understanding by those skilled in the art, the present invention is further described below with reference to the accompanying drawings.
[0057] Figure 1 The figure is a flow chart of a garbage identification and classification method based on image sample distance measurement according to the present invention. DETAILED DESCRIPTION
[0058] In order to further explain the technical means and effects adopted by the present invention to achieve the predetermined invention purpose, the specific implementation method, structure, characteristics and effects of the present invention are described in detail below in conjunction with the accompanying drawings and preferred embodiments.
[0059] See also Figure 1 , a garbage identification and classification method based on image sample distance measurement, comprising:
[0060] S1: Obtaining garbage image data, wherein the garbage image data includes recyclable garbage images, kitchen waste images, hazardous waste images, and other garbage images;
[0061] S2: obtaining junk image standard data through junk image preprocessing according to the junk image data, and establishing a junk image sample set through junk type labeling according to the junk image standard data, wherein the junk image preprocessing includes image scaling, grayscale conversion, image enhancement, and morphological processing;
[0062] S3: constructing a garbage distance metric classification model through a pre-trained ResNet model according to the garbage image sample set;
[0063] S4: Obtaining garbage image data to be classified, and obtaining garbage type labels according to the garbage image data to be classified through the garbage distance measurement classification model.
[0064] In this embodiment, the obtaining of garbage image data includes collecting common garbage images in life through a high-definition camera at multiple angles and under different lighting conditions;
[0065] The recyclable garbage images include but are not limited to paper images, can images, and plastic bottle images;
[0066] The kitchen waste images include but are not limited to potatoes, fruit peels, peppers, bananas, tomatoes, and green vegetables;
[0067] The hazardous waste includes but is not limited to batteries and medicines;
[0068] The other garbage includes but is not limited to broken porcelain pieces, broken glass, and broken bricks.
[0069] In this embodiment, the step of obtaining the junk image standard data through junk image preprocessing according to the junk image data is specifically implemented by the following steps:
[0070] The image scaling is to uniformly scale the junk image data to 224×224 pixels;
[0071] The grayscale conversion includes converting the garbage image data into a garbage grayscale image by a weighted average method;
[0072] The image enhancement includes removing noise by Gaussian blurring according to the garbage grayscale image and enhancing contrast by histogram equalization;
[0073] The morphological processing includes normalizing the garbage shape through dilation and erosion operations.
[0074] In this embodiment, the establishing of the junk image sample set by junk type labeling according to the junk standard image data includes labeling the junk image type by using a LabelImg tool.
[0075] Specifically, labeling junk image types through the LabelImg tool includes: installing and starting the LabelImg tool, setting the automatic save mode and specifying the label save directory, loading the junk standard image folder, entering the classification label for each junk standard image and confirming it, the system will automatically save the label, and repeating this process until all images are labeled.
[0076] In this embodiment, the construction of the garbage distance measurement classification model by pre-training the ResNet model according to the garbage image sample set is specifically implemented by the following steps:
[0077] S301: Obtaining a garbage feature extractor by improving the model structure according to the pre-trained ResNet model;
[0078] The model structure improvement includes removing the last fully connected layer of the pre-trained ResNet model, retaining the convolutional layer and the pooling layer, and introducing a hybrid attention mechanism in the residual block of the pre-trained ResNet model, wherein the hybrid attention mechanism combines the channel attention and the spatial attention mechanism.
[0079] S302: extracting a multi-dimensional feature set of junk images through the junk feature extractor according to the junk image sample set;
[0080] S303: converting the garbage image multidimensional feature set into a garbage feature vector set through a global average pooling layer, wherein the global average pooling layer is a global average pooling layer added after the garbage feature extractor;
[0081] S304: constructing a garbage distance measurement classification model based on the garbage feature vector set by using a KNN algorithm;
[0082] S304-1: obtaining a garbage training feature vector set and a garbage test feature vector set by randomly dividing the garbage feature vector set, wherein the ratio of the garbage feature vector random division is 8:2;
[0083] S304-2: Obtaining a junk feature vector distance set by calculating the junk training feature vector set and the junk test feature vector set through Euclidean distance metric;
[0084] The mathematical expression of the Euclidean distance metric is:
[0085]
[0086] X=[X1,X2,…,X n ]
[0087] Y=[Y1,Y2,…,Y n ]
[0088] Where X is the garbage training feature vector set, Y is the garbage test feature vector set, X i is the eigenvalue of the garbage training feature vector in the i-th dimension, Y i is the eigenvalue of the garbage test feature vector in the i-th dimension, and n is the dimension of the garbage feature vector.
[0089] S304-21: predefine a K value range and initialize an initial K value, wherein the K value is the number of nearest neighbors considered by the KNN algorithm when predicting the garbage type;
[0090] S304-22: traverse the garbage test feature vector set, and for each garbage test feature vector, respectively calculate the garbage feature vector distance with the garbage training feature vector in the garbage training feature vector set;
[0091] S304-3: Determine a junk test prediction label by majority voting strategy according to the junk feature vector distance set;
[0092] S304-31: selecting K garbage training feature vector samples with the smallest garbage feature vector distances according to the garbage feature vector distance set as a garbage nearest neighbor sample set;
[0093] S304-32: Obtaining a junk test prediction label by majority voting strategy according to the junk nearest neighbor sample set;
[0094] Specifically, for each garbage test feature vector's garbage nearest neighbor sample set, the number of occurrences of each garbage type is counted, and the garbage type label with the largest number of occurrences is the garbage test prediction label.
[0095] S304-4: Obtaining the optimal K value by using a grid search method according to the predefined K value range;
[0096] The grid search method includes traversing all possible K values, evaluating the model performance for each K value through a cross-validation method, and selecting the K value with the best performance as the optimal K value of the garbage distance metric classification model.
[0097] In this embodiment, obtaining the garbage type label according to the garbage image data to be classified by using the garbage distance measurement classification model is specifically implemented by the following steps:
[0098] S401: obtaining a standard image of garbage to be classified by performing the garbage image preprocessing according to the garbage image data to be classified;
[0099] S402: Obtaining a garbage type label according to the standard image of the garbage to be classified through the garbage distance measurement classification model.
[0100] A garbage identification and classification system based on image sample distance measurement includes a garbage image acquisition and processing module, a garbage sample set construction module, a garbage feature extraction module, and a garbage type identification module;
[0101] The garbage image acquisition and processing module is used to acquire garbage image data and perform image scaling, grayscale conversion, image enhancement and morphological processing to obtain garbage image data;
[0102] The garbage sample set building module is used to build a garbage image sample set according to the garbage standard image data by garbage type labeling;
[0103] The garbage feature extraction module is used to obtain a garbage feature extractor by improving the model structure according to the pre-trained ResNet model, thereby extracting a multi-dimensional feature set of garbage images and obtaining a garbage feature vector set through a global average pooling layer;
[0104] The garbage type identification module is used to construct a garbage distance measurement classification model according to the garbage feature vector set through the KNN algorithm, obtain garbage image data to be classified, perform feature extraction and distance measurement through the garbage distance measurement classification model, and output a garbage type label.
[0105] The computer storage medium of the embodiment of the present invention may adopt any combination of one or more computer-readable media. The computer-readable medium may be a computer-readable signal medium or a computer-readable storage medium. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples (non-exhaustive list) of computer-readable storage media include: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this document, a computer-readable storage medium may be any tangible medium containing or storing a program that may be used by or in combination with an instruction execution system, device or device.
[0106] Computer-readable signal media may include data signals propagated in baseband or as part of a carrier wave, which carry computer-readable program code. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than a computer-readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, apparatus, or device.
[0107] The program code included on the computer readable medium can be transmitted with any appropriate medium, including but not limited to wireless, electric wire, optical cable, RF, etc., or any suitable combination of the above. The computer program code for performing the operation of the present invention can be written in one or more programming languages or their combinations, and the programming language includes object-oriented programming languages-such as Java, Smalltalk, C++, and also includes conventional procedural programming languages-such as "C" language or similar programming languages. The program code can be executed completely on the user's computer, partially on the user's computer, as an independent software package, partially on the user's computer and partially on the remote computer, or completely on the remote computer or server. In the case of a remote computer, the remote computer can be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computer (for example, using an Internet service provider to connect through the Internet).
[0108] The above description is only a preferred embodiment of the present invention and does not limit the present invention in any form. Although the present invention has been disclosed as a preferred embodiment as above, it is not used to limit the present invention. Any technical personnel in this field can make some changes or modify the technical contents disclosed above into equivalent embodiments without departing from the scope of the technical solution of the present invention. However, any simple modification, equivalent change and modification made to the above embodiments according to the technical essence of the present invention without departing from the content of the technical solution of the present invention still fall within the scope of the technical solution of the present invention.
Claims
1. A garbage identification and classification method based on image sample distance measurement, characterized in that: include: Acquire garbage image data, wherein the garbage image data includes recyclable garbage images, kitchen waste images, hazardous waste images, and other garbage images; According to the junk image data, junk image preprocessing is performed to obtain junk standard image data, and according to the junk standard image data, a junk image sample set is established by junk type labeling, wherein the junk image preprocessing includes image scaling, grayscale conversion, image enhancement, and morphological processing; Constructing a garbage distance metric classification model through a pre-trained ResNet model according to the garbage image sample set; Obtain garbage image data to be classified, and obtain garbage type labels according to the garbage image data to be classified through the garbage distance measurement classification model.
2. The garbage identification and classification method based on image sample distance measurement according to claim 1 is characterized in that: The obtaining of junk image data comprises: Using high-definition cameras to collect common garbage images in life at multiple angles and under different lighting conditions; The recyclable garbage images include paper images, can images, and plastic bottle images; The kitchen waste images include potatoes, fruit peels, peppers, bananas, tomatoes, and green vegetables; The hazardous waste includes batteries and medicines; The other garbage includes broken porcelain pieces, broken glass, and broken bricks.
3. The garbage identification and classification method based on image sample distance measurement according to claim 1 is characterized in that: The obtaining of junk standard image data by junk image preprocessing according to the junk image data comprises: The image scaling is to uniformly scale the junk image data to 224×224 pixels; The grayscale conversion includes converting the garbage image data into a garbage grayscale image by a weighted average method; The image enhancement includes removing noise by Gaussian blurring according to the garbage grayscale image and enhancing contrast by histogram equalization; The morphological processing includes normalizing the garbage shape through dilation and erosion operations.
4. The garbage identification and classification method based on image sample distance measurement according to claim 1 is characterized in that: The constructing of a garbage distance measurement classification model by a pre-trained ResNet model according to the garbage image sample set comprises: The garbage feature extractor is obtained by improving the model structure based on the pre-trained ResNet model; Extracting a multidimensional feature set of junk images through the junk feature extractor according to the junk image sample set; Converting the garbage image multidimensional feature set into a garbage feature vector set through a global average pooling layer, wherein the global average pooling layer is a global average pooling layer added after the garbage feature extractor; A garbage distance measurement classification model is constructed according to the garbage feature vector set using a KNN algorithm.
5. The garbage identification and classification method based on image sample distance measurement according to claim 4 is characterized in that: The garbage feature extractor obtained by improving the model structure according to the pre-trained ResNet model includes: The model structure improvement includes removing the last fully connected layer of the pre-trained ResNet model, retaining the convolutional layer and the pooling layer, and introducing a hybrid attention mechanism in the residual block of the pre-trained ResNet model, wherein the hybrid attention mechanism combines the channel attention and the spatial attention mechanism.
6. The garbage identification and classification method based on image sample distance measurement according to claim 4 is characterized in that: The constructing of a garbage distance measurement classification model by using a KNN algorithm according to the garbage feature vector set includes: According to the garbage feature vector set, a garbage training feature vector set and a garbage test feature vector set are obtained by randomly dividing the garbage feature vector set; A junk feature vector distance set is obtained by calculating the junk training feature vector set and the junk test feature vector set through the Euclidean distance metric; The mathematical expression of the Euclidean distance metric is: X=[X1,X2,…,X n ] Y=[Y1,Y2,…,Y n ] Where X is the garbage training feature vector set, Y is the garbage test feature vector set, X i is the eigenvalue of the garbage training feature vector in the i-th dimension, Y i is the eigenvalue of the garbage test feature vector in the i-th dimension, and n is the dimension of the garbage feature vector; Determine the junk test prediction label by majority voting strategy according to the junk feature vector distance set; Obtaining the optimal K value by a grid search method according to the predefined K value range; A garbage distance measurement classification model is obtained according to the optimal K value.
7. The garbage identification and classification method based on image sample distance measurement according to claim 6 is characterized in that: Determining the junk test prediction label by majority voting strategy according to the junk feature vector distance set includes: According to the garbage feature vector distance set, K garbage training feature vector samples with the smallest garbage feature vector distance are selected as the garbage nearest neighbor sample set; A garbage test prediction label is obtained according to the garbage nearest neighbor sample set through a majority voting strategy. The majority voting strategy includes counting the number of occurrences of each garbage type for the garbage nearest neighbor sample set of each garbage test feature vector, and the garbage type label with the largest number of occurrences is the garbage test prediction label.
8. A garbage identification and classification system based on image sample distance measurement using the garbage identification and classification method based on image sample distance measurement according to claim 1, comprising a garbage image acquisition and processing module, a garbage sample set construction module, a garbage feature extraction module, and a garbage type identification module, characterized in that: The garbage image acquisition and processing module is used to acquire garbage image data and perform image scaling, grayscale conversion, image enhancement and morphological processing to obtain garbage image data; The garbage sample set building module is used to build a garbage image sample set according to the garbage standard image data by garbage type labeling; The garbage feature extraction module is used to obtain a garbage feature extractor by improving the model structure according to the pre-trained ResNet model, thereby extracting a multi-dimensional feature set of garbage images and obtaining a garbage feature vector set through a global average pooling layer; The garbage type identification module is used to construct a garbage distance measurement classification model according to the garbage feature vector set through the KNN algorithm, obtain garbage image data to be classified, perform feature extraction and distance measurement through the garbage distance measurement classification model, and output a garbage type label.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the garbage identification and classification method based on image sample distance measurement as described in any one of claims 1 to 7 is implemented.
10. A storage medium containing computer executable instructions, characterized in that: The computer executable instructions are used to execute the garbage identification and classification method based on image sample distance measurement as described in any one of claims 1 to 7 when executed by a computer processor.