3.Installing OpenCV Package

Once you have OpenCV installed in your RPi let’s test to confirm that your camera is working properly.

I am assuming that you have a PiCam already installed and enabled on your Raspberry Pi.

Enter the below Python code on your IDE:

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height
while(True):
ret, frame = cap.read()
frame = cv2.flip(frame, -1) # Flip camera vertically
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cv2.imshow('frame', frame)
cv2.imshow('gray', gray)

k = cv2.waitKey(30) & 0xff
if k == 27: # press 'ESC' to quit
break
cap.release()
cv2.destroyAllWindows()

The above code will capture the video stream that will be generated by your PiCam, displaying both, in BGR color and Gray mode.

Note that I rotated my camera vertically due the way it is assembled. If it is not your case, comment or delete the “flip” command line.

You can alternatively download the code from my GitHub: simpleCamTest.py

To execute the script, enter the command:

python simpleCamTest.py

To finish the program, you must press the key [ESC] on your keyboard. Click your mouse on the video window, before pressing [ESC].

 

The above picture shows the result.

Some people found issues when trying to open the camera and got “Assertion failed” error messages. That could happen if the camera was not enabled during OpenCv installation and so, camera drivers did not install correctly. To correct, use the command:

sudo modprobe bcm2835-v4l2

You can also add bcm2835-v4l2 to the last line of the /etc/modules file so the driver loads on boot.

Comments

Popular posts from this blog

1.Face Detection