These versions are all outdated. I strongly recommend you to use the new vesion instead!
Raspberry Pi Zero W security camera system.
Script that sends a video feed from the Raspberry Pi over the network and starts recording when there is motion. The recordings will be saved and removed when the storage gets full. The camera and the recordings can be looked at using a Django website (https://github.com/Ruud14/Django-Camera-View-And-Playback). This feature only works with version 3. Version 3 also has a nice feature where the recording always contains the a couple of frames from before the action happened that triggered the recording.
Every version has its own advantages and disadvantages but I strongly recommend you use version 2 or 3 since they are faster and are way easier to use. Version 3 isn't always better than version 2 though. It just depends on your needs.
| Version | Advantages | Disadvantages |
|---|---|---|
| V1 | - Requires less dependencies. | - Relatively slow (low fps). |
| - Over raw tcp instead of http. | - Stream is more difficult to view. | |
| V2 | - Requires less dependencies. | - Requires more processing power on the server. |
| - Requires less processing power on the Rb Pi. | - Recording fps depends on internet speed. | |
| V3 | - Higher frame rate recordings. | - Requires FFMPEG to be installed. |
| - Recording fps isn't affected by internet speed. | ||
| - Also records frames that happened before there the motion got detected. | ||
| Stores the recordings in h264 format for easier playback and less storage occupation. |
In this version the Raspberry pi sends the camera feed to the server frame by frame using raw tcp sockets. The server receives these frames and detects motion in them. When it does detect motion it starts the recording. The main disadvantage here is that it is slow and because sending frame after frame requires a lot of processing power.
How to use:
- Get yourself a Raspberry pi and a (Night Vision IR) camera module.
- Install an os and python3 + openCV.
- Put the
Raspberry Pi.pyscript on your Raspberry pi. - enable the camera in the
sudo raspi-configmenu. - You might want the script start on boot. I achieved this using crontab.
- Make a file called
data.jsonand put it in the same folder as yourRaspberry Pi.py. - In your
data.jsonadd{"server_ip":"your_server_ip", "server_port": your_port_number, "secret_key": "your_made_up_key"}and change the values. - In
Raspberry Pi.pyin thesetupfunction change/home/pi/Scripts/Camera/data.jsonto the file location of yourdata.json. - Put the
CameraReceiver.pyandCamera.pyon your server/host machine. - Also add that same
data.jsonfile from before to the folder yourCameraReceiver.pyis in. - In
Camera.pyin theCameraclass changevideo_output_folderfrom.\\Recordings\\to whatever path you want your video's to be stored in. - In
CameraReceiver.pyin the__init__method changefile = open('data.json')to the file location of yourdata.json. - You might want to change the
camera_countto the amount of camera's you're using. (The value ofcamera_countcan be more but can't be less.) - If your host machine's os has a GUI you might want to enable
cv2.imshow("Frame",frame_data)in therecv_streammethod. - If you want to use this with a django webpage you'd want to get the content of
django_views.pyand add it to your djangoviews.py. You should also changefrom .scripts.Cameras_Receiver import CameraReceiverindjango_views.pytofrom PATH_TO_YOUR_CAMERASRECEIVERSCRIPT import CameraReceiver. - Just to make sure that you don't have to change the ip's in the code every time your router resets, Reserve the ip addresses of both the host and the Raspberry pi in the settings of your router.
- Enjoy
In this version the Raspberry pi hosts a little http site with the stream of the camera on it. The server then scrapes that site and grabs the video stream. Whenever motion is detected in the video stream the server will start the recording. The main disadvantage here is that the fps of the recording is slow when the internet connection is slow.
How to use:
- Get yourself a Raspberry pi and a (Night Vision IR) camera module.
- Install an os and python3 + openCV.
- Put the
Raspberry Pi.pyscript on your Raspberry pi. - enable the camera in the
sudo raspi-configmenu. - You might want the script start on boot. I achieved this using crontab.
- Make a file called
data.jsonand put it in the same folder as yourRaspberry Pi.py. - In your
data.jsonadd{"username":"pi", "password":"picamera"}. You might want to change the values. - In
Raspberry Pi.pychange/home/pi/camera/Camera/data.jsonto the file location of yourdata.json. - Put the
CameraReceiver.pyandCamera.pyscripts on your server/host machine. - You might also want this script to start on boot, again I used crontab.
- Also add that same
data.jsonfile from before to the folder yourCameraReceiver.pyis in. - In
Camera.pyin theCameraclass changevideo_output_folderfrom.\\Recordings\\to whatever path you want your video's to be stored in. - In
Camera.pyin theCameraclass you can changemotion_sensitivityto change the sensitivity for detecting motion. - In
CameraReceiver.pychangedata.jsoninfile = open('data.json')to the file location of yourdata.json. - In
use.pychange the ip address to the ip of your camera. - Just to make sure that you don't have to change the ip's in the code every time your router resets, Reserve the ip addresses of both the host and the Raspberry pi in the settings of your router.
- Enjoy
In this version the Raspberry pi still hosts a little http site with the stream of the camera on it. It is different from version 2 because here the recording (after movement is detected) takes place on the Raspberry pi itself instead of on the server. The movement is detected on the server side and the server then sends a message to the camera to start the recording. After the recording is done the recording is sent to the server. The main advantages here are that the fps of the recording isn't affected by the internet speed and that it records frames that happen before there the motion was detected. So the action causing the camera to record will always be visible in the recording.
How to use:
- Get yourself a Raspberry pi and a camera module.
On the Raspberry Pi:
- Install an os and python3 + OpenCV + FFMPEG.
- Put everything from inside the
camerafolder on your Raspberry Pi. - enable the camera in the
sudo raspi-configmenu. - You might want the
HTTPServer.pyanduse.pyscripts to start on boot. I achieved this using crontab. - Make a file called
data.jsonand put it in the same folder as yourCamera.py,CameraReceiver.py,HTTPServer.pyanduse.py(if there isn't one already). - In your
data.jsonadd{"username":"pi", "password":"picamera"}. You might want to change the values. - In
HTTPServer.pychange/home/pi/scripts/data.jsonto the file location of yourdata.json. - In
CameraReceiver.pychangedata.jsoninfile = open('data.json')to the same location as inHTTPServer.py. - In
Camera.pyin theCameraclass changevideo_output_folderfrom/home/pi/recordings/to whatever path you want your video's to be stored in. (The recordings will get removed after sending it to the server.) - In
use.pychange the ip address to the ip of your camera/Raspberry pi. - In
Camera.pychange theserver_ipto the ip address of your server/host.
On the Server:
- Put everything from inside the
serverfolder on your server/host. - In
FileReceiver.pychange thehostin themainfunction to the same ip address asserver_ipinCamera.pyon your Raspberry pi. - In
use.pychange the ip-address to the ip of your camera/Raspberry pi. - Make a file called
data.jsonand put it in the same folder as yourdetector.py,CameraReceiver.py,FileReceiver.pyanduse.py(if there isn't one already). - In your
data.jsonadd the same content as in thedata.jsonfile of your Raspberry pi. - In
detector.pyin theCameraclass you can changemotion_sensitivityto change the sensitivity for detecting motion. - Run
use.pyandFileReceiver.py. - You might also want the scripts to start on boot, again I used crontab.
- Just to make sure that you don't have to change the ip's in the code every time your router resets, Reserve the ip addresses of both the host and the Raspberry pi in the settings of your router.
- Enjoy
What I learned:
- My first time using a Raspberry pi.
- OpenCV in python.
- Use of struct (un)packing in python.
- Use of crontab in linux.
- Use of base64
- HTTP/MJPG streaming
- FFMPEG
Part of the script running on the raspberry pi in version 2 & 3 is based on this project.
This project assumes that the Raspberry Pi and the server/host are on the same network.
The os used on the raspberry pi is 'Raspbian Buster Lite'. The OpenCV version used on the raspberry pi is '3.4.6' The ffmpeg version used on the raspberry pi is 'git-2020-01-17-c95dfe5' The python version used on the raspberry pi is 'Python 3.7.3'
