Frontiers in Computing and Intelligent Systems ISSN: 2832-6024 | Vol. 9, No. 2, 2024 53 High‐speed Image Process in Pharmaceutical Bottle Production Basing OpenCV Xinfeng Chen *, Zhoujing Wu Wenzhou Polytechnic, Wenzhou Zhejiang, 325000, China * Corresponding author: Xinfeng Chen Abstract: In pharmaceutical production, the quality of medicine bottles is crucial for ensuring drug safety and efficacy. Traditional manual visual inspection methods are inefficient, costly, and prone to errors. To address these issues, this paper proposes a real-time bottle defect detection system based on OpenCV and Python, which uses computer vision technology for automatic defect detection, thereby improving efficiency and accuracy. The paper discusses the background of bottle quality inspection, the problems with traditional methods, and the advantages of computer vision technology. It details the system’s design, implementation, and algorithm optimization. Experimental results demonstrate that the system performs well in terms of real-time capability and accuracy, and suggestions for improvements are provided. Overall, this paper offers a comprehensive reference for the design and application of a real-time bottle defect detection system based on OpenCV and Python. Keywords: OpenCV; Image Process; Machine Vision; Prediction. 1. Introduction In the pharmaceutical industry, medicine bottles are a crucial component of drug packaging, and their quality directly impacts the safety and efficacy of the drugs. However, traditional bottle quality inspection methods often rely on manual visual checks, which are inefficient, subjective, and error-prone, failing to meet the demands of large-scale production. Therefore, there is an urgent need and significant importance in developing a real-time bottle defect detection system based on computer vision and image processing technology. Image processing and computer vision technology refers to a technique that uses computers to process and analyze images. It encompasses various aspects such as image acquisition, preprocessing, feature extraction, object detection, and classification. In industrial production, image processing and computer vision technology are widely used in fields such as product quality inspection, defect detection, and automated recognition. Basically, OpenCV is an open-source computer vision library that offers a wide range of image processing and computer vision algorithms, including image processing, feature detection, object tracking, and machine learning functionalities. It supports multiple programming languages, such as C++ and Python, and is widely used in fields such as image processing, medical imaging analysis, robotics vision, and security surveillance. Python, as a concise and powerful programming language, also has extensive applications in the field of image processing. With Python’s image processing libraries (such as PIL, Scikit-image, etc.) and tools like OpenCV, it is possible to perform image reading, processing, analysis, and visualization. Additionally, Python features a rich array of scientific computing libraries and machine learning libraries, enabling the integration of image processing techniques for more complex applications. Numerous studies have focused on developing computer vision-based defect detection systems across various fields, including agriculture [1], art works, games and industry [2]. However, research on real-time defect detection for medicine bottles is relatively scarce, with existing methods often limited to static image analysis and lacking real-time capabilities and accuracy. Therefore, it is essential to conduct research on real-time defect detection systems for medicine bottles to fill this gap and enhance quality control in pharmaceutical production lines. 2. Problem Identification Bottle defect detection technology refers to the use of image processing and computer vision techniques to assess the quality of medicine bottles, identifying and classifying defects. Common defects in medicine bottles include body damage, foreign objects, and missing caps. Through suitable algorithms and systems, automatic detection and recognition of bottle defects can be achieved, improving production efficiency and product quality. 3. Research Methodology A real-time bottle defect detection system based on OpenCV and Python combines image processing and computer vision technologies [3]. It involves real-time acquisition of bottle images, followed by preprocessing, feature extraction, and classification, ultimately enabling accurate detection and analysis of defects. This system is characterized by its real-time performance and efficiency and can be widely applied in quality control on pharmaceutical production lines, enhancing production efficiency and product quality. (1) Environment Setup To ensure that the system operates effectively in a real production environment, the experimental environment should closely mimic the conditions of the production line. The setup of the experimental environment includes: Hardware Environment: Use high-performance computers equipped with multi-core CPUs and appropriate GPUs to meet real-time processing requirements. Software Environment: Utilize Python as the programming language and OpenCV as the image processing library. 54 Additionally, use Yolov8 or PyTorch for training deep learning models. Camera Setup: Install the camera at appropriate locations on the production line to ensure clear capture of bottle images. After setting up the experimental environment, system parameters need to be fine-tuned. Key parameters for tuning include: Threshold Setting: Adjust the threshold for defect detection based on the samples in the dataset. Detection Speed: Improve detection speed while maintaining accuracy. Model Training: Choose the appropriate model architecture based on the size and complexity of the dataset and adjust parameters such as the learning rate. (2) System Design The architecture design of a real-time bottle defect detection system includes both hardware and software components. The hardware part primarily consists of cameras, image acquisition devices, and computers. Figure 1. system framework The software part primarily includes image processing algorithms, defect detection algorithms, and user interfaces. The key to the system architecture design is to ensure the real- time capability of image acquisition, the accuracy of image processing, and the stability of the system. The image processing workflow design is the core component of a real-time bottle defect detection system. It mainly includes four steps: image acquisition, preprocessing, feature extraction, and defect detection. Image acquisition captures real-time images of the bottles using cameras. Preprocessing involves operations such as noise reduction, grayscale conversion, and edge detection. Feature extraction involves extracting features from the image, such as color, texture, and shape. Defect detection uses a trained model to classify and recognize defects based on the extracted features. The implementation of defect detection algorithms is a key part of a real-time bottle defect detection system. Commonly used algorithms include Support Vector Machines (SVM) [4] and Convolutional Neural Networks (CNN). In Python, these algorithms can be implemented using OpenCV and other machine learning libraries. First, image features are extracted and converted into a format suitable for training. Then, a dataset is used for training the model. Finally, the trained model is applied to actual bottle defect detection. (3) System Performance Evaluation Method system performance evaluation is a crucial component of a real-time bottle defect detection system and primarily includes metrics such as accuracy, recall, and F1 score. Accuracy refers to the ratio of the number of defects correctly detected by the system to the total number of defects detected. Recall is the ratio of the number of defects correctly detected by the system to the total number of actual defects present. The F1 score is the harmonic mean of precision and recall. By evaluating these metrics, one can comprehensively assess the system’s performance and optimize and improve the system based on the evaluation results. (4) Implementation To test the real-time bottle defect detection system based on OpenCV and Python, it is necessary to collect a large number of image data featuring both normal and defective bottles. This data is collected from the production lines of pharmaceutical companies and includes the following types of defects: Surface scratches: Visible scratches or wear on the surface of the bottles. Bottom defects: Scratches on the bottle bottom, clamping issues, and broken bottles. preprocessing is required to ensure that all images have a consistent format and quality. The preprocessing steps include: Image resizing: Adjusting all images to a uniform size to facilitate processing. Grayscale conversion: Converting color images to grayscale to reduce computational complexity. Noise removal: Using Gaussian filtering or other methods to remove noise from the images. These preprocessing steps ensure data consistency and reduce errors during model training and detection. Using QT as a UI design tools and convert ui file into python format file is a main method in python computer vision method. An official module Pyside provide powerful function in this project. PySide is a Python binding for the Qt application framework, providing access to the functionality of the Qt library and simplifying the development of graphical user interface (GUI) applications. Key features of PySide include: Cross-Platform Support: PySide supports multiple operating systems, including Windows, macOS, and Linux, allowing developers to write code once and run it on various platforms. Rich Set of Widgets and Features: PySide offers the full range of features from the Qt library, including various UI widgets (buttons, labels, text boxes, etc.), layout management, drawing capabilities, and database integration. Pythonic API: Compared to Qt’s C++ API, PySide provides a Python-style API, making it simpler and more intuitive to use. Open Source: PySide is open source and compatible with Qt’s commercial license, making it suitable for both open source and commercial projects. Qt Designer Integration: PySide integrates with the Qt Designer tool, which allows for graphical UI design and the generation of corresponding Python code. In Python, threads are used to achieve concurrency, allowing a program to perform multiple operations simultaneously. Thus, Move image acquisition, image processing and result processing into thread and make UI as a main thread in the project. 4. Evaluation of Results After setting up the experimental environment and tuning the parameters, a series of experiments were conducted to validate the system’s performance. The experimental results 55 and analysis include the following aspects: Accuracy: The system exhibits high overall accuracy and can effectively detect various types of bottle defects. Recall: The system’s recall rate is over 90%, indicating that it can identify most defects. Precision: The system has a low false positive rate, demonstrating good accuracy. Processing Speed: The system operates in real-time and meets the requirements of the production line. Based on these experimental results, it can be concluded that the real-time bottle defect detection system based on OpenCV and Python performs well in terms of accuracy, recall, precision, and processing speed, making it suitable for practical production environments. Additionally, analyzing the experimental results can help identify areas for system improvement, such as further reducing the false positive rate or enhancing processing speed. 5. Conclusion This paper provides an in-depth study and analysis of a real-time bottle defect detection system based on OpenCV and Python, with detailed exploration of system design, experimental validation, and performance evaluation. The experimental results and performance assessments confirm the system’s effectiveness and feasibility in the field of bottle defect detection. In future research, further optimization of the system’s algorithms and models can be pursued to enhance detection accuracy and real-time performance. Additionally, exploring the application of new technologies such as deep learning in bottle defect detection may further improve the system’s performance and effectiveness. Moreover, expanding the system’s application to other industrial sectors, such as food packaging and automotive parts, could enable broader technological applications and industrial growth. Overall, the real-time bottle defect detection system based on OpenCV and Python has significant potential and prospects for industrial applications. With ongoing optimization, improvements, and integration of new technologies, this system is expected to bring greater convenience and benefits to industrial production, driving progress and development in related industries. 6. Future Work Despite the achievements of the real-time bottle defect detection system, there is still room for optimization and improvement. Future efforts can focus on the following aspects: Algorithm Optimization: Improve detection algorithms and model architectures to enhance the system’s accuracy and robustness, and reduce false positive rates. Real-time Performance Enhancement: Optimize algorithms and engineering implementations to increase processing speed and real-time performance, meeting the demands of production lines. Data Augmentation: Use data augmentation techniques to expand the dataset and improve the system’s adaptability to different scenarios and defect types. Hardware Optimization: Employ advanced hardware devices, such as specialized image processing chips or FPGAs, to boost the system’s processing performance and efficiency. User Interface Improvement: Refine the system’s user interface and interaction methods to make them more user- friendly and accessible, thereby lowering the operational threshold. Although the real-time bottle defect detection system has achieved certain results, there is still room for optimization and improvement. Future system optimization and improvements can be approached from the following aspects: Algorithm Optimization: Improve detection accuracy and robustness by refining detection algorithms and model architectures and reduce false positives. Real-Time Optimization: Enhance system processing speed and real-time performance by optimizing algorithms and engineering implementations to meet production line requirements. Data Augmentation: Expand the dataset through data augmentation techniques to increase the system’s adaptability to different scenarios and defect types. Hardware Optimization: Utilize more advanced hardware devices, such as dedicated image processing chips or FPGAs, to improve system performance and efficiency. User Interface Optimization: Refine the system’s user interface and interaction methods to make them more user- friendly and accessible, thereby lowering the operational threshold. Through continuous optimization and improvement, the real-time bottle defect detection system will better serve industrial production and continually enhance its performance and value. Through continuous optimization and improvement, the real-time bottle defect detection system will better serve industrial production and enhance its performance and value. Acknowledgments This research was conducted in Zhejiang Denuo CO., LTD, funded by department of Education of Zhejiang, Wenzhou Polytechnic (funding No. Y202250639). References [1] Xinyue Lv, Xiaolong Zhang, Hairong Gao, Tingting He, Zhiyuan Lv, Lili Zhangzhong, When crops meet machine vision: A review and development framework for a low-cost nondestructive online monitoring technology in agricultural production, Agriculture Communications,Vol.2(1), 2024. [2] Jill Walker Rettberg, Linda Kronman, Ragnhild Solberg, Marianne Gunderson, Stein Magne Bjørklund, Linn Heidi Stokkedal, Kurdin Jacob, Gabriele de Seta, Annette Markham,Representations of machine vision technologies in artworks, games and narratives: A dataset, Data in Brief, Vol.42, 2022. [3] Zhiwen Tu, Xianan Qin, A Python-OpenCV based software for processing single-bacterium tracking microscopy videos, Biophysical Journal, Vol.122(3), 2023, P.140a-141a. [4] Hosein Nouri-Ahmadabadi, Mahmoud Omid, Seyed Saeid Mohtasebi, Mahmoud Soltani Firouz, Design, development and evaluation of an online grading system for peeled pistachios equipped with machine vision technology and support vector machine, Information Processing in Agriculture, Vol.4(4), 2017, P. 333-341.