CCTV Object Detection using ML
Tags: tensorflow, cctv, python, iot
Published: 2020-09-12 09:51:54

My CCTV setup always have challenges in terms of motion detection. It simply uses the difference between frames based on a tolerance value and triggers an alert. I need to have a more sophisticated alert mechanism, which can detect person, cat , dog , car etc. But right now i am not going to upgrade the CCTV to a high end system.

![Google](https://dev-to-uploads.s3.amazonaws.com/i/aarbsdl3piscmvqhchax.png)
 
`Hey google find me a utility which can detect objects from CCTV camera`. 

After a  long search i ended up in a github page of frigate. https://github.com/blakeblackshear/frigate

As per the definition in the README.md 
```
Uses OpenCV and Tensorflow to perform realtime object detection locally for IP cameras. Designed for integration with HomeAssistant or others via MQTT.

* Leverages multiprocessing heavily with an emphasis on realtime over processing every frame
* Uses a very low overhead motion detection to determine where to run object detection
* Object detection with Tensorflow runs in a separate process
* Object info is published over MQTT for integration 
* An endpoint is available to view an MJPEG stream for debugging, but should not be used continuously
```
![Wow](https://media.giphy.com/media/3XR0chfiSTtAI/giphy.gif)
WoW !. This is what i was looking for.

Wait a second! Did they say IP camera ? All my camera are analog camera connected to HikVision DVR (DS-7B08HUHI-K1). :( 

I didn't give up. Loaded Google and started the search with the query `HIKVISION CAMERA STREAM WITHOUT IP CAMERA`

![](https://media.giphy.com/media/gf5lHEzYVU8DoVVlhs/source.gif)

After hours of searching. I found that HikVision have the capability to share the rtsp stream of any camera even though non-ip camera. 

You need to connect the DVR to your network using a LAN cable
Once it is configured, then the rtsp stream is available at 
`rtsp://<username>:<password>@<ip of the DVR>:554/Streaming/channels/<x>0<y>`

where username and password is same as that you use to login to the CCTV console,
x is the camera number
y can be 1 or 2. 2 will be a low quality stream and 1 will be a high quality stream. 

eg : 
`rtsp://<username>:<password>@<ip of the DVR>:554/Streaming/channels/102`

So now i have a IP camera stream. So back to the setup of frigate. 

It is very simple. You need to install docker. Head over to the docker.com to install docker in your machine. (https://docs.docker.com/get-docker/)

You can then follow the steps mentioned in the github page (https://github.com/blakeblackshear/frigate#getting-started). 

You can also tweak the `config.yml` for your preference.
```
    ################
    # Camera level object config. This config is merged with the global config above.
    ################
    objects:
      track:
        - person
```

You can use this section to tell frigate about the object that needs to be tracked. By default it tracks person. You can add cats, dog etc. The available objects that can be tracked by default can be found at (https://dl.google.com/coral/canned_models/coco_labels.txt)

If you setup a MQTT broker like mosquitto (https://mosquitto.org/), then frigate will send events on each detection to the broker. You can use this for automation. 

Make sure you give the rtsp link of the camera correctly and the camera name is correct. 

If everything is done correctly, you can start the docker and the site will be available @ port 5000

Load http://localhost:5000/<cameraname> to see the debug stream of the camera with object recognition. 
`As per the author the debug stream can reduce the performance of the program`

Once a person is in front of the camera, you will see something like this in the debug stream, also an event will be published in the MQTT server, which can be used to trigger some message or anything. 

![Debug Stream](https://dev-to-uploads.s3.amazonaws.com/i/kjg7karvn97eituezz90.jpg)
 
 
#Next Steps

My next step is to integrate this with my home automation to trigger a message on my phone with the picture. Also to run this on raspberry pi, since the original version doesn't support pi.

I will be back once it is ready. Till then take care bye.

Read the second part of the blog [here](https://dev.to/vinodsr/cctv-object-detection-using-ml-part-2-with-pi-glf)