Raspberry Pi Night Vision Project

The Raspberry Pi Night Vision project is now underway, at long last but first a little explanation.

The Raspberry Pi does have a camera module, the NoIR camera, it does not have the IR filter which is what allows it to be used for Night Vision with an Infrared torch. You will see in my first ever Slow Motion Night Vision that I achieved this very easily but there is a down side. The downside is that the Sony IMX219 sensor on the module is only 1/4 of an inch and for Nigth Vison we’ve been accustomed to using a 1/3 inch CCD sensor that had a better ability detect the infrared light. The solution is to use a powerful Infrared touch to allow the image to be seen.

I was asked why should I do this, the kits I build already work well so what is the point. The point is mainly education as I tend to like to show others how things can be put together.

Parts required

Raspberry Pi (testing on Raspberry Pi Zero and Raspberry Pi 3)
For the Raspberry Pi Zero advisable to purchase USB to MicroUSB OTG Converter Shim
Micro SD card
USB camera, a standard webcam can be used to start with before connecting a dedicated camera

Code of choice will be Python and OpenCV. Please note that based on my setup I use a Linux environment so some changes may be required to get a Windows setup running, however, the code base should remain similar.

The code below will get you started to get the USB camera to show an image, see the additional code below for the adding of the crosshairs.

First type:

Sudo nano video.py

This shall start the editor and create the file ‘video.py’

import cv2, time
#Setup collecting images for usb camera
video=cv2.VideoCapture(0)
#Set to a variable to allow the while statement to run
a=0
#Put the video into a frame
while True:
        a = a + 1
        check,frame = video.read()
#Show frame
        cv2.imshow("www.diy-nightvision.com", frame)
        key=cv2.waitKey(1)
#Press Q will close the window
        if key == ord('q'):
                break
#Switch off camera once 'q' is pressed
video.release()
cv2.destroyAllWindows

Once you have added your code, press Ctrl+X to exit, yes to save the changes and confirm the file name to be ‘video.py’

To test the USB camera you just have to type

python video.py

All being well your USB camera should now run.

Now we want to add the crosshairs. The code below will show a small cross-hair in the middle in neon green with an additional small circle

import cv2, time
#Setup collecting images for usb camera
video=cv2.VideoCapture(1)
#Set to a variable to allow the while statement to run
a=0
#Put the video into a frame
while True:
        a = a + 1
        check,frame = video.read()
# Horizontal line
#       cv2.line(frame, (0,240),(640,240),(57,255,20),1)
# Vertical line
#       cv2.line(frame,(320,0),(320,480),(57,255,20),1)
# small circle
        cv2.circle(frame, (320,240),10,(0,255,0), 1)

# Crosshair in the middle only

# ------Horizontal Line--------------------------------
        cv2.line(frame,(280,240),(360,240),(57,255,20),2)
# ------Vertical Line Line-----------------------------
        cv2.line(frame,(320,200),(320,280),(57,255,20),2)
#Show frame
        cv2.imshow("www.diy-nightvision.com", frame)
        key=cv2.waitKey(1)
#Press Q will close the window
        if key == ord('q'):
                break
#Switch off camera once 'q' is pressed
video.release()
cv2.destroyAllWindows