Building your own DIY smart home security system with Raspberry Pi and OpenCV is an excellent way to monitor your home and protect your family without spending a fortune. With just a few components, you can set up motion detection, facial recognition, and real-time monitoring right from your own home.

Gathering the Necessary Hardware Components

The first step is gathering the core hardware components you'll need for your Raspberry Pi home security system:

Raspberry Pi

The Raspberry Pi acts as the brain of your security system. I recommend the Raspberry Pi 4 B model for its improved processor speed and ability to handle computer vision tasks. You'll also need a power supply and microSD card loaded with Raspberry Pi OS.

Camera Module

The official Raspberry Pi camera module enables computer vision capabilities. Choose between the regular or HQ camera depending on your budget. The camera module connects directly to the Pi over the CSI port.

Motion Sensor

A PIR motion sensor like the HC-SR501 allows motion detection. It connects to the Pi over GPIO pins. Detecting motion can trigger recording or send you alerts.

Webcam (Optional)

For facial recognition, add a USB webcam. Many AI services like OpenCV require higher resolution images, so choose at least 720p resolution. I recommend the Logitech C920 HD Pro.

Setting Up the Raspberry Pi

With your components gathered, it's time to set up the Raspberry Pi:

Enable Camera and GPIO

  1. Open Raspberry Pi configuration: sudo raspi-config.
  2. Under Interfaces, enable the camera and GPIO.
  3. Reboot the Pi to apply changes.

Connect Components

  1. Insert the camera module ribbon cable into the CSI port.
  2. Connect the motion sensor's data pin to a GPIO pin (I use GPIO 4).
  3. Connect the 5V and GND pins to power.
  4. Plug in your USB webcam if using facial recognition.

Your Pi is now ready to start developing computer vision applications!

Developing Facial Recognition with OpenCV

OpenCV is an open source library for computer vision and machine learning. We can use it to develop facial recognition for home surveillance on the Raspberry Pi.

Install OpenCV

  1. Update packages: sudo apt update && sudo apt upgrade
  2. Install dependencies: sudo apt install build-essential cmake pkg-config libjpeg-dev libtiff5-dev libjasper-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libgtk-3-dev libatlas-base-dev gfortran python3-dev
  3. Download OpenCV source code: wget
  4. Unzip and cd into opencv-4.x/
  5. Configure: mkdir build && cd build
  7. make -j4
  8. sudo make install
  9. sudo ldconfig

OpenCV is now installed and ready for developing facial recognition.

Developing Facial Recognition Logic

With OpenCV installed, we can develop the facial recognition capabilities in Python:

  1. Import OpenCV and dependencies: import cv2, os
  2. Initialize face cascade classifier: face_cascade = cv2.CascadeClassifier( + 'haarcascade_frontalface_default.xml')
  3. Start video capture: cap = cv2.VideoCapture(0)
  4. Detect faces in frames:
    while True:
    ret, frame =
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:

    cv2.imshow('Facial Recognition', frame)
    5. Show video feed with rectangle around detected faces

This basic script captures video, detects faces, and draws a blue rectangle around identified faces. Expand on it to add facial recognition by training against labeled face datasets.

Developing Motion Detection with a PIR Sensor

For motion detection, we can interface a PIR sensor with the Raspberry Pi's GPIO pins.

Connect the PIR Sensor

  1. Connect VCC to 5V pin.
  2. Connect OUT pin to GPIO 4.
  3. Connect GND to ground.

Detecting Motion with Python

To detect motion from the PIR sensor:

  1. Import GPIO: import RPi.GPIO as GPIO
  2. Set pin mode: GPIO.setmode(GPIO.BCM)
  3. Set up GPIO pin: GPIO.setup(4, GPIO.IN)
  4. Detect motion:
    while True:
    if GPIO.input(4):
    print("Motion Detected!")
    # Take action here
  5. Monitor the terminal output for "Motion Detected!" when movement triggers the sensor.

From here, you could have motion detection trigger recording, send an alert, or take any other custom action within your system.

Live Streaming Video from the Raspberry Pi Camera

To enable remote security monitoring, we can live stream video from the Pi Camera over the local network or internet.

Streaming with the Picamera Python Package

  1. Install Picamera: pip3 install picamera[array]
  2. Import: from picamera import PiCamera
  3. Initialize camera: camera = PiCamera()
  4. Set resolution: camera.resolution = (640, 480)
  5. Start recording: camera.start_recording('foo.h264')
  6. Stream over network: camera.start_recording('foo.h264', format='mjpeg', quality=23)

The stream will be accessible on your network at http://<pi_ip>:8080/?action=stream.

Streaming with ffserver

  1. Install ffmpeg and ffserver.
  2. Edit /etc/ffserver.conf with stream settings.
  3. Run ffserver in the background.
  4. Stream video feed to ffserver from PiCamera withffmpeg.

These methods allow you to view security footage from anywhere!

Bringing It All Together

With the core components of facial recognition, motion detection, and live streaming implemented, you can now combine them into an integrated DIY smart home security system!

Some ways to expand your system further:

The possibilities are endless for customizing your own Pi-powered smart security camera! With some basic Python and OpenCV skills, you can keep your home safe on a Raspberry Pi.