CCTV Object Detection using ML - Part 2 with Pi
Tags: tensorflow, cctv, iot, python
Published: 2020-10-10 07:44:50

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](https://dev.to/vinodsr/cctv-object-detection-using-ml-4923).

# 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 :moneybag:.

![Raspberry-Pi-4-4GB-Model-B-8](https://dev-to-uploads.s3.amazonaws.com/i/k71cfzn2l374hdo8xx6z.jpg)
 
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) :rage: .

"Hey Google! Give me some solution"

This time found a [fork of frigate](https://github.com/kpine/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](https://github.com/vinod/frigate)

### Changes
* Added Raspberry Pi support (Tested in Pi 4)
* Added a new MQTT event (frigate/<camera_name>/<object_name>/event) that will hold the event details and snapshot in base64. (schema)[https://github.com/vinodsr/frigate#frigatecamera_nameobject_nameevent]

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](https://media.giphy.com/media/kdiLau77NE9Z8vxGSO/giphy.gif)
<figcaption>Don't worry it will be easy :)</figcaption>
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](https://nodered.org/) for home automation. Used MQTT, [telegram bot](https://flows.nodered.org/node/node-red-contrib-telegrambot) nodes to design the automation. (used botmaster to create a telegram bot. You can read more about it from [here](https://core.telegram.org/bots#creating-a-new-bot))

Attaching my flow definition for your [reference](https://gist.github.com/vinodsr/edabaf37ca8ab77f68688844ff48124e#file-node-red-flow-sample-json). You can import it to your node-red flows.

![node-red flow](https://dev-to-uploads.s3.amazonaws.com/i/o28fnyyvx0v3pmlmr65k.png)
 <figcaption>My node-red flow/figcaption>
 
# The Result
After I setup the CCTV automation, when someone :walking: is at my front door, I will get a telegram message. 

![Telegram message](https://dev-to-uploads.s3.amazonaws.com/i/wqlog5t5xaxbayu35zl0.jpg)
<figcaption>Message from telegram bot</figcaption>
 
# 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](https://github.com/vinod/frigate). See you next time with another home IoT project.