Raspberry Pi TensorFlow 2 set up and Yolo V3 object detection | by Rokas Balsys | Oct, 2021

This tutorial will present step-by-step directions for organising TensorFlow 2.* on the Raspberry Pi. By following this step-by-step information, it is possible for you to to make use of your Raspberry Pi to carry out object detection on stay video feeds from a Picamera or USB webcam. After this tutorial, it is possible for you to to mix this tutorial with my earlier tutorials on the right way to practice your individual customized YOLOv3 object detector to establish particular objects. The entire tutorial is split into seven components:

Arrange and replace the Raspberry Pi

First, it is advisable install a Raspberry Pi working system picture on an SD card for those who haven’t executed that earlier than. It’s really useful to make use of Raspberry Pi Imager. It seems to be following whereas putting in:

As you may see, you may even select to put in it with a web-based obtain.

I made a decision to put in Raspberry Pi OS (32-bit) with Desktop. It’s as much as you what you will use.

After set up, the very first thing it is best to do to make issues simpler for us is to set up a local network and wireless connectivity and enable SSH. Additionally, you will have to arrange your shopper to hook up with raspberry via ssh. All this data is given on the similar hyperlink.

To point out you the following tutorial steps, I'll connect with my raspberry via RealVNC viewer.

First, the Raspberry Pi must be absolutely up to date. Open a terminal and difficulty:

sudo apt-get replace
sudo apt-get dist-upgrade

Relying on how lengthy it’s been because you’ve up to date your Pi, the improve might take wherever between a minute and an hour.

A few of you ask me why it runs python3 after I kind python within the terminal. That’s comparatively simple. If you're on Linux, open your .bashrc file nano ~/.bashrc. Sort alias python=python3 on to a brand new line on the finish of the file, then save the file with ctrl+o and shut the file with ctrl+x. Then, again at your command line, kind supply ~/.bashrc. Now your alias ought to be everlasting.

Set up all wanted packages and libraries

Raspberry OS ships with Python 3 because the default Python set up. We have to full the next steps to put in pip (pip3) for Python 3:

sudo apt set up python3-pip

The command above will even set up all of the dependencies required for constructing Python modules.

As soon as the set up is full, we will confirm the set up by checking the pip model:

pip3 --version

More often than not, this command installs not the latest model, so we have to improve it with the next command:

pip3 set up --upgrade pip

Earlier than transferring to a different step, we should always set up a batch of packages with the command under. This may occasionally take some time (copy and paste to the terminal):

Set up OpenCV

I favor to make use of OpenCV in all my tasks as a result of it’s simpler to work with and fewer error-prone. The article detection scripts on this information’s GitHub repository use OpenCV. So, we have to set up OpenCV. You probably have already tried to run pip3 set up opencv-python, you already could also be confronted, that this fashion, it would not work on Raspberry Pi.

The issue is as a result of the present model of OpenCV (presently is just not appropriate with RPi. To get OpenCV engaged on the Raspberry Pi, additionally there are fairly a number of dependencies that should be put in via apt-get. If any of the next instructions don’t work, difficulty “sudo apt-get replace" and check out once more. Challenge:

You probably have already tried to put in OpenCV, simply run pip3 uninstall opencv-python. Now that we have all these put in, we will set up OpenCV, run pip3 set up opencv-python==

This could kind you out, and you probably have any points, verify what variations of OpenCV are appropriate along with your Raspbian or no matter you're utilizing.

Set up TensorFlow

Now that we’ve put in all of the packages, we have to set up TensorFlow. If you happen to already tried to difficulty pip3 set up tensorflow you noticed, it tries to put in TensorFlow 1.* or says: Couldn't discover a model that satisfies the requirement, that is our downside. So I discovered a really good tutorial on GitHub to seek out our answer to this downside.

Within the above hyperlink, it says to difficulty the next instructions right into a terminal:

sudo pip3 set up keras_applications==1.0.8 --no-deps
sudo pip3 set up keras_preprocessing==1.1.0 --no-deps
sudo pip3 set up h5py==2.9.0
sudo pip3 set up pybind11
pip3 set up -U --user six wheel mock
wget "https://uncooked.githubusercontent.com/PINTO0309/Tensorflow-bin/grasp/tensorflow-2.2.0-cp37-cp37m-linux_armv7l_download.sh"
sh ./tensorflow-2.2.0-cp37-cp37m-linux_armv7l_download.sh
sudo pip3 uninstall tensorflow
sudo -H pip3 set up tensorflow-2.2.0-cp37-cp37m-linux_armv7l.whl

After set up, open python on the terminal, import TensorFlow and print its model:

>>> import tensorflow as tf
>>> print(tf.__version__)

Arrange my YOLOv3 GitHub listing

Up thus far, we have now nearly every little thing we want put in and dealing: python, OpenCV, TensorFlow. Now we will obtain my GitHub repository, which we’ll use for our real-time object detection with YOLOv3. I like to recommend navigating to Desktop (cd Desktop) within the terminal, and right here clone my repository: git clone https://github.com/pythonlessons/TensorFlow-2.x-YOLOv3.git.

Whereas we're doing this detection, not with Raspberry Pi, we might set up necessities from a file, however now to not mess with OpenCV and TensorFlow, we’ll do that step manually:

As a result of I'm utilizing Raspberry Pi3, it’s fairly weak to run the unique YOLOv3, so I'll use YOLOv3-Tiny on this tutorial. So I'm downloading yolov3-tiny weights:

wget -P model_data https://pjreddie.com/media/files/yolov3-tiny.weights

Raspberry Pi Digital camera configuration

Okay, now every little thing is about up for performing object detection on the Pi! The Python script detects objects in stay feeds from a Picamera or USB webcam. The script units paths to the mannequin and label map, hundreds the mannequin into reminiscence, initializes the Picamera, and begins working object detection on every video body from the Picamera.

If you happen to’re utilizing a Picamera, make certain it's enabled within the Raspberry Pi configuration menu:

Raspberry Pi configuration menu

Slightly than utilizing the Raspberry Pi digicam module, you should utilize a typical USB webcam to take photos and movies on the Raspberry Pi. As a result of I've solely a typical USB digicam, I'll use it!

First, we have to set up the fswebcam package deal:

sudo apt-get set up fswebcam

Now we have to check it. Enter the command fswebcam adopted by a filename, and an image might be taken utilizing the webcam and saved to the filename specified:

fswebcam -r 640x480 --no-banner picture.jpg

That is the best option to check our webcam. In case your picture was created, because of this our digicam is working, and we will transfer to the following ultimate step. In any other case, attempt to debug your downside with google assist!

Detect objects!

Now, to run a real-time object detection, we have to change configurations of our undertaking, so:

Open yolov3/configs.py file and alter TRAIN_YOLO_TINY from False to True, as a result of we downloaded tiny mannequin weights. The subsequent factor I alter is YOLO_INPUT_SIZE from 416 to 320. A smaller enter picture will give us extra FPS.

Now open detection_demo.py script and remark detect_image operate and uncomment detect_realtime operate, this fashion, our script will use webcam output to our mannequin as enter.

Lastly, open the terminal and sort python detection_demo.py.

If you happen to did every little thing accurately, it is best to see the outcomes on the raspberry window. If you wish to see my outcomes, verify the above YouTube tutorial.


Word: If you happen to plan to run this on the Pi for prolonged intervals (higher than 5–10 minutes), make certain to have a heatsink put in on the Pi’s principal CPU! All of the processing causes the CPU to run sizzling. With out a heatsink, it should shut down as a consequence of excessive temperature fairly quickly.

On this tutorial, I confirmed solely the right way to run detection on the pretrained COCO dataset. To run thins detection in your customized mannequin, verify my earlier tutorials on GitHub on the finish of the readme file.

Thanks for following via with this tutorial. I hope you discovered it useful. Good luck along with your object detection functions on the Raspberry Pi!

See you within the subsequent tutorial half!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *