📜 ⬆️ ⬇️

Another Telegram-bot for video surveillance

In this article we will discuss the basic principles of the work of a telegram-bot for video surveillance.



After reading the article about the created telegram-bot from the materials at hand , I wanted to share with the public my decision for video surveillance.


Iron


In contrast to the approach of the author of this article, I purposely purchased the necessary equipment for the organization of video surveillance in the apartment for the purpose of additional security. I bought an IP camera that knew how to monitor the movement in the frame, put it in a folder on ftp and even send it to email. But all this was, firstly, inconvenient, secondly, there was an error in the firmware that sending to e-mail did not work properly, and, finally, there were many false positives. Therefore, it was decided to write a telegram bot as the most convenient way to alert and control the observation.


So, from me I have an IPEYE camera that can take power through the network (POE), a POE injector, a small server on Ubuntu Server, the Internet with redundancy via a USB modem, and UPS to support the power of the entire system.


Determine the opening of the door


I have only one camera aimed at the front door. Ideally, I wanted to get a picture of a person entering the door. At first I tried to set up motion detection by the forces of the camera itself:




But unfortunately, no matter how I twisted the sliders, the camera in the twilight went crazy and began to constantly alert about movement in the frame when switching from day to night mode and vice versa. It was possible, of course, to raise the motion , he follows the movement in the frame much more accurately. But I did not want to raise an additional service, so I decided to leave the standard ability of the camera to add images to ftp, and check for false positives on my own.

To do this, I wrote a simple daemon on php that monitors the folder for new images, opens them and analyzes the difference in light in the pre-selected areas, something like this (the areas are specially selected above so that people do not accidentally close the areas):




If the difference exceeds the experimentally selected threshold, then the door is really open.
')

Configure the bot


So, our demon has determined that there is motion in the frame and the door is open. Next you need to send this image to the chat telegram. To do this, we need to register a new bot through BotFather and get an API key. To interact with the Telegram API, I used the longman / telegram-bot library. Telegram provides two ways to receive new long-polling messages and a web hook. The second method seemed to me preferable, but for its operation a static URL with an SSL certificate is required. To do this, use letsencrypt or a self-signed certificate that you need to send BotFather. More information about registering bots can be found in the telegraph documentation.


Analysis of wi-fi clients


When you get a lot of messages, you start to simply ignore them, so some automation was needed so that alerts would be sent only when no one is at home. This can be done very simply, just scan the network for the presence of certain wi-fi clients. To do this, you need to install arp-scan ( sudo apt-get install arp-scan ), then you can determine that the client with the necessary mac address is connected like this:


 $output = exec("arp-scan -q --retry=1 --timeout=500 --numeric --destaddr={$mac} {$ip} | grep -oP --color=never \"{$mac}\""); $result = $output === $mac; 

Here I specifically indicate the specific ip-address, so as not to scan the entire network. But for this you need to fix in DHCP ip-Schnick for this MAC-address. But, in principle, ip is optional.



Video recording


Photos are, of course, good. But the best thing will show the incoming person - video. Since the video is not being written here, it can be “obtained” only by recording the stream. Fortunately, my camera provides several rtsp streams encoded in h264. To send a video, you need to record a stream, and send it as a file. To do this, use avconv (fork of ffmpeg in Ubuntu):


 $ffmpeg = "avconv -rtsp_transport tcp -i rtsp://user:password@192.168.1.10/1/h264major -t 10 -an -vcodec copy {$file}"; $mv = "mv {$file} {$out}"; passthru("nohup sh -c '{$ffmpeg} && {$mv}' > /dev/null 2>&1 &"); 

Here I write a video in the background, and then move it to a folder for tracking. The daemon picks up a new file and sends it as a video. Because I have no sound in the video, Telegram automatically converts it to gif on the desktop client, which is very convenient.


Auto daemon


The daemon is written in php, and although it can work for months, no one is immune from mistakes. Therefore, it is good to see that the demon has not completed the process. You could, of course, set up a supervisor , but I decided to do just auto-cron. If the process is alive, the daemon does not restart:


 passthru("ps -p $pid > /dev/null", $result); return !$result; 

Conclusion


Thanks to all the actions taken, the bot notifies me about the incoming person with two photos and a five-second video. As a bonus, a bot can also notify about the time of departure and arrival of certain wi-fi clients, and whether they are at home now.


You can view the result on github .

Source: https://habr.com/ru/post/343616/


All Articles