DEV Community

Vinod S R
Vinod S R

Posted on

CCTV Object Detection using ML - Part 2 with Pi

This post is the second part of my experiments with CCTV series. If you hadn't read the first part please read it over here.

Mission Pi

After the successful setup of CCTV Object detection in my Laptop, I had decided to implement the feature using my Pi4. Thus achieving 24/7 monitoring under low power consumption ie more savings 💰.

Raspberry-Pi-4-4GB-Model-B-8

Took my Pi, installed docker and ran the docker compose as per the frigate documentation.

But it failed and I understood that the current version of frigate won't work with ARM base. (containers != virtualization) 😡 .

"Hey Google! Give me some solution"

This time found a fork of frigate, which uses pyarrow image to run the application. But the code base is far behind the actual frigate one. So latest features will be missing if I use this repo.

The only choice I have is to make my own fork and implement the changes. This also gave me the opportunity to add some features. The original version sends the image and details of an event as separate MQTT message. There is no option to link the two messages (no reference ids) to produce a meaningful output.

Sat down with the code for 2 hours and came up with a Pi enabled frigate version. You can download and build the image from my repo

Changes

During this time I found that my Pi was getting hot > 50 deg. Since the ML process is looking through each frames for identifying the objects it increases the CPU load. I had tweaked the config of frigate, which made the process to look in every 10 frames and this improved the performance and didn't have any problems with the result.

    take_frame: 15

Uploading my config for your reference(https://gist.github.com/vinodsr/edabaf37ca8ab77f68688844ff48124e#file-config-yml)

The Setup

Installation fun

Don't worry it will be easy :)

You need to install a MQTT server(I am using mosquito since it is light weight), compile and run docker and finally setup home automation to trigger some action on the detection.

Install Mosquitto

To install the Mosquitto Broker enter these next commands:

pi@raspberry:~ $ sudo apt update
pi@raspberry:~ $ sudo apt install -y mosquitto mosquitto-clients

To make Mosquitto auto start on boot up enter:

pi@raspberry:~ $ sudo systemctl enable mosquitto.service

Build Frigate for Pi

Clone my repo

git clone https://github.com/vinodsr/frigate.git

Run docker build

 docker build -t frigate:rpi -f Dockerfile.rpi .

Create docker-compose.yml

version: "3.8"
services:
    frigate:
        container_name: frigate
        restart: unless-stopped
        privileged: true
        shm_size: '1g' # should work for 5-7 cameras
        image: frigate:rpi
        volumes:
            - /dev/bus/usb:/dev/bus/usb
            - ./config:/config
            - ./clips:/clips
        ports:
            - "5000:5000"

Run docker compose

docker-compose up -d

Once you build the docker containers, you can toggle the detection by executing docker stop and start comamnds

docker stop frigate
docker start frigate

Further more you can add this as a cronjob to automate the start the stop

crontab -e

Paste the content in the editor to start the detection at 10 pm and stop at 6 am

00 22 * * * docker start frigate
00 06 * * * docker stop frigate

Automate everthing

I use node-red for home automation. Used MQTT, telegram bot nodes to design the automation. (used botmaster to create a telegram bot. You can read more about it from here)

Attaching my flow definition for your reference. You can import it to your node-red flows.

node-red flow


My node-red flow/figcaption>

The Result

After I setup the CCTV automation, when someone 🚶 is at my front door, I will get a telegram message.

Telegram message

Message from telegram bot

Conclusion

This wraps up the CCTV using ML series. It was very fun integrating ML to my normal CCTV cameras. The solution that I achieved is 100% safe and secure. No data is being sent to cloud or stored in any third party server. Hope you like my frigate fork. See you next time with another home IoT project.

Top comments (0)