Run object detection model on the Raspberry Pi

Overview

Intro

Using TensorFlow Lite with Python is great for embedded devices based on Linux, such as Raspberry Pi.

This is the guide for installing TensorFlow Lite on the Raspberry Pi and running pre-trained object detection models on it.

Step 1. Setting up Rasperry Pi

Upgrade Raspbian Stretch to Buster

(If you on Buster, skip this step and simply run sudo apt-get update and sudo apt-get dist-upgrade)

$ sudo apt-get update && sudo apt-get upgrade -y

Verify nothing is wrong. Verify no errors are reported after each command. Fix as required (you’re on your own here!).

$ dpkg -C
$ apt-mark showhold

Prepare apt-get Sources

Update the sources to apt-get. This replaces “stretch” with “buster” in the repository locations giving apt-get access to the new version’s binaries.

$ sudo sed -i 's/stretch/buster/g' /etc/apt/sources.list    
$ sudo sed -i 's/stretch/buster/g' /etc/apt/sources.list.d/raspi.list

Verify this caught them all by running the following, expecting no output. If the command returns anything having previously run the sed commands above, it means more files may need tweaking. Run the sed command for each. The aim is to replace all instances of “stretch”.

$ grep -lnr stretch /etc/apt

Speed up subsequent steps by removing the list change package.

$ sudo apt-get remove apt-listchanges

Do the Upgrade

To update existing packages without updating kernel modules or removing packages, run the following.

$ sudo apt-get update && sudo apt-get upgrade -y

Alternatively, to include kernel modules and removing packages if required, run the following

$ sudo apt-get update && sudo apt-get full-upgrade -y

Cleanup old outdated packages.

$ sudo apt-get autoremove -y && sudo apt-get autoclean

Verify with

 cat /etc/os-release.

Update Firmware

$ sudo rpi-update

and

sudo apt-get install -y python3-pip

and

pip3 install --upgrade setuptools

2. Making sure camera interface is enabled in the Raspberry Pi Configuration menu

Click the Pi icon in the top left corner of the screen, select Preferences -> Raspberry Pi Configuration, and go to the Interfaces tab and verify Camera is set to Enabled. If it isn't, enable it now, and reboot the Raspberry Pi.

Converting Tensorflow to Tensorflow Lite

Using TensorFlow Lite converter. It takes TensorFlow model and generates a TensorFlow Lite model (an optimized FlatBuffer format identified by the .tflite file extension).

Step 2. Install TF Lite dependecies and set up virtual environment

clone this repo

git clone https://github.com/yanovsk/Raspberry-Pi-TF-Lite-Object-Detection

rename the folder to "tfliteod"

mv Raspberry-Pi-TF-Lite-Object-Detection tfliteod
cd tfliteod

run shell script to install get_pi_requirements

bash get_pi_req.sh

Note: shell script will auto install the lastest version of Tensorflow. To install specific version of TF, run pip3 install tensorflow==x.xx (where x.xx stands for the version you want to install)

Set up virtual environment

Install vitrtualenv

pip3 install virtualenv 

Then, create the "tfliteod-env" virtual environment by issuing:

python3 -m venv tfliteod-env

This will create a folder called tfliteod-env inside the tflite1 directory. The tfliteod-env folder will hold all the package libraries for this environment. Next, activate the environment by issuing:

source tfliteod-env/bin/activate

Step 3. Set up TensorFlow Lite detection model

Once, tensorflow is install we can proceed to seting up the object detection model.

We can use either pre-trained model or train it on our end. For the simplicity sake let's use pre-trained sample model by google

Download the sample model (also could be done thru direct link here)

wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip

upzip it

unzip coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip -d Sample_model

Step 4. Run the model

Note: the model should work on either Picamera module or any other webcam plugged in to the Raspberry Pi as a usb device.

From home/pi/tfliteod run the following command:

python3 TFL_object_detection.py --modeldir=Sample_model

After initializing webcam window should pop-up on your Raspebbery Pi and object detection should work.

Note: this model can recongnize only 80 common objects (check labels.txt for more info on metadata)

However, you can custom train the model using this guide.

Happy hacking!

Owner
Dimitri Yanovsky
Dimitri Yanovsky
Pytorch implementations of Bayes By Backprop, MC Dropout, SGLD, the Local Reparametrization Trick, KF-Laplace, SG-HMC and more

Bayesian Neural Networks Pytorch implementations for the following approximate inference methods: Bayes by Backprop Bayes by Backprop + Local Reparame

1.4k Jan 07, 2023
PointNetVLAD: Deep Point Cloud Based Retrieval for Large-Scale Place Recognition, CVPR 2018

PointNetVLAD: Deep Point Cloud Based Retrieval for Large-Scale Place Recognition PointNetVLAD: Deep Point Cloud Based Retrieval for Large-Scale Place

Mikaela Uy 294 Dec 12, 2022
Scalable Graph Neural Networks for Heterogeneous Graphs

Neighbor Averaging over Relation Subgraphs (NARS) NARS is an algorithm for node classification on heterogeneous graphs, based on scalable neighbor ave

Facebook Research 67 Dec 03, 2022
A Neural Net Training Interface on TensorFlow, with focus on speed + flexibility

Tensorpack is a neural network training interface based on TensorFlow. Features: It's Yet Another TF high-level API, with speed, and flexibility built

Tensorpack 6.2k Jan 01, 2023
Official PyTorch Implementation of Mask-aware IoU and maYOLACT Detector [BMVC2021]

The official implementation of Mask-aware IoU and maYOLACT detector. Our implementation is based on mmdetection. Mask-aware IoU for Anchor Assignment

Kemal Oksuz 46 Sep 29, 2022
Session-aware Item-combination Recommendation with Transformer Network

Session-aware Item-combination Recommendation with Transformer Network 2nd place (0.39224) code and report for IEEE BigData Cup 2021 Track1 Report EDA

Tzu-Heng Lin 6 Mar 10, 2022
Adversarial Attacks are Reversible via Natural Supervision

Adversarial Attacks are Reversible via Natural Supervision ICCV2021 Citation @InProceedings{Mao_2021_ICCV, author = {Mao, Chengzhi and Chiquier

Computer Vision Lab at Columbia University 20 May 22, 2022
Like ThreeJS but for Python and based on wgpu

pygfx A render engine, inspired by ThreeJS, but for Python and targeting Vulkan/Metal/DX12 (via wgpu). Introduction This is a Python render engine bui

139 Jan 07, 2023
A collection of pre-trained StyleGAN2 models trained on different datasets at different resolution.

Awesome Pretrained StyleGAN2 A collection of pre-trained StyleGAN2 models trained on different datasets at different resolution. Note the readme is a

Justin 1.1k Dec 24, 2022
NumPy로 구현한 딥러닝 라이브러리입니다. (자동 미분 지원)

Deep Learning Library only using NumPy 본 레포지토리는 NumPy 만으로 구현한 딥러닝 라이브러리입니다. 자동 미분이 구현되어 있습니다. 자동 미분 자동 미분은 미분을 자동으로 계산해주는 기능입니다. 아래 코드는 자동 미분을 활용해 역전파

조준희 17 Aug 16, 2022
An Unbiased Learning To Rank Algorithms (ULTRA) toolbox

Unbiased Learning to Rank Algorithms (ULTRA) This is an Unbiased Learning To Rank Algorithms (ULTRA) toolbox, which provides a codebase for experiment

back 3 Nov 18, 2022
Learning Representational Invariances for Data-Efficient Action Recognition

Learning Representational Invariances for Data-Efficient Action Recognition Official PyTorch implementation for Learning Representational Invariances

Virginia Tech Vision and Learning Lab 27 Nov 22, 2022
[NeurIPS'21] "AugMax: Adversarial Composition of Random Augmentations for Robust Training" by Haotao Wang, Chaowei Xiao, Jean Kossaifi, Zhiding Yu, Animashree Anandkumar, and Zhangyang Wang.

AugMax: Adversarial Composition of Random Augmentations for Robust Training Haotao Wang, Chaowei Xiao, Jean Kossaifi, Zhiding Yu, Anima Anandkumar, an

VITA 112 Nov 07, 2022
Prior-Guided Multi-View 3D Head Reconstruction

Prior-Guided Head MVS This repository includes some reconstruction results of our IEEE TMM 2021 paper, Prior-Guided Multi-View 3D Head Reconstruction.

11 Aug 17, 2022
Source code of generalized shuffled linear regression

Generalized-Shuffled-Linear-Regression Code for the ICCV 2021 paper: Generalized Shuffled Linear Regression. Authors: Feiran Li, Kent Fujiwara, Fumio

FEI 7 Oct 26, 2022
Open source repository for the code accompanying the paper 'PatchNets: Patch-Based Generalizable Deep Implicit 3D Shape Representations'.

PatchNets This is the official repository for the project "PatchNets: Patch-Based Generalizable Deep Implicit 3D Shape Representations". For details,

16 May 22, 2022
FishNet: One Stage to Detect, Segmentation and Pose Estimation

FishNet FishNet: One Stage to Detect, Segmentation and Pose Estimation Introduction In this project, we combine target detection, instance segmentatio

1 Oct 05, 2022
Orange Chicken: Data-driven Model Generalizability in Crosslinguistic Low-resource Morphological Segmentation

Orange Chicken: Data-driven Model Generalizability in Crosslinguistic Low-resource Morphological Segmentation This repository contains code and data f

Zoey Liu 0 Jan 07, 2022
ONNX-GLPDepth - Python scripts for performing monocular depth estimation using the GLPDepth model in ONNX

ONNX-GLPDepth - Python scripts for performing monocular depth estimation using the GLPDepth model in ONNX

Ibai Gorordo 18 Nov 06, 2022
iNAS: Integral NAS for Device-Aware Salient Object Detection

iNAS: Integral NAS for Device-Aware Salient Object Detection Introduction Integral search design (jointly consider backbone/head structures, design/de

顾宇超 77 Dec 02, 2022