Deploying Yolov5 on Raspberry Pi for Object Detection

Click the card below to follow the “New Machine Vision” public account

Important content delivered at the first time






1. Task Description

Realize the identification of workpieces through machine vision, using Raspberry Pi as the host, issuing different instructions to the lower machine based on different identification effects, controlling the entire machine operation. The process is as follows:

Deploying Yolov5 on Raspberry Pi for Object Detection

2. Dataset Acquisition

This is a part sent by the teacher, so there is definitely no directly usable model online, unlike mask 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 a new photo by rotating each image by 10 degrees. This way, for every photo taken, we can generate 36 photos, expanding the source of the dataset.

Afterwards, we use labelImg to annotate the images, thus we have obtained a large amount of dataset and corresponding label txt files. I distributed the images according to the ratio of test:train:val = 1:8:2. Thus, our dataset is ready.

Deploying Yolov5 on Raspberry Pi for Object Detection

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, and the number of classes. I have 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 has a faster detection speed, also setting the number of classes to 2. After preparation, we start training. Since I trained directly on the CPU, the training duration was about 5.7 hours. If using a GPU, the speed can be significantly improved.

Deploying Yolov5 on Raspberry Pi for Object Detection

4. Model Training Results

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

Deploying Yolov5 on Raspberry Pi for Object Detection

5. Detection Effect

Deploying Yolov5 on Raspberry Pi for Object Detection

From the video, we can see that the red and blue workpieces are recognized, with the red labeled as ‘red’ and the blue labeled as ‘blue’. The numbers represent precision, 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 a laptop. At this point, our Yolov5 module is completed. The next question is how to make it run perfectly on Raspberry Pi.

6. Raspberry Pi Environment Setup

This is my first time encountering Raspberry Pi and Linux, so I encountered many problems. Sometimes it took a whole day to solve a single problem.

I recorded this learning process, along with various difficulties encountered, and posted it on CSDN, hoping to help you. The link is as follows: Summary of Problems in Deploying Yolov5 on Raspberry Pi 4B 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 time of exploration, I found that Pytorch is very difficult to install on a 32-bit operating system. Even if installed successfully, it cannot run.

So I chose the 64-bit operating system of Raspberry Pi and installed opencv and pytorch.

Deploying Yolov5 on Raspberry Pi for Object Detection

7. Yolov5 Deployment on Raspberry Pi

After preparing the environment, we need to deploy Yolov5. First, connect to Raspberry Pi through vncviewer, so we can control Raspberry Pi from the computer. If there are problems here, you can refer to my previous two blogs.

Deploying Yolov5 on Raspberry Pi for Object Detection

Transfer the trained pt module to Raspberry Pi through the file transfer at the top. Use the linux cd command to enter the folder where yolo is located and enter the running command, python3 run detect file, using the weight file we just transferred, best.pt, and source as 0 to call the CSI camera installed on Raspberry Pi. After that, press enter to run it.

Deploying Yolov5 on Raspberry Pi for Object Detection

8. Raspberry Pi Detection Effect

Deploying Yolov5 on Raspberry Pi for Object Detection

Let’s take a look at the detection effect: the left side shows the detection environment, with Raspberry Pi equipped with a CSI camera to capture images. The image below is one of the images during detection.

It can be seen that the red part is recognized and labeled as ‘red’, and the blue part is also recognized and labeled as ‘blue’. We can use these two labels to issue different instructions to the lower machine to complete specified tasks.

Conclusion

The project mainly involves using Yolov5 for object detection, and then using Raspberry Pi as the host to deploy the model for recognition and control the operation of the lower machine.

During the process, I encountered countless problems, both hardware and software. Often a single problem would take a day to resolve. This process was painful, but fortunately, the result was satisfactory. During detection, I also found that the recognition speed of the Yolov5 model is indeed a bit slow; even the laptop can struggle, and it is even more laggy on Raspberry Pi, taking 2-3 seconds to detect a single image.

Future optimization directions include two aspects: one is to optimize the model by using the yolo-lite model, and the other is to optimize the method by using opencv template matching for recognition, which should significantly speed up without involving deep learning.

Of course, that’s a discussion for another time. There are still many areas in artificial intelligence that I have not learned, and I am still just a beginner. I will continue to work hard!

Copyright statement: This article is an original article by CSDN blogger “I Love to Sleep” and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement when reprinting.

Original link:

https://blog.csdn.net/weixin_63866037/article/details/128427628

Editor: Gu Yue Ju

Statement: Some content comes from the internet, for the purpose of learning and communication for readers. The copyright of the article belongs to the original author. If there are any issues, please contact for deletion.



—THE END—
Deploying Yolov5 on Raspberry Pi for Object Detection

Leave a Comment

×