DEV Community

Vinod S R
Vinod S R

Posted on • Updated on

CCTV Object Detection using ML

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

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
Enter fullscreen mode Exit fullscreen mode

Wow
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

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
Enter fullscreen mode Exit fullscreen mode

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/ 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

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

Top comments (1)

Collapse
 
milkyfloor profile image
MilkyFloor

Anyway, I would recommend you to read about Ajax. There are also lots of videos on Youtube about it. You'd probably like that. I did not want to install any professional system as well. But finally, I agreed to do that. You do not have to throw away your CCTV system. If I were you, I would install Ajax (they have qualitative motion sensors that work great) and connect my cameras to the system. If the problem is in the motion detectors only, you do not have to install all the items the company sells. Just choose what you need.