Skip to content

Create Dashcam with object detection using Raspberry Pi 4 and Tensorflow Lite

  • by

Using live detection object with tensorflow and record it on video format with common usb web, make your own dashcam.

These tutorial combined from EdjeElectronics article how to build model and run on raspberry Pi, and combined with pyimagesearch tutorial to save video

In Indonesia, you can bought equipment here:

Assembly raspberry with memory card, (optional) 3.5 screen and case.

Connect raspberry into monitor using hdmi. Or install VNC server to remote access using laptop.

Open terminal and run following steps:

  • update raspberry
sudo apt-get update && sudo apt-get dist-upgrade

Connect webcam with raspberry

  • install webcam driver on raspberry
# install webcam driver
sudo apt-get install fswebcam
  • check webcam work
# capture image with selected resolution and set output name and format
fswebcam -r 640x480 image.jpg
# clone object
git clone https://github.com/kusumandaru/TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi.git
# rename directory
mv TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi tensorflow-lite-raspberry
# go to folder
cd tensorflow-lite-raspberry
  • create virtual environment, to separate python library to each project
# install virtual environement library
sudo pip3 install virtualenv
# create virtual environment
python3 -m venv tflite-env
# activate environment
source tflite-env/bin/activate

keep in mind to re-activate environment when reboot system.

  • install dependency
bash get_pi_requirements.sh
  • Grab model

Google have selected model to tensorflow lite such as Image classification, object detection, and other. For these project we use object detection. When passed image it will return confidence score and location object.

Confidence score: number between 0 and 1 that indicates confidence that the object was genuinely detected. The closer the number is to 1, the more confident the model is. When score is below threshold score it will abandon from result.

Location: array of four numbers representing a bounding rectangle that surrounds its position consist of: top, down, left, right.

Google provides a sample quantized SSDLite-MobileNet-v2 object detection model which is trained off the MSCOCO dataset and converted to run on TensorFlow Lite. It can detect and identify 80 different common objects, such as people, cars, cups, etc.

wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip
# to get latest image download https://www.tensorflow.org/lite/models/object_detection/overview

than extract inside tensorflow-lite-raspberrydirectory.

unzip coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip -d tflite_model
  • running program, it need library to loaded and make sure webcam is record image, detected object will have squared boxes and label displayed in real time.
python3 TFLite_detection_dashcam.py — modeldir=tflite_model

Since computational power is relative low, it only detect about 4–5 frame per second. To increase power 5–6 times, you can bought coral usb accelerator

Sample video can be located below:

Leave a Reply

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