How To Run TensorFlow Lite on Raspberry Pi for Object Detection

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey everyone this is a step by step guide showing how to set up tensorflow light object detection on the Raspberry Pi at the end of this guide you'll be able to run object detection models to locate and identify objects and images videos or live camera feeds on your Raspberry Pi tensorflow light is a subset of regular tensorflow that has been optimized to run lightweight machine learning models on resource-constrained devices like the pi tensorflow light models have a faster inference time and require less processing power so they run at higher speeds than regular models this video follows my written github guide for setting up tensorflow light which is linked in the video description below the setup process will work for both the Raspberry Pi 3 and the Raspberry Pi for running either raspbian stretch or raspbian buster if you get any errors while following this video check the appendix in my github guide where I'll list common errors and their solutions you can also try googling the error asking about it in the comments section of this video or tweeting me at edge electronics on Twitter I usually respond fastest on Twitter alright let's get started with setting up tensorflow light the first step is to update the Raspberry Pi open a terminal and issue sudo apt-get update then issue sudo apt-get upgrade I just updated my PI so there's no updates to download depending on how long it's been since you've updated your PI this command could take anywhere between a minute and an hour if you're using a PI camera make sure the camera interface is enabled by going to the Raspberry Pi configurations menu clicking on the interfaces tab and verifying that the enable button is checked if it isn't enabled now and then reboot your PI next we'll download the full github repository for this guide which contains the Python code we'll use to run tensorflow Lite and a shell script that will make installing everything easier to download the we type git clone HTTP github.com slash edge electronics slash tensor flow dash light object detection on Android and Raspberry Pi get I promise this will be the only long command we have to type out once it's typed out hit enter to clone the repository this downloads everything into a folder called tensorflow light object detection on android and raspberry pi that's a little long to work with so we'll rename the folder to TF light one by typing MV tensorflow then press tab to complete the path to the file then space TF light 1 then CD into it using CD TF light one will work in this folder for the rest of the guide next we need to create a virtual environment to hold the tensorflow light packages in using a virtual environment will allow us to avoid version conflicts with previously installed versions of tensorflow or other libraries install virtual implying pseudo pip 3 install virtual end [Applause] once it's done installing create a new virtual environment called TF light 1m by issuing Python 3 M ven v TF light wan - in this creates a folder called TF light 1m that will hold all the Python packages for this environment activate the environment by issuing source TF light 1 - n slash pine slash activate once it's activated you'll see TF light 1m appear in a parentheses in front of your command prompt if you ever close and then reopen the terminal window you'll need to reactivate this environment by moving into the TF light 1 folder and then re issuing the source TF light 1 - m / behind slash activate command now we'll install tensorflow and open CV to make things easier I wrote a shell script that will automatically download and install all the packages and dependencies run the shell script by issuing bash get PI requirements Sh this downloads about 400 megabytes worth of installation files so it'll take a while go take a break or grab a drink while it's downloading when everything's finished that means both tensorflow and open CV have been installed the shell script automatically installs the latest version of tensorflow if you want to use a different version just to use pip 3 install tensorflow equals equals and then put the version that you want to install it'll override the existing installation with a specified version next we'll set up the detection model that will be used with tensorflow light you can either download a sample TF flight model provided by Google or use a model you've trained yourself a detection model has two files associated with it a detect EF flight file which holds the detection graph for the model and a label map txt file which provides the labels a preferred way to keep them organized is to create a folder named after the model and keep both files in that folder Google's sample model is a quantized SSD mobile net model that's trained from the MS cocoa dataset and converted to run on tensorflow light the sample model can detect and identify up to 80 common objects the quantized part means it uses 8-bit integer values rather than 32-bit floating-point values in the neural network this allows it to run faster by reducing the memory latency and taking advantage of optimizations inside the CPU using a quantized model speeds up detection while having only a minimal drop in accuracy you can find a link to the model on the TF flight object detection overview page which is linked in the video description below open the page right click download starter model and labels and copy the link address then go back to your terminal type W git and paste the link address then press Enter this will download the model directly to the TF flight 1 folder unzip the model to a folder called a sample TF flight model by typing unzip cocoa then pressing tab to complete the path to the file then - D space sample underscore TF flight underscore model ok now the sample model is all ready to go you can also train your own model to detect custom objects if you want to try it I've created a written guide on github that walks you through how to train a detection model and convert it to tensorflow lite also be creating a series of videos that walks through the process step by step I'll put links to the guides in the description for this video if you've trained your own model using my guide you should have a folder called TF flight model or something similar that contains a detect EF flight file and a label map file to use it simply transfer to your s berry pie using a USB Drive and then move the folder into the home PI TF flight 1 folder once it's been moved into the TF flight 1 folder your custom model is ready to go alright it's time to see the detection model in action to run the real-time webcam detection script use Python 3 TF light detection webcam PI - - model D R equals sample TF flight model this script works with either a PI camera or a regular USB webcam if your model folder has a different name than sample TF light model use that name instead hey guys so once the program initializes a window will appear with your live webcam feed and detection results drawn on the frame as you can see I get about 4.4 FPS with my Raspberry Pi 4 and that's about 3 FPS faster than I was getting with regular tensorflow so it's a pretty nice improvement by I also wrote scripts to perform detection on videos and images first I'll show how the TF light detection video script works using my custom bird squirrel and raccoon detection model as an example instead of using sample TF light model for the model deer argument I'm using my custom folder instead to indicate which video file to process use the - - video argument when the program starts it'll go through each frame of the video and draw detection results this is a video of a bird feeder at my parents house in Montana as you can see the custom detection model is very accurate at locating and identifying the bird you can press Q to stop the script at any time you can use the TF light detection image script on a single image or a folder full of images I put a folder named critters in the TF flight one directory that has pictures of birds squirrels and raccoons when calling the image script I use the - - image deer command to point it at that critters folder you can also just use - - image to specify a single image the program will be form detection on the images one at a time press any key to move on to the next image or press Q to quit so there you go now you've got tensorflow light setup for object detection on your Raspberry Pi in my next video I'll show you how you can get a huge boost in detection speed by using google's choral USB accelerator with the accelerator I get up to five times faster frame rates when running real time object detection the next video will show step by step how set it up and give a brief explanation of how it works the improved speed of tensorflow light makes it more useful for real-time detection applications like smart cameras or alarm systems if you want to see a fun example for using tensorflow on the pie check out my pet detector video where I use object detection to alert me if my cat wants to be let outside I'll also be posting more videos of tensorflow computer vision projects so stay tuned I hope this video helps you get started on your own projects with the Raspberry Pi stay tuned for my next video on setting up the choral USB accelerator thanks for watching and good luck with your projects [Music]
Info
Channel: Edje Electronics
Views: 849,415
Rating: undefined out of 5
Keywords: raspberry pi, tensorflow, machine learning, tensorfow lite, raspberry pi 4, raspberry pi 3, computer vision, deep learning, python
Id: aimSGOAUI8Y
Channel Id: undefined
Length: 10min 48sec (648 seconds)
Published: Tue Nov 12 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.