Tobacco worm image recognition and counting method based on YOLOv5 and micro-service architecture

The tobacco insect image recognition and counting method based on YOLOv5 and microservice architecture solves the problems of low efficiency and poor accuracy of traditional tobacco insect recognition, achieves efficient and accurate tobacco insect image recognition and counting, adapts to complex environments and supports multi-task expansion.

CN120689721APending Publication Date: 2025-09-23YUNNAN ZIJIN SCI & TRADE CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510772188.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-06-11
Publication Date
2025-09-23

AI Technical Summary

Technical Problem

Traditional tobacco insect identification relies on manual observation, which is inefficient and easily affected by subjective factors. Improper cleaning of tobacco insect traps increases detection complexity. The existing detection system has poor detection accuracy for small targets and complex backgrounds.

Method used

Using YOLOv5 and microservice architecture, high-resolution images taken by PDA are used as training data for image processing and enhancement. The YOLOv5 model is used for training and detecting tobacco insect images in the microservice system. The results are counted and packaged to achieve efficient recognition and counting.

Benefits of technology

It achieves efficient and accurate tobacco insect image recognition and counting in complex environments, has good robustness and adaptability, supports multi-task expansion and continuous learning, and improves detection accuracy and system stability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120689721A_ABST
    Figure CN120689721A_ABST
Patent Text Reader

Abstract

The invention discloses a tobacco insect image recognition and counting method based on YOLOv5 and a micro-service architecture, and the method comprises the following steps: S1, image acquisition, S2, image processing and data enhancement, S3, model training, S4, to-be-detected image input, S5, detection result writing into a text file, S6, statistics, and S7, packaging a statistical result in a system, and carrying out the data rendering of a front end. And S8, returning a statistical result. The method has the advantages that through fusion of deep learning algorithm optimization and distributed architecture design, efficient intelligent analysis service for tobacco insects is constructed. The system adopts a three-stage technical framework: firstly, performing weight training on a model level by using YOLOv5, reconstructing a backbone architecture through a lightweight feature extraction network, and enhancing a small target feature expression ability in combination with a cross-level attention mechanism; secondly, building a FastAPI-based micro-service, carrying out asynchronous processing on tasks, and realizing request distribution and dynamic scheduling of computing resources; and finally, realizing elastic expansion of algorithm service through a containerized deployment scheme.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of pest and disease image recognition, and in particular to a tobacco insect image recognition and counting method based on YOLOv5 and a microservice architecture. Background Art

[0002] Traditional tobacco beetle identification relies on manual observation, which is inefficient and easily influenced by subjective factors, leading to statistical deviations. Traditional detection and identification systems also perform poorly for small targets like tobacco beetles in complex backgrounds. In particular, tobacco beetle traps, in addition to detecting and capturing various tobacco beetles, may also become attached to other foreign matter. For example, tobacco factories may clean and reuse tobacco beetle traps to save costs. If the tobacco beetle traps are not cleaned properly, the remaining parts of the tobacco beetles will remain on them and overlap with newly captured tobacco beetles, increasing the complexity of detection and posing a challenge to the accuracy of the detection model. Summary of the Invention

[0003] The technical problem to be solved by the present invention is to provide a tobacco insect image recognition and counting method based on YOLOv5 and microservice architecture.

[0004] In order to solve the above technical problems, the technical solution of the present invention is:

[0005] A tobacco insect image recognition and counting method based on YOLOv5 and microservice architecture includes the following steps:

[0006] S1. Image acquisition: Use a PDA to take high-resolution pictures of tobacco worms as training data sets;

[0007] S2. Image processing and image enhancement: During the training process, the system uses a high-resolution image dataset taken by a PDA for training. In order to ensure fairness in model training, verification, and testing, the entire dataset is divided into a ratio of 7:2:1 for model training: for parameter adjustment and evaluation: for final testing. In order to enhance the robustness of the YOLOv5 model, the model uses preprocessing methods such as flipping, rotation, cropping, and color jittering. During cropping, in order to reduce the inaccuracy of target detection caused by noise outside the border, the border is identified using the border recognition function of YOLOv5, and then the noise area outside the border is removed using Python tools. For image enhancement, YOLOv5's mosica enhancement is used. Specifically, it randomly uses 4 pictures for random scaling, cropping, and then splicing them in a random arrangement. This greatly enriches the dataset. Image processing is necessary, especially for the detection of small targets such as insects. It enhances the quality of image input and enables subsequent models to better capture key information during training.

[0008] S3, model training: Use the high-resolution tobacco insect data images processed in S2 as the data source for the YOLOv5 model and use YOLOv5 for training;

[0009] S4. Input the tobacco insect image to be detected into the microservice system for model detection;

[0010] S5. Put the output of the model into a text file, where each line stores the data of a detection target, and the data contains relevant data of the detection target;

[0011] S6. Implement statistical counting. In the text file saved in step S5, by traversing all the test result text files, count the number of occurrences of the first number (category number) in each line, and finally convert the category number into the corresponding category name through the customized category name list, and finally obtain the number of detected categories for each category name;

[0012] S7. Encapsulate the statistical results in the system: encapsulate the tobacco insect names and corresponding quantity information obtained in step S6, as well as the identified images, into a class. This is to uniformly return the results and facilitate data rendering on the front end.

[0013] S8. Return statistical results: In the microservice system, the statistical results encapsulated in step S7 are returned to the calling host that calls this detection interface.

[0014] Preferably, in step S2, 800 to 1200 high-resolution image data containing tobacco insects are selected from the collected high-resolution images.

[0015] Preferably, in step S5, the data includes the coordinates of the recognition box, the confidence level, and the category of the detection target.

[0016] Preferably, in step S8, the microservice system is a lightweight system built based on YOLOv5 and microservice architecture, which is simple and efficient. By registering its own system service in the microservice system, when the microservice system is accessed, after executing step S4, the microservice system can return the statistical results after detection.

[0017] The above technical solution has the following advantages:

[0018] This invention leverages the versatility of YOLOv5 in target recognition and statistics tasks, embodied in the following: A single-stage detection architecture achieves effective coordination of speed and accuracy while balancing efficiency and accuracy. Millisecond-level response is achieved on standard computing devices, and a multi-scale feature fusion mechanism adapts to targets of varying sizes. The demand data enhancement strategy incorporates built-in scene generalization capabilities to effectively address complex environmental interference such as illumination changes and perspective shifts. This feature allows the model to adapt to the unique environment of tobacco insect traps. In terms of functional scalability, its infrastructure provides a multi-task extension interface that can simultaneously implement complex functions such as target classification, location tracking, and quantity statistics. The open model framework supports continuous learning and optimization, adapting to emerging target types and dynamic scene changes. Therefore, this invention can effectively process and identify tobacco insect images, ensuring high accuracy in their recognition. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] Figure 1 is a flow chart of the present invention;

[0020] Figure 2 This is a simplified structural diagram of the C3 module;

[0021] Figure 3 This is a simplified structural diagram of the SPPF module;

[0022] Figure 4 It is an interface program for tobacco insect detection;

[0023] Figure 5 Flowchart for tobacco insect image recognition and counting;

[0024] Figure 6 An image of one of the tobacco worm images rendered and labeled;

[0025] Figure 7 Data rendering and labeling of another tobacco insect image;

[0026] Figure 8 Screenshot of the statistical results interface. DETAILED DESCRIPTION

[0027] The following is a further description of specific embodiments of the present invention in conjunction with the accompanying drawings. It should be noted that the description of these embodiments is intended to facilitate understanding of the present invention and does not constitute a limitation of the present invention. In addition, the technical features involved in the various embodiments of the present invention described below may be combined with each other as long as they do not conflict with each other.

[0028] Example 1

[0029] A tobacco insect image recognition and counting method based on YOLOv5 and microservice architecture, such as Figure 1 As shown, the following steps are included:

[0030] S1. Image acquisition: High-resolution images of tobacco insects taken by PDA (a handheld device with computing and data processing capabilities) are used as training datasets.

[0031] S2. Image processing and image enhancement: During the training process, the system uses a high-resolution image dataset taken by a PDA for training. In order to ensure fairness in model training, verification, and testing, the entire dataset is divided into 7 (for model training): 2 (for parameter adjustment and evaluation): 1 (for final testing) in proportion. In order to enhance the robustness of the YOLOv5 model, the model uses preprocessing methods such as flipping, rotation, cropping, and color jittering. During cropping, in order to reduce the inaccuracy of target detection caused by noise outside the border, the border is identified using the border recognition function of YOLOv5, and then the noise area outside the border is removed using the python tool. For image enhancement, the mosica enhancement of YOLOv5 is used. Specifically, it randomly uses 4 pictures for random scaling, cropping, and then splicing them in a random arrangement. This greatly enriches the dataset. Image processing is necessary, especially for the detection of small targets such as insects. It enhances the quality of image input and enables the subsequent model to better capture key information during training.

[0032] S3, model training: Use the high-resolution tobacco insect data images processed in S2 as the data source for the YOLOv5 model and use YOLOv5 for training.

[0033] S4. Input the tobacco insect picture to be detected into the microservice system for model detection

[0034] S5. Put the output of the model into a text file, where each line stores the data of a detected target. The data includes the coordinates of the recognition box of the detected target, the confidence level, the category, etc.

[0035] S6. Implement statistical counting. In the text file saved in S5, traverse all the detection result txt files, count the number of occurrences of the first number (category number) in each line, and finally convert the category number into the corresponding category name through the customized category name list, and finally obtain the number detected by each category name.

[0036] S7. Encapsulate statistical results in the system: Encapsulate the tobacco insect names and corresponding quantity information obtained in S6, including the recognized images, into a class. This is to uniformly return results and facilitate data rendering on the front end.

[0037] S8. Return statistical results: In this microservice system, the statistical results encapsulated in S6 can be returned to the calling host that calls this detection interface.

[0038] YOLOv5 is an efficient object detection model developed by Ultralytics and is part of the YOLO (You Only Look Once) family. It maintains high accuracy while having a fast inference speed, making it well-suited for real-time object detection tasks.

[0039] Example 2

[0040] A tobacco insect image recognition and counting method based on YOLOv5 and microservice architecture, with the following specific applications:

[0041] In step S1, a PDA is used to photograph tobacco insect traps containing tobacco insects as training data. The images can be taken from multiple angles and with different lighting conditions to ensure richer images and improve model robustness.

[0042] In step S2, the high-resolution image dataset taken with a PDA is divided into training, validation, and test sets in a ratio of 7:2:1. The total tobacco insect dataset contains 1,081 high-resolution images, of which 758 are used for training, 215 for testing, and 108 for validation. This ensures that the model learns the main distribution characteristics of tobacco insect images, stably evaluates the model iteration effect, and truly reflects the performance of the model on unknown data. In addition, before training the model, the YOLOv5 model needs to be cropped to a square size to facilitate unified model processing;

[0043] In step S3, the high-resolution tobacco insect data images processed in S2 are used as the data source for the YOLOv5 model and trained using YOLOv5. The overall architecture of YOLOv5 mainly consists of three parts: Backbone, Neck, and Head. The main modules in Backbone are Conv module, C3 module, and SPPF module. The C3 module is shown in the attached figure. Figure 2 As shown in the figure, its main function is to increase the depth and sensitivity of the network and improve the ability of feature extraction. Figure 3As shown in the figure, its purpose is to achieve spatial invariance and position invariance of input data in order to improve the recognition ability of neural networks. The Neck feature pyramid combines feature maps of different levels to generate feature maps with multi-scale information to improve the accuracy of target detection. The Head module is mainly responsible for multi-scale target detection on the feature maps extracted by the backbone network. In addition, in YOLOv5, it is based on the one-stage method in the target detection algorithm. Its main idea is to divide the entire image into several grids, and each grid predicts the type and position information of the object in the grid. Then, the target frame is screened according to the IoU value between the predicted frame and the true frame, and finally the category and position information of the predicted frame are output. Compared with other target detection algorithms, YOLOv5 is faster while ensuring high accuracy, especially in GPU environment, it can achieve real-time detection. In particular, in terms of accuracy, YOLOv5 uses multi-scale prediction and CIoUloss loss calculation. The formula is as follows:

[0044]

[0045] Where v is the normalized difference between the length and width ratio of the predicted box and the real box, The value of the α component ranges from 0 to π / 4 and can be converted to a value between 0 and 1 by multiplying it by 4 / π. α is a balancing factor between the loss caused by the aspect ratio and the loss caused by the IoU component. IoU considers the difference between the predicted and ground-truth boxes in more dimensions, improving the accuracy of tobacco insect detection.

[0046] In step S4, the tobacco insect images to be detected are input into the microservice system. The system will save the images locally and then perform detection statistics. In this way, the images used for detection can also be saved and subsequently used to train the model.

[0047] S5. Put the output of the model into a text file, where each line stores the data of a detected target. The data includes the coordinates of the recognition box of the detected target, the confidence level, the category, etc.

[0048] S6. Implement statistical counting. In the text file saved in S5, traverse all the detection result text files (txt format), count the number of occurrences of the first number (category number) in each line, and finally convert the category number into the corresponding category name through the customized category name list, and finally obtain the number detected by each category name.

[0049] S7. Encapsulate the statistical results in the system: encapsulate the tobacco insect names and corresponding quantity information obtained in S6, including the recognized images, into a class. This is to uniformly return the results and facilitate data rendering on the front end.

[0050] S8. In the returned statistical results: the statistical number and the detected images are included. A table-based data rendering is also done on the front end. For specific image annotation, please refer to the attached manual. Figure 6 、 Figure 7 , the statistical results interface can be found in the manual Figure 8 In this microservice system, the FastAPI framework is used to encapsulate the trained model in S3 into an interface and deployed to the server using Uvicorn. Specifically, the tobacco insect detection interface is as shown in the attached manual. Figure 4 As shown, in order to explain in detail, the functional implementation of this interface can be divided into multiple operators, each of which implements a specific business, and finally forms a directed acyclic graph that can efficiently realize the tobacco insect image recognition and counting function. The specific process can be seen in the appendix of the manual. Figure 5 As shown, the system has also added a global exception handling block. Specifically, the handler registered with the @app.exception_handler decorator builds a pipeline for converting HTTP exceptions to response objects. This design allows business code to focus on core logic while externalizing error management capabilities to a unified governance layer. To ensure system stability, heartbeat monitoring is added to monitor system status in real time, facilitating adjustments based on exceptions.

[0051] This paper combines the target detection model with the FastAPI framework to make model-based services more efficient. By adopting the ASGI (Asynchronous Server Gateway Interface) protocol supported by FastAPI, it achieves asynchronous I / O processing capabilities and combines it with the event loop mechanism of the Uvicorn server to effectively improve the overall processing performance of the system.

[0052] This paper leverages the following technical features of FastAPI: its asynchronous, non-blocking processing mechanism is based on the ASGI protocol and supports asynchronous request processing via coroutines (see related content on the Uvicorn server). A single instance can handle 3000+ queries per second, significantly outperforming traditional synchronous frameworks like Flask (with performance improvements of approximately 5-10 times). Uvicorn supports rapid deployment and seamlessly adapts to advanced features such as asynchronous routing and dependency injection.

[0053] In addition, in terms of global exception handling, the tobacco insect image recognition and counting system built based on YOLOv5 and microservice architecture in the present invention implements a hierarchical exception processing mechanism, including classified management of business exceptions and system exceptions, and unified response format, thereby significantly improving the robustness and maintainability of the API interface.

[0054] This paper leverages the versatility of YOLOv5 in target recognition and statistical tasks. Specifically, the model employs a single-stage detection architecture, ensuring both accuracy and processing efficiency, enabling millisecond-level response speeds on standard computing devices. By incorporating a multi-scale feature fusion mechanism, it effectively adapts to targets of varying sizes. Furthermore, its built-in data augmentation strategy exhibits excellent scene generalization capabilities, effectively addressing complex environmental factors such as illumination variations and perspective shifts, thereby improving the model's adaptability and stability in the specific application scenario of tobacco insect traps.

[0055] In terms of functional expansion, YOLOv5's infrastructure supports multi-task collaborative processing, enabling simultaneous implementation of complex functions such as target classification, location tracking, and population counting through a unified model framework. Furthermore, this model framework is highly open, supporting continuous learning and model optimization based on new sample data, thereby adapting to new target types and dynamically changing application scenarios. Based on these technical features, the present invention can efficiently and accurately perform tobacco insect image recognition tasks, significantly improving the accuracy and robustness of image recognition to meet practical application requirements.

[0056] The embodiments of the present invention are described in detail above with reference to the accompanying drawings, but the present invention is not limited to the described embodiments. It is apparent to those skilled in the art that various changes, modifications, substitutions, and variations to these embodiments may be made without departing from the principles and spirit of the present invention, and these changes and modifications still fall within the scope of protection of the present invention.

Claims

1. A tobacco insect image recognition and counting method based on YOLOv5 and microservice architecture, characterized by: The following steps are involved: S1. Image acquisition: Use a PDA to take high-resolution pictures of tobacco worms as training data sets; S2. Image processing and image enhancement: During training, the system uses a high-resolution image dataset captured by a PDA. To ensure fairness in model training, verification, and testing, the entire dataset is divided into a ratio of 7:2:1 for model training: for parameter adjustment and evaluation: for final testing. To enhance the robustness of the YOLOv5 model, the model uses preprocessing methods such as flipping, rotation, cropping, and color jittering. During cropping, to reduce the impact of noise outside the border on target detection accuracy, the border is identified using YOLOv5's border recognition function. The noise area outside the border is then removed using Python tools. For image enhancement, YOLOv5's Mosica enhancement is used. S3, model training: Use the high-resolution tobacco insect data images processed in S2 as the data source for the YOLOv5 model and use YOLOv5 for training; S4. Input the tobacco insect image to be detected into the microservice system for model detection; S5. Put the output of the model into a text file, where each line stores the data of a detection target, and the data contains relevant data of the detection target; S6. Implement statistical counting. In the text file saved in step S5, by traversing all the detection result text files, count the number of occurrences of the first number in each line, and finally convert the category number into the corresponding category name through the customized category name list, and finally obtain the number of detected categories for each category name; S7. Encapsulate the statistical results in the system: encapsulate the tobacco insect names and corresponding quantity information obtained in step S6, as well as the identified images, into a class. This is to uniformly return the results and facilitate data rendering on the front end. S8. Return statistical results: In the microservice system, the statistical results encapsulated in step S7 are returned to the calling host that calls this detection interface.

2. The tobacco insect image recognition and counting method based on YOLOv5 and microservice architecture according to claim 1 is characterized in that: In step S2, 800 to 1200 high-resolution image data containing tobacco insects are selected from the collected high-resolution images.

3. The tobacco insect image recognition and counting method based on YOLOv5 and microservice architecture according to claim 1 is characterized in that: In step S5, the data includes the coordinates of the recognition box, the confidence level, and the category of the detected target.

4. The tobacco insect image recognition and counting method based on YOLOv5 and microservice architecture according to claim 1 is characterized in that: In step S8, the microservice system is a lightweight system built based on YOLOv5 and microservice architecture. It is simple and efficient. By registering its own system service in the microservice system, when the microservice system is accessed, after executing step S4, the microservice system can return the statistical results after detection.