A simple version of an indoor video surveillance system using a motion sensor and Python on the Raspberry platform

    Good day, Habrovsk! After several experiments with the development of a video surveillance system, which I reflected in a previous publication , I decided to share the final solution.

    Description of the final decision

    After analyzing my experience using the video surveillance system and recommendations for the previous post (special thanks to Varkus ), as a result I chose for myself the implementation using the PIR sensor (hereinafter referred to as the sensor) for determining movements / movements. The solution algorithm is implemented in Python.

    Earlier, I did not want to use an additional hardware component in my solution, but the approach using a sensor was very simple and concise. Moreover, the sensor turned out to be inexpensive if you order it on aliexpress.

    Hardware part

    From the "iron" used:

    - Raspberry Pi3;
    - Logitech USB camera;
    - motion sensor SR501P1.
    To work at night, it is additionally necessary to purchase an infrared emitter (lamp).

    The sensor was connected using 30-cm connectors, the appearance is given below.

    Yes, I preselected the necessary sensitivity by adjusting it using a potentiometer.

    Software part

    To automate the video surveillance process, I limited myself to one Python script that does all the work, namely, it monitors movements using a sensor and, if it is detected:

    - takes several pictures of the room using the Python OpenCV library for this;
    - saves pictures in Yandex.Disk;
    - upon reaching a certain threshold of movements, in my case, ten identified movements, stops shooting and sends the last of the saved images to Telegram.

    The threshold (threshold) is set in order to limit the number of images when recording movements. Upon reaching the threshold, shooting stops and one, the last saved photo is sent to Telegram. I set the threshold to ten, this is enough to fix the fact of penetration in the photo. He considered the video to be superfluous, besides, the photo takes up less space.

    In the final version, I refused to use the algorithm for determining the faces in the photo.
    The script code, without an algorithm for determining faces, is given below.

    from gpiozero import MotionSensor
    import cv2
    import telepot
    import time
    def Telegram_send(input_file):
     import glob
     import os
     bot = telepot.Bot('*************************************')
     bot.sendMessage(*********, 'Motion detected!')
     bot.sendPhoto(*********, open(file,'rb'))
    def main():
      while counter<=threshold:
       #if pir.motion_detected:
        print("Motion detected at "+str(time.strftime("%Y%m%d-%H%M%S")))
        if not camera.isOpened():
      if counter >=threshold: Telegram_send(file)
     except Exception as e:
      print('Something is wrong.',e)
    if __name__=="__main__":

    Pros and Cons of a Solution Using a Sensor

    Advantages of using the sensor:

    1. Simple final code in the form of ONE Python script that implements the entire operation algorithm.
    2. The relative cheapness of the sensor.

    Of the minuses, I see so far only the possibility of failure of the sensor during prolonged use. But perhaps this device (sensor) will work stably and for a long time, despite the fact that this is not an “industrial” option.

    Additional features

    I also tried to connect a second camera. Raspberry coped with processing the video stream from two cameras without any problems.


    Using the sensor turned out to be quite simple. It remains to verify the operation of the solution with prolonged use.

    I hope that the proposed option will be a useful alternative to the implementation of a video surveillance system, which can be easily done "do it yourself."

    Also popular now: