Fourth Part in my Raspberry Pi Project Series
Finally the series is approaching towards my main project. The previous posts can be seen as build up for the canon story. Anyways let’s get started.
In this post I am using the previous setup of SMTP service to send video files from Raspberry Pi camera to my mail.
Connecting Pi Camera —
Let’s see how to interface Pi camera module with our Pi and do basic things like capturing image.
The module have a ribbon connector, one side of which have the exposed tin plated connects. You need to connect the ribbon connector as explained in the images below —
To interface camera using Python, readthedocs have great explanation.
A basic video recording code looks like this —
camera = picamera.PiCamera()
camera.resolution = (640, 480)
camera.wait_recording(60) # time in seconds
h264 is the supported file format for the camera. I tried creating mp4 file but it gave this error —
camera.exc.PiCameraValueError: Unsupported format mp4
Format Conversion —
Now you cannot directly play this video, so to convert h264 format to mp4 we have to use a packager called MP4Box. To install —
sudo apt install gpacCheck —
which MP4Box and it should return the path —
/usr/bin/MP4BoxA basic command is like —
MP4Box -add video.h264 video.mp4
But as this is a shell command, you cannot use this directly. You have to use subprocess library in python —
X= "MP4Box -add video.h264 video.mp4"
File Size —
Now that we have our mp4 file, we can easily play it and mail it using SMTP service. But Gmail only allows 25MB file size to be attached. So we have to test multiple files wrt resolution and length of the video, to understand which file can be under our size constraints.
I tested 1280*720, 640*480, 320*240 resolutions, and 2–5 mins of video lengths. And I concluded that 320*240 resolution and 2.5 mins of video is 5–20 MB long, depending on the foreground and background.
Naming Convention —
As the camera is being used for surveillance, the file name should contain date-time. Using time library in python does the job —
dt = time.strftime("%Y%m%d-%H%M%S")
So when a video starts recording, that video is saved as the date-time it started at. And then the next video will start at that later date-time, in this case 2.5 mins later. So it’s easy to keep a track of video timestamps.
But then converting this file to mp4 should be like this —
X= "MP4Box -add " + dt+".h264 " + dt+".mp4"
Triggering Surveillance —
There is no point is recording video when there is no one present in the video frame. Or if you are present in the frame. In my later videos I will try to implement Motion detection using OpenCV to trigger surveillance. But for now to test a basic trigger, I have attached a switch with Pi’s GPIO pins to start the video recording.
state = GPIO.input(switch)
if(state == 1):
Attaching the video —
After recording, conversion, and taking care of naming, the video is ready to be send using attachment service uuencode, that we discussed about in last post.
X = "uuencode " + dt+".mp4 " + dt+".mp4 " + "| sendmail -v firstname.lastname@example.org"
process = subprocess.call(X, shell=True)
A good coding practice is to create logs. In case there are errors, logs make it easy to troubleshoot. A basic logging code can be implemented as given,
# Python Logging - Link
from logging.handlers import RotatingFileHandlerdebug_logger = logging.getLogger("Debug Rotating Log")
# add a rotating handler
debug_handler = RotatingFileHandler("debug.log", maxBytes=419430400,backupCount=1)
debug_logger.addHandler(debug_handler)debug_logger.debug("verbose email: ")
Final Results —
So after integrating all the above parts of code and running it, I was receiving videos at regular interval on my mail. I was easily able to download and play it. I observed that the recorded video was always 3 mins long, even though I put 2.5*60 in the function.
There are a few drawbacks in this setup.
- File size Constraints because of using Gmail attachments. I can try integrating and testing Dropbox to store the files.
- Right now I have to press the button to trigger the recording. But later on I will implement motion detection using OpenCV.
- If the camera is still then it only have a fixed frame for recording. To cover a much larger area, we would require multiple camera system and that might not be feasible. There is a concept regarding this, namely, Object Tracking.
I am going to discuss the 3rd point in my next post.
Thanks for reading and Stay Tuned!