Skip to content
This repository has been archived by the owner on Mar 4, 2023. It is now read-only.

Motion detecting by home camera and sending notification

License

Notifications You must be signed in to change notification settings

saratoga8/home_cam_detection

Repository files navigation

Detecting a motion by camera and notifying user

Node.js CI

Regular camera can be connected to you computer and used for detecting any motion. When motion detected, notification will be sent to the user by messenger(e.g. Telegram or Slack) It can be run on any computer with Linux OS. The optimal case is to run on a single board computer like Raspberry Pi

Table of contents

Technologies

The project developed with:

The project tested with:

Requirements

  • Linux OS
  • Telegram bot
  • Connected camera

Getting started

  1. Install Motion
  2. Install NodeJS by NVM or by your Linux package manager
  3. Download the project and install it from its directory npm install --only=prod
  4. In the resources/motion.conf is the configuration file of Motion used in the project. If you camera device file is not /dev/video0, update the value of videodevice in the file. More information about the configuration file can be found here. The values of the file used in the project
  5. Edit file resources/detections.yml in the project directory. See this
  6. Create Telegram bot and take its API token. See this
  7. Add the taken API token to the resources/io.yml(Telegram bot settings)
  8. From project's directory run: npm start &
  9. From the created Telegram bot send text hello (Telegram bot commands)
  10. For stopping/starting motion detecting use commands start/stop
  11. To stop run: npm stop

Detections settings of the project

The settings are in the file resources/detections.yml. Except motion all the values are default and can be left as they are

Paths:

  • motion - path to the motion program(use command which motion)

Extensions:

  • img - Extension of image files with detections, same as in motion.conf
  • video - Extension of video files with detections(depends on supported by Motion and the messenger. E.g. Telegram support only MP4), same as in motion.conf

  • max_saved_imgs - Directory of detections is being cleaned periodically. All files removed except the given number of the oldest ones
  • max_saved_videos - The same as the previous, just for video files
  • new_imgs_threshold - Notification will be sent only if there are number of detection images more then the given threshold
  • seconds_between_detections - Time between detections. In the case of multiple detections(e.g. smth. moves constantly against camera), to avoid constant notifications there is period of delay between detections. E.g. detections of 15 images will be sent every 2 seconds

Telegram bot settings of the project

The settings are in the file resources/io.yml. The file contains different Input/Output instances for notifications. E.g. Telegram or CLI(for tests only). Only one instance should be used

Telegram:

  • use - Should the instance be used for notification (value: yes/no)
  • token - API token of created Telegram bot
  • msg_type - type of notification message(value: image/video) (For weak computers, like Raspberry Pi or slow connections image should be used)

Cli (for tests only):

  • use - Should the instance be used for notification (value: yes/no)

Telegram bot commands

  • help - print all commands
  • start - start detecting motions
  • stop - stop detecting motions
  • hello - initialize connection to bot(use just after creating bot)

Motion configuration variables used in the project

Be careful with editing of the variables it can affect program's run

  • process_id_file - motion/motion.pid (change ${MOTION_CONF_PATH} by the real path)
  • logfile - motion/log/motion.log
  • videodevice - Path to your camera device, usually it is /dev/video0 (if there is only one camera)
  • max_movie_time - Maximal time in seconds of created movie. Shouldn't be too long, 10 seconds is pretty enough
  • output_pictures - If you don't want to get notifications with pictures(only videos), set it off
  • ffmpeg_output_movies - If you want to get notifications with videos only, set it on
  • ffmpeg_video_codec - For Telegram mp4(others haven't checked)
  • target_dir - Directory for saving images/videos of motion detections, set it motion/detections
  • on_movie_start - Set it rm -f /tmp/video.finished
  • on_movie_end - Set it touch /tmp/video.finished

How to run in Docker?

From the directory of the project build the docker image:

docker build -t ${IMG_NAME} .
docker run -d --device=${VIDEO_DEV_PATH}:/dev/video0 --name ${CONTAINER_NAME} -ti ${IMG_NAME}
docker exec -ti ${CONTAINER_NAME} bash

${VIDEO_DEV_PATH} - path to the video device(e.g. /dev/video0) ${IMG_NAME} - name of docker image ${CONTAINER_NAME} - name of docker container

How does it work?

The program starts Motion as a child process, the Motion uses motion directory in the project for logging and saving detection files(images/videos). Motion uses its config file resources/motion.conf. Configuration of detections is in resources/detections.yml If there are detection files in the motion directory, the files will be sent to the messenger configured in resources/io.yml

If it has stuck

Try npm stop or from the directory bin ./stop.sh If it is still stuck killall motion; killall node

Planned features

  • Auto stop/start of motion detecting by pinging of user's phone device (E.g. if the device isn't reachable then detecting will be started)
  • Scheduled stop/start of motion detecting (E.g. every weekend stop motion detecting)
  • Slack support
  • Support running in a cloud in the case of web camera use