Yolov5 ported Raspberry Pi to achieve target detection

Hello, everyone! I wrote a few blogs about Yolov5- related projects before, and after learning about the Raspberry Pi , I updated a few Raspberry Pi blogs. My ultimate goal is to port Yolov5 to the Raspberry Pi and realize it through the camera on the Raspberry Pi. Target Detection. Think about it, in a factory, you can't use a laptop as a host computer to send instructions to a slave computer , so the Raspberry Pi has unique advantages as a small PC. Next, project-driven, introduce the whole process, including Yolov5 data set acquisition , model training , recognition effect , Raspberry Pi environment construction , Raspberry Pi transplantation , and model detection effect on Raspberry Pi !

Table of contents

1. Task description 

2. Data set acquisition

3. Yolov5 model training

4. Model training results

5. Detection effect

6. Raspberry Pi Environment Construction

Seven, Yolov5 porting Raspberry Pi

8. Raspberry Pi detection effect

Summarize


1. Task description 

The recognition of the workpiece is realized by means of machine vision, and the Raspberry Pi is used as the upper computer to issue different instructions to the lower computer according to different recognition effects to control the operation of the entire machine. The process is as follows:

2. Data set acquisition

This is a part sent by the teacher, so there is definitely no directly usable model on the Internet, unlike the ready-made data sets for mask detection, so we need to obtain the data sets ourselves. Since we have only two parts, if we want to get a large number of pictures, we can rotate them . I set each picture to generate a new picture every 10 degrees, so that 36 pictures can be generated without taking a picture. Photos, source of expanded dataset. Then label the pictures with labelImg , so that we have obtained a large number of data sets and corresponding label txt files. I allocate pictures according to the ratio of test:train:val = 1:8:2 . With this our dataset is ready.

3. Yolov5 model training

Since the creator of yolov5 open sourced the model code , we can use it directly. We only need to modify the path of train and val , and change it to our own, as well as the number of categories . I divide it into two categories here, so it is 2, the label name It is the corresponding color, blue and red. I chose the yolov5s model. The model is lighter and the detection speed is faster. The number of categories is also set to 2. After we are ready, we will start training. Since I directly use the cpu for training , the training output is 5.7 Hours or so, if you use a GPU , the speed can be improved a lot.

4. Model training results

 From the figure, we can see that both the accuracy rate and the recall rate are close to 100% , and the average mAp reaches 0.995 , which shows that the recognition effect of the model is very good, and the task of our workpiece recognition can be well completed.

5. Detection effect

From the video, we can see that the red and blue workpieces are recognized, the red ones are labeled red, and the blue ones are labeled blue . The number represents the accuracy, which is basically above 0.8 , which is an ideal result. The video is detected frame by frame, and the recognition effect of each frame is very good. This is the result of the model running on the laptop. Now that our Yolov5 module has come to an end, the next question is how to make it run perfectly on the Raspberry Pi .

6. Raspberry Pi Environment Construction

This is my first contact with Raspberry Pi, and also my first contact with Linux , so I have encountered many problems, and sometimes it takes a whole day to solve a problem. I recorded this learning process and all kinds of intractable diseases I encountered, and posted them on csdn, hoping to help you. The link is as follows: Yolov5 porting Raspberry Pi 4B problem summary   Raspberry Pi 4B Raspberry Pi study notes

The environment I chose is the 64-bit operating system of the Raspberry Pi . The advantage of this is that the download of opencv is very convenient. After a long period of exploration, I found that Pytorch is difficult to install on a 32-bit operating system. Even if the installation is successful, there is no way to run it. . So I chose the Raspberry Pi 64-bit operating system and installed openv and pytorch.

7. Yolov5 porting Raspberry Pi

After the environment is ready, Yolov5 will be transplanted. First, connect to the Raspberry Pi through vncviewer , so that we can control the Raspberry Pi through the computer. If there is a problem here, please refer to my two blogs above.

Transfer the trained pt module to the Raspberry Pi through the top file transfer, use the linux cd command to enter the folder where yolo is located, enter the running command, python3 runs the detect file, and the weight file uses the best we just passed in. pt file , the source is 0 to call the CSI camera installed on our Raspberry Pi . Then press Enter to run it.

8. Raspberry Pi detection effect

 Let's take a look at the detection effect: the left side is the detection environment, and the Raspberry Pi is equipped with a csi camera to obtain images. The following picture is a picture during detection. You can see that the red parts are recognized and marked with red labels . The blue parts are also identified and marked with blue labels . We can use these two labels to send different instructions to the lower computer to complete the specified tasks.

Summarize

The project mainly uses Yolov5 for target detection , and then uses the Raspberry Pi as the upper computer , transplants the model to the Raspberry Pi for recognition, and controls the operation of the lower computer . There are countless problems encountered in the process, including hardware and software, and often one problem lasts for a day. This process is painful, but fortunately, the result is satisfactory. During the detection, I also found that the recognition speed of the Yolov5 model is indeed a bit slow, and the laptop is a bit immobile, and the Raspberry Pi is even more stuck, and it takes 2-3 seconds to detect a picture. There are two optimization directions in the back, one is to optimize the model, using the yolo-lite model, and the other is to use the opencv template matching method to identify the optimization method, which should be much faster without deep learning. Of course, this is something to say later. There are still many places in artificial intelligence that I haven’t learned or even heard of.

Guess you like

Origin blog.csdn.net/weixin_63866037/article/details/128427628