AI-Powered Surveillance Camera System

by Orange Digital Center in Circuits > Wireless

41 Views, 0 Favorites, 0 Comments

AI-Powered Surveillance Camera System

IMG_3183.jpg
IMG_3109.jpg

This project was developed within the Orange Digital Center Morocco , a space dedicated to fostering innovation, creativity, and rapid prototyping. At the FabLab, individuals and teams have access to state-of-the-art tools, including 3D printers, laser cutters, and a variety of electronic and mechanical resources. The center provides a collaborative environment where innovators, entrepreneurs, and students can transform their ideas into tangible products. By focusing on sustainable and impactful solutions .

This project utilizes a Raspberry Pi along with a camera module to create an AI-powered surveillance system capable of real-time face recognition, object detection, and tracking. The system also features an attendance tracker and the ability to monitor people as they enter and exit a monitored space.

Features:

  1. Real-time video feed with streaming capabilities.
  2. Face recognition for attendance tracking.
  3. Object detection and people tracking.
  4. Data storage in a local database (SQLite).
  5. Web interface for live video streaming and data visualization.

Supplies

Arduino UNO.png
Arduino UNO (1).png

Materials Needed :

  1. Raspberry Pi 4 (or any Raspberry Pi with camera module support)
  2. Raspberry pi camera module V2
  3. MicroSD card (8GB or higher) for the Raspberry Pi OS
  4. External storage (optional, for saving data)

Setting Up Raspberry Pi 4

imager.png

In this step, we will set up the Raspberry Pi, install the necessary software, for your AI-Powered Surveillance System.

Preparing the Raspberry Pi

Before we begin, make sure you have the following materials:

Raspberry Pi 4 model B (Recommended)

Raspberry Pi Camera Module (or any compatible camera module)

MicroSD card (at least 32GB) with Raspberry Pi OS installed

Power supply for Raspberry Pi

HDMI cable and monitor (for initial setup)

Keyboard and mouse (for setup)

Note : To format the SDCard if needed :

Win + R, ‘cmd’ then OK

diskpart
list disk
select disk X (MicroSD)
clean
create partition primary
format fs=fat32 quick (for less then 32gb)
format fs=exfat quick (for more then 32gb)
assign
exit


Installing Raspberry Pi OS

Download the Raspberry Pi OS 64bit (Legacy) bullseye from the official website.

Use a tool like Raspberry Pi Imager or balenaEtcher to flash the image to your microSD card.

Once the image is flashed, insert the SD card into the Raspberry Pi and power it up.

Follow the on-screen instructions to set up the language, timezone, Wi-Fi connection and enable the ssh protocole.

This is a complete guide to install the raspberry pi OS :

https://www.youtube.com/watch?v=vxmO_a5WNI8

Connecting and Configuring the Camera

Check-raspberry-pi-version-and-system-info-board-model.jpg

Connecting the Camera Module


In this step, we will set up and configure the camera module for your AI-Powered Surveillance System using vnc viewer or just a monitor to connect to the raspberry pi's interface.

This is the complete guide of how to use vnc viewer :

https://www.youtube.com/watch?v=carRkTXv_8c

Connect the Camera to the Raspberry Pi:

Locate the Camera Serial Interface (CSI) port on the Raspberry Pi. It's usually near the edge of the board.

Carefully insert the ribbon cable from the camera into the CSI port, ensuring the blue side of the ribbon faces the Ethernet port.

Enable Camera Support:

Once Raspberry Pi OS is running, open a terminal window and enter the following command to enable camera support:

sudo raspi-config
  1. In the configuration menu, navigate to Interface Options, then select Camera and enable it.
  2. Reboot your Raspberry Pi to apply the changes.

Test the Camera:

After rebooting, test the camera by running the following command in the terminal:

raspistill -o test.jpg
  1. This will capture a still image and save it as test.jpg in your current directory. If the camera is working, you should see the image saved.

Setting Up Python Environment

1.3 Setting Up the Python Environment

For your AI-powered surveillance system, you will need to install a few libraries and packages. These include OpenCV for video processing, Flask for the web application, and SQLAlchemy for database management.

Install Python 3.8

This project was implemented using Python 3.8. Ensure that the correct version of Python is installed.

Update the system packages:

sudo apt update
sudo apt upgrade -y

Install Python 3.8 (if not already installed):

sudo apt install python3.8 python3.8-dev python3.8-venv -y

Set Python 3.8 as the default version:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1

Verify the Python version:

python3 --version

Create a Virtual Environment (Optional but Recommended)

For a clean environment, it’s recommended to use a virtual environment.

Install the venv module if it's not already installed:

sudo apt install python3-venv -y

Create a virtual environment (replace env_name with the name you want for the environment):

python3 -m venv env_name

Activate the virtual environment:

source env_name/bin/activate

Update Package Managers

Once the virtual environment is active, update the package managers (pip, setuptools, and wheel):

Update pip to the latest version:

python -m pip install --upgrade pip

Update setuptools and wheel:

pip install --upgrade setuptools wheel

Install Dependencies

Install the required Python packages for your project. These packages include Flask, SQLAlchemy, face-recognition, and others necessary for object detection, face recognition, and video processing.

Install the dependencies:

pip install Flask Flask-SQLAlchemy face-recognition ultralytics supervision imutils

This will install the following dependencies:

  1. Flask: A lightweight WSGI web application framework.
  2. Flask-SQLAlchemy: An extension for Flask that adds support for SQLAlchemy (used for database management).
  3. face-recognition: A library for facial recognition.
  4. ultralytics: For advanced object detection.
  5. supervision: A package for task management.
  6. imutils: A collection of utility functions for computer vision.

Face Recognition System Overview

This module allows capturing face images, training a recognition model, and identifying faces in video frames. It also tracks attendance and manages unknown faces.

What It Does ?

  1. Detects faces in real-time video feed.
  2. Recognizes known faces for logs attendance (next step).
  3. Manages unknown faces, assigning temporary IDs.

How It Works ?

  1. Database: Uses SQLite to store face encodings, names, and attendance data.
  2. Face Detection: Identifies faces, compares them to the trained database, and records the attendance.

Example Usage:

# Initialize face recognition model
face_recognition_model = FaceRecognitionModel()

# Train the model with the dataset
face_recognition_model.train(dataset_path="dataset")

# Save the model
face_recognition_model.save(method="sql", encodings_path="encodings.db")

# Load the model
face_recognition_model.load(encodings_path="encodings.db")

# Detect faces in the captured frame
detected_faces = face_model.detect(frame)

Note : This is the link to the github repository where you can find the complete code : https://github.com/marwanazd/SmartSurveillanceSystem/tree/main/modules

Attendance Monitoring Module Overview

What It Does ?

  1. Records attendance using face recognition.
  2. Retrieves attendance records (e.g., today, week, all time).
  3. Exports records to CSV, Excel, or JSON.
  4. Stores data in a SQLite database.

How It Works ?

  1. Database: Uses SQLAlchemy to manage attendance data (name, timestamp, face image).
  2. Recording: Ensures no duplicate attendance for the same person on the same day.
  3. Retrieving: Filters records by time period or custom date range.
  4. Exporting: Saves records in your chosen format and stores them in the uploads folder.

Example Usage

Initialize:

tracker = AttendanceTracker("attendance.db")

Record Attendance:

tracker.record_attendance([{"name":,"timestamp":,"face_base64":}])

Get Records:

today_records = tracker.get_attendance_records(time_period='today')

Export Records:

tracker.save_attendance_records(time_period='today', file_format='csv', file_path='today_attendance')


Note : This is the link to the github repository where you can find the complete code : https://github.com/marwanazd/SmartSurveillanceSystem/tree/main/modules

Social Distancing and People Movement Monitoring System

What It Does ?

This script provides a framework for tracking people in a monitored space, measuring social distancing, and counting how many people enter or exit the space. It uses YOLOv8 for object detection, ByteTrack for multi-object tracking, and Supervision for bounding box and label annotations. The system records person movements and interactions with defined boundaries (e.g., doors, lines) to monitor people flow.

How It's Built ?

  1. YOLO model: Utilized for object detection (people in this case). This model is loaded using the ultralytics package, and the detections are processed and tracked.
  2. ByteTrack: Used for tracking objects across frames. This ensures consistent IDs for people moving through the frame.
  3. Supervision: A package that helps with visualizing detections and tracking information, including bounding boxes and labels on frames.
  4. SQLAlchemy: Used for database management to store tracking information.
  5. OpenCV: Used for drawing bounding boxes, center points, lines, and labels on frames, as well as capturing and saving images of detected persons.

Example Usage:

# Initialize the tracker
tracker = Tracker(model='yolov8s.pt', db='trackings.db')

# Process the frame for detection, tracking, and distance calculation
annotated_frame = tracker.process_frame(frame, track_movements_flag=True)

# Detect close people (social distancing violation)
close_people = tracker.watch_distances(distances, frame, min_distance=1.0, output_folder='output')

# Count people (e.g., people entering/exiting through a line)
tracker.count(frame, line_id=1, drawing_db='lines.db', db_model=Line, plot=True)


Note : This is the link to the github repository where you can find the complete code : https://github.com/marwanazd/SmartSurveillanceSystem/tree/main/modules

Security Object Detection System

What It Does ?

This Security Object Detection System is designed to identify and track non-desired items within a monitored space, such as in a laboratory or workplace. The system uses a pre-trained YOLO (You Only Look Once) model to detect various objects (like food, smoke, violence, coffee cups, soda cans, bicycles, etc.) from a video feed captured via a camera module connected to a Raspberry Pi. Detected objects are processed and stored in an SQL database, including an image of the detected object encoded in base64 format, along with the timestamp of the detection.

How It's Built ?

Object Detection:

  1. The system uses a YOLO model to detect objects from video frames.
  2. The model provides bounding box coordinates and class IDs for each detected object.
  3. It excludes the detection of 'person' objects (to focus on non-desired items).
  4. Detected objects are captured in their bounding box and stored as base64-encoded images for efficient database storage.

Database Integration:

  1. The system uses SQLAlchemy to manage a database, where it stores details about each detected object (including the object’s class ID, label, timestamp, and image encodes it into base64).
  2. Detected objects are stored in an SQLite database, providing a historical record of all the objects detected by the system.

Real-Time and Historical Usage:

The system can detect objects in real-time via video feed.

It also allows querying historical data from the database, allowing the user to review previous detections.

Example Usage

Model Setup and Initialization:

# Initialize the object detection model
model = ObjectDetectionModel(model_name='best', db_name='surveillance_system')

Detecting Non-Desired Objects:

# Detect objects in the frame
detected_objects = model.detect_objects(frame)

Saving Detected Objects to Database:

# Save the detected objects to the database
model.save_to_db(detected_objects)


Note : This is the link to the github repository where you can find the complete code : https://github.com/marwanazd/SmartSurveillanceSystem/tree/main/modules

Web Interface

home dashboad.png
attendance.png

This web app is a robust surveillance solution that uses the Raspberry Pi, camera module, and several advanced computer vision models to monitor and track activities in a space. It provides real-time video streaming, attendance tracking, object detection, and people counting with a user-friendly web interface for managing and viewing the data. The use of Flask ensures that the system can be easily accessed via a web browser, and the multi-threading setup ensures smooth video processing without interruption.

Dashboard (Home Page)

The Home page of the AI-Powered Surveillance System dashboard provides an intuitive interface for monitoring real-time data. It features a live camera feed along with key metadata like frame rate, resolution, and uptime. The dashboard also displays important statistics such as the number of people detected, event logs, and storage usage, offering a comprehensive overview of the FabLab’s activity. With automatic updates and a clean, responsive layout, the page ensures easy access to crucial information for security and system management.

Attendace Dashboard

This page serves as the main dashboard for managing and viewing attendance records. Users can filter records by specific time periods such as today, yesterday, or a custom date range. The page allows for easy downloading of records in multiple formats (CSV, XLSX, JSON) and provides a simple form for adding new people by uploading their images for face recognition. Attendance data is displayed in a dynamic table that updates every 5 seconds, showing the ID, name, timestamp, and face image of each person detected.

Settings Page

This will allow users to configure options for different modules like Object Detection, Face Recognition, Distance Monitoring, and Attendance System.

Note : This is the link to the github repository where you can the templates code :

https://github.com/marwanazd/SmartSurveillanceSystem/tree/main/templates

Surveillance Camera 3D Model

FZ6OYO3M5B1VZFZ.jpg
FIIIIEZM5B1VZF5.jpg
FSEEDABM5B1VZGF.jpg
FR3LJAPM5B1VZFJ.jpg
FMCWGGHM5B1VZES.jpg
FO4SD8HM5B1VZEG.jpg
FGQE2IEM5B1VZE5.jpg
F1WBHQ9M5B1VZDV.jpg
FURP1BKM5B1VZGW.jpg
FBE304SM5B1VZHG.jpg

To ensure a smooth printing experience, I'll be clear and concise with these steps. I've placed images at the top bar. The instructions and filenames are listed in the same order here. While printing in this sequence is not required, I printed the V1_AV_CAM_Bottom.stl first, followed by the V1_AV_CAM_Mount.stl, which helped me place the electronics early on. Most parts don’t require support, but they’re easy to clean if you follow the guidelines below. Thanks for following my Instructable!

Lens_Mount.stl

Print with 20% infill and the holder with 50%.

Assembling the 3D Printed Parts

How to Assemble Your DIY AI-Powered Surveillance Camera Case!
FSS3JF1M5B1W0MH.jpg
FXCHVV6M5B1W0MX.jpg
F5EQ3YYM5B1W0OF.jpg
FX6RZ2HM5B1W0OZ.jpg
FS9WQW4M5B1W0RH.jpg
F4LI2IKM5B1W0NE.jpg
FBWCYDSM5B1W0NW.jpg
FX59QF8M5B1W0QT.jpg
FZXGMWUM5B1W0S6.jpg
FO9K4BDM5B1VZK0.jpg
F3HYF89M5B1W0PK.jpg
F9F9JGLM5B1W0Q6.jpg
FYQY5EWM5B1W0SW.jpg

Assembling the camera is straightforward, with most steps being self-explanatory from the pictures and rendering. Here are a few tips:

After printing, you may need to ream out the screw holes using a 1/8 inch or 2.5mm drill bit. You may also need to remove material from supports and trim the brim. Clean the edges with a needle file, as needed.

  1. First, mount the Raspberry Pi and attach the camera to it.
  2. Secure the camera in the mount by placing the board in the back. A small amount of hot glue in each corner will help hold it in place. The two screw holes at the front of the mount fit into the sides of the camera mount.
  3. Once the electronics are in place, insert the 38mm acrylic lens in front of the camera mount, and secure the lens with four M3 screws.
  4. Route the power cord through the bottom channel of the Raspberry Pi mount and plug it into the Raspberry Pi (without powering it on yet).
  5. Leave the top and back off for now, as you'll need to flash the image and set up the Raspberry Pi.
  6. Attach the base mount to the bottom of the camera with four M3 screws, then attach the mount post with the M4 screw.
  7. Finally, attach the wall mount to the base post using an M3 x 20mm screw to complete the camera assembly.


After this, you'll mount the camera once the software is ready.

Executing the Surveillance System Code

DIY AI-Powered Surveillance Camera: Build Your Smart Security System!

In this step, we will deploy and run the AI-powered surveillance system software on our Raspberry Pi with the camera module. The program is designed to detect objects, track movement, and analyze video feeds for security purposes. Once the setup is complete, your system will begin monitoring the environment autonomously, processing video in real-time and storing results in a database.

Commands to clone code from github :

cd ~
mkdir SmartSurveillanceSystem
cd SmartSurveillanceSystem
git clone https://github.com/marwanazd/SmartSurveillanceSystem.git .
ls


Commands to run the script :

conda activate env
(env) cd SmartSurveillanceSystem
(env) python app.py