Smart CCTV Camera Tutorial – Video Streaming with Flask and OpenCV on Raspberry Pi

Story


Imagine the endless possibilities if we could make our standard CCTV Camera smarter and interactive.
Well, introducing the Smart CCTV Camera. A Raspberry Pi IoT powered Smart CCTV camera featuring face recognition, home automation, and the ability to interact with your camera’s live stream. In essence, it’s more like a DIY Ring Camera.

The smart CCTV Camera keeps tabs on your valuable assets as well as informing you of its surroundings, whether you’re indoors or outdoors.

Smart CCTV Camera - display and cctv
Watch the Full Youtube Tutorial on How to build a Smart CCTV

Taking advantage of the new Raspberry Pi High Quality Camera, the Smart CCTV Camera also features:

  1. Face Recognition – Identifying who’s at the door
  2. Camera Movement – Reach those blind spots a typical CCTV camera is limited to with a controllable servo motor.
  3. Smart Home Automation – Integrate with your Smart Appliances and Smart Plugs.
  4. Dedicated Camera Monitor Feed – Interact with who’s at the door
  5. Live Stream – Watch videos in Realtime
  6. Live Notifications – When not around, get a message at who’s at your door via email.

Whether you place the SmartCCTV indoors or on your front porch this works as your intelligent watchdog camera.

Smart CCTV Camera - raspberry pi black final
Smart CCTV Camera Build

The following article details how to build your own Smart CCTV camera using a Raspberry Pi with the new High-Quality Pi Camera – as well as its dedicated monitor. Let’s get started!

Pre-requisite: Raspberry Pi 4 (RAM 2GB+) Recommended.


Parts List – What you’ll need

Smart CCTV Camera - parts list
Smart CCTV Camera – Parts List

Main Parts (Hardware Components):

Amazon
Raspberry Pi 4 Model B (4GB +)

Raspberry Pi High Quality Camera

TBC
Raspberry Pi 6mm Camera Lens

TBC
HDMI (Micro HDMI)


Micro SD


Raspberry Pi Charger


7 inch Touchscreen Display


Software and Platforms

  • Open CV
  • Python 3.7+ & Flask
  • Face recognition and dlib

Hardware Materials:

  • Fiberboard wood
  • Hacksaw

Smart CCTV Camera - camera outdoor
Smart CCTV Camera can be placed outdoors and Indoors

Costs: The total project cost ~$180 dollars, however with a bit of research you can bring this price down. You could also make a cost-effective version using the standard Pi Camera module V2. View the video on the comparison of the two Pi Camera modules.

How it Works:

Smart CCTV Camera - raspberry pi high quality camera
Smart CCTV Camera – Using the new Raspberry Pi High Quality Camera

The smart CCTV work by capturing live footage from the camera build and using Flask to create the live stream for our client devices to view the stream. Flask is a lightweight micro web framework which is a great bridge between Python and HTML Web pages. This makes it ideal for touch screen interaction.

Smart CCTV Camera -live stream with flask
Smart CCTV Web Stream footage from Raspberry Pi HQ Cam

Devices connected to the same network of the Pi (CCTV Camera) would be able to visit the URL and see the live footage of the stream. OpenCV would be used to access our video stream. Finally, we would use a face recognition library to identify users.


Assembly of Parts:

The assembly of parts is broken into two sections:

  1. Camera Build
  2. Dedicated Display Monitor

Camera Build

The camera build consists of a Raspberry Pi 4, micro SD card, Pi High-Quality Camera, and a Plastic Dome Camera Enclosure. The Raspberry Pi is powered by a USB-C Power Supply. In essence, this is all you need to complete the build is a Pi and Pi Camera module.

Similar to the Raspberry Pi V2 Camera module the High Quality Pi Camera module ribbon cable is inserted into the Camera Port of the Pi.

Smart CCTV Camera - camera and enclosure
Smart CCTV Camera with Enclosure Box

CCTV Camera Enclosure (Optional)

The CCTV camera enclosure was made by stripping out a cheap plastic dome CCTV camera and using the plastic camera Chassis as the enclosure. Finding a CCTV enclosure is optional, however, this does help with the aesthetics. The original color of the camera came in white, the camera and enclosure were then spray painted to a matte black finish – which helped give it a stealthy look.


Servo Motor Implementation:

Smart CCTV Camera - pi hq camera with motor
Smart CCTV Camera – Servo Motor Implementation

The Smart CCTV camera won’t be as ‘smart’ if we can’t interact with it.
The question is how can we interact with the CCTV camera to make it move into known blind spots?

This is where a servo motor comes into the picture. Servo Motors are really great for precision. The MG90S servo motor allows for a full 180-degree rotation which is ideal for a camera mounted to a wall.

To mount the servo motor we would strip the wires and solder female header pins to plug into the GPIO pin of the raspberry Pi header.


Note:
These would be programmed in the software section and can be re-programmed in the servo.py file.

The following configuration.

Smart CCTV Camera - servo motor configuration
Smart CCTV Camera – Servo and Pi pin configuration
Servo Wire ColourFunctionPi 4 GPIO Pins
Red+5VPin 2
BrownGNDPin 4
OrangePWMPin 11
Pin Configuration Table

A hole can be drilled into the Camera Chassis to screw the motor in – enabling the camera to rotate when the servo rotates. Once completed, re-assemble your Smart CCTV back together.

Note: A camera enclosure was used to house the Raspberry Pi. Keep in mind to build an enclosure that has some form of ventilation to prevent the Pi from getting warm overtime.


Dedicated Monitor Display:

Smart CCTV Camera - display on live stream
Smart CCTV Camera Dedicated Display Monitor

The Smart CCTV camera live stream can be accessed from any smartphone via Web URL. However, for the times we want to view the live stream at that immediate instant without pulling out our phones every time, a dedicated touchscreen display monitor would be practical. This Interactive display monitor indefinitely shows the live stream, as well as you can interact with the camera moving left and right.

Construction of the Smart CCTV display monitor
Hardware Construction of the Smart CCTV Dedicated Monitor

The Dedicated touchscreen monitor frame was assembled using 1×4 MDF Fiberboard wood which can be found at your local DIY store. Using a hacksaw, the wood was used to cut to the exact dimensions of 13 H cm x 22.5 W cm x 4.5 D cm. The frame was then assembled and was held together with PVA glue and nails. In addition, a glossy Black finish was applied, giving a modern aesthetic look that can blend on your bookshelf.

Great! We’re on the Software part. Now the next part is installing face recognition and the dlib library.


Software


Setup of Pi Camera Module

Smart CCTV Camera - Pi Cam Enabled
Software Installation – Enabling Raspberry Pi Camera Port

Having flashed the latest Raspbian OS onto your Micro SD, and completed the setup of Pi. The next part involves installing the face recognition & dib library to the camera build Raspbian OS.

Note: This was installed on the Raspbian OS release May 2020.

After logging in via VNC and completing the Pi OS Setup Wizard. We’re going to enable the Pi Camera Port ports.

  1. The Smart CCTV uses the Pi Camera Module. Firstly, ensure that the camera port is enabled.

Open the terminal window and run:

sudo rasp-config

Select Interface Options, Camera Port and select Enabled. You can now exit. This may cause your Pi to reboot.

You can make sure that the camera module is all working well by taking a picture via the Pi camera module by running this in the below terminal command:

raspistill -o Desktop/image.jpg

This would save a picture of what your camera is viewing to the desktop as image.jpg. You can view the image.

Smart CCTV Camera - pi hq camera adjustment
Smart CCTV Camera – Pi HQ Camera Lens Adjustment

Note: If you’re using the Pi HQ camera you may need to adjust your lens if the picture is too blurry.

Smart CCTV Camera - dlib and face recognition installation
Raspberry Pi Package Installation – dlib and face recognition terminal commands

Face Recognition & dlib Installation

The Smart CCTV camera relies on a few dependencies that are required to be installed before running the lives stream. The dlib library is a pre-requisite to start using the face recognition library.  Visit the Smartbuilds.io for the terminal commands library dependencies for face recognition. Copy and paste the library commands to install the dlib library. (This also, including face recognition).

Note: The process took about ~40 minutes on the Raspberry Pi 4 (4 GB), but can take several hours on other Pi models. If unsure of the installation, you can also refer to the video tutorial.

Smart CCTV Git Installation

Once the dlib library and face recognition is installed you can now clone the Smart CCTV Git Repo. This repository contains Flask to establish live streams from Pi to other devices connected to the network.

Smart Mirror Touchscreen - Git Folder
Smart CCTV Camera Git Folder
  • Breakdown of files in the Smart CCTV repo
    • Profile Folder – Where all Face recognition profiles are stored.
    • Templates Folder – This is where the HTML webpage would be stored
    • Python script:
      • Camera.py – The script access open CV and the face recognition library to identify persons from its encoding matrix.
      • main.py – Where the Flask stream is created – importing servo.py and camera.py
      • Servo.py – This python script controls the servo motor rotating the Smart CCTV in the location desired. (Left or Right)
    • Demo Python Scripts:
      • facerec_pi_test.py – Test script to detect faces
      • face_pi_test_profiles.py – Test script to detect multiple faces.
Smart CCTV Camera - pi web stream display
Smart CCTV Camera live stream via Flask

To access the live stream with face recognition run main.py. You can start your live stream by visiting the IP address of your Pi on a browser connected to the same network. The address can be found by entering ifconfig in the terminal of the Pi.

Visit the URL on your browser: <your_pi_ip>:5000 to view your stream. (5000 being your port number)

Via the Livestream, you can now interact with your camera by rotating the camera to cover your blinds spots. Moreover, you can add additional functionalities such as light control. (That’s a whole other topic).

Smart CCTV Camera - eben face recognition
Smart CCTV Camera with Face Recognition running on Main.py

How to add a profile to the face recognition:

  • Adding a profile is as simple as placing your ‘passport-style’ in the Profiles folder as a .png file.
Smart CCTV Camera - Face recognition
Adding additional face profiles using Music Artist Stormzy

How the Face Recognition Library works

  • The camera script encodes the following face into a matrix to be matched against via the face recognition library. The script then performs a comparison of the face it sees relative to the current frame of the camera stream. (This uses the same library as the Smart Mirror Face ID Module)

Note: Picture size 100kb max – The smaller the picture is the faster the face recognition feature performs.

If you have any questions, leave a comment below or watch the full tutorial video.




Next Steps:

There you have it, your own SmartCCTV camera. If you like this project, feel free to leave comments /contribute and develop the SmartCCTV repo. You can now program the camera to theoretically perform any action based on an intent (being face recognition). Think of IR LEDs, night vision smart locks… the possibilities are endless!

2 thoughts on “Smart CCTV Camera Tutorial – Video Streaming with Flask and OpenCV on Raspberry Pi”

  1. Hi Vwsurfer,

    Absoultuey, would require a bit of programming to add an additional camera. Autotrack is also possible.
    If you do develop this, I would be interested to see how this turn out.

    P.S. Next release is a DIY Skateboard – faster than a boosted board.

    Eben.

    Reply

Leave a comment