
1. Task Description
Using machine vision to identify workpieces, the Raspberry Pi serves as the host computer, sending different commands to the lower computer based on different recognition results, with the following process:

2. Dataset Acquisition
This is a part sent by the teacher, so there is definitely no directly usable model online, unlike masks detection which has ready-made datasets, so we need to acquire the dataset ourselves.
Since we only have two parts, if we want to acquire a large number of images, we can generate new photos by rotating; I set it to generate a new photo every 10 degrees of rotation, so for each photo taken, we can generate 36 new ones, expanding the source of the dataset.
Then we use labelImg to annotate the images, thus obtaining a large dataset and corresponding label txt files. I allocated the images in a ratio of test:train:val = 1:8:2. Now our dataset is ready.

3. Yolov5 Model Training
Since the creator of Yolov5 has open-sourced the model code, we can use it directly; we just need to modify the paths for train and val to our own, as well as the number of categories. Here, I divided it into two categories, so it is 2, and the label names correspond to the colors, blue and red.
I chose the Yolov5s model, which is lighter and faster in detection, and set the number of categories to 2. After preparing, we start training. Since I trained directly on the CPU, the training took about 5.7 hours; using a GPU would significantly speed it up.

4. Model Training Results
From the figure, we can see that both the accuracy and recall rates are close to 100%, with an average mAP of 0.995, indicating that the model’s recognition effect is very good and can complete our workpiece recognition task effectively.

5. Detection Effect

From the video, we can see that the red and blue workpieces are recognized; the red one is labeled as red, and the blue one is labeled as blue, with the numbers representing accuracy, which is basically above 0.8, a relatively ideal result.
Frame-by-frame detection of the video shows that the recognition effect is very good for each frame. This is the result of the model running on the notebook; thus, our Yolov5 module comes to an end. The next issue is how to make it run perfectly on the Raspberry Pi.
6. Raspberry Pi Environment Setup
This is my first time working with Raspberry Pi and my first time using Linux, so I encountered many issues; sometimes it would take an entire day just to solve one problem.
I documented this learning process, along with the various difficulties I encountered, and posted it on CSDN, hoping it can help you. The link is as follows: Yolov5 Porting Raspberry Pi 4B Problem Summary Raspberry Pi 4B Learning Notes
The environment I chose is the 64-bit operating system of Raspberry Pi, which makes it very convenient to download OpenCV. After a long period of exploration, I found that it is very difficult to install Pytorch on a 32-bit operating system; even if successfully installed, it cannot run.
So I chose the 64-bit operating system of Raspberry Pi and installed OpenCV and Pytorch.

7. Yolov5 Porting to Raspberry Pi
After preparing the environment, we need to port Yolov5. First, connect to the Raspberry Pi through VNC Viewer so that we can control the Raspberry Pi from the computer. If there are issues, refer to my previous two blogs.

Transfer the trained pt module to the Raspberry Pi using the file transfer feature at the top. Use the Linux cd command to enter the folder where Yolo is located, then input the run command, python3 to run the detect file, using the weight file we just transferred, best.pt, with source as 0 to call the CSI camera installed on the Raspberry Pi. After that, press Enter to run.

8. Raspberry Pi Detection Effect

Let’s take a look at the detection effect: on the left is the detection environment, with the Raspberry Pi equipped with a CSI camera to capture images; the image below is one taken during detection.
We can see that the red part is recognized and labeled as red, while the blue part is also recognized and labeled as blue. We can use these two labels to send different commands to the lower computer to complete specified tasks.
Conclusion
The project mainly involves using Yolov5 for object detection, then using Raspberry Pi as the host computer to port the model for recognition and control of the lower computer’s operation.
Throughout the process, countless problems were encountered, both hardware and software, often taking a day to solve a single issue. This process was painful, but fortunately, the results were satisfying. During detection, I also found that the recognition speed of the Yolov5 model is indeed a bit slow; even the notebook struggles, and it lags on the Raspberry Pi, taking 2-3 seconds to detect an image.
The future optimization directions are twofold: one is to optimize the model by adopting the YOLO-Lite model, and the other is to optimize the method using OpenCV template matching for recognition, which should speed things up significantly without involving deep learning.
Of course, this is a future discussion; there are still many areas in artificial intelligence that I have not learned or even heard of. I am still just a beginner, so I will continue to work hard!
Copyright statement: This article is an original piece by CSDN blogger “I Love to Sleep” and follows the CC 4.0 BY-SA copyright agreement. Please include the original source link and this statement when reprinting.
Original link:
https://blog.csdn.net/weixin_63866037/article/details/128427628

↑ Hot course, limited time coupon! 🎉 Grab it now ↑
Click “Read the Original” to view the course