Raspberry Pi - Set up Camera

Subscribe Send me a message home page tags


#raspberry pi  #camera  #opencv  #video 

In this post, we document how to set up the camera on Raspberry Pi.

System Configuration

Set Up the Camera

I followed many instructions mentioned in different blogs and I don't know which one (or the combination of them) makes it work.

1. Enable (Legacy) Camera in raspi-config

First, download the latest raspi-config. The complete list can be found here. The version I downloaded is raspi-config_20220506_all.deb. To download and install the library, we can use the following commands:

cd ~/workspace/raspi-config-installation
wget https://archive.raspberrypi.org/debian/pool/main/r/raspi-config/raspi-config_20220506_all.deb -P ./
sudo apt -y install libnewt0.52 whiptail parted triggerhappy lua5.1 alsa-utils
sudo apt install -fy
dpkg -i ./raspi-config_20211019_all.deb

After the library is installed, enter sudo raspi-config. A GUI will show up and we need to select Interface Options and then press Enter.

2. Update Raspberry Pi Firmware

Please be careful with the commands in this subsection as they may have side effects. If something goes wrong, it may be very hard to recover and I personally don't think this step is necessary but I executed the commands anyway.
curl -L --output /usr/bin/rpi-update https://raw.githubusercontent.com/Hexxeh/rpi-update/master/rpi-update && chmod +x /usr/bin/rpi-update
sudo rpi-update
echo 'SUBSYSTEM==\"vchiq\",GROUP=\"video\",MODE=\"0660\"' > ./tmp_video
sudo mv ./tmp_video  /etc/udev/rules.d/10-vchiq-permissions.rules
usermod -a -G video ubuntu

3. Enable the camera in boot config.

Add the following line to /boot/firmware/config.txt file.

start_x=1

Reboot the Raspberry Pi with the camera module connected. Then do an update

sudo apt-get update
sudo apt-get upgrade

Test Camera Setup

At this point, we should see video0 item in the output of ls -l /dev. For example

ubuntu@ubuntu:~$ ls -l /dev | grep video
crw-rw----  1 root   video    29,   0 Apr 21 12:54 fb0
crw-rw----  1 root   video    29,   1 Apr 21 12:54 fb1
crw-rw----  1 root   video   234,   0 Apr 21 12:54 media0
crw-rw----  1 root   video   234,   1 Apr 21 12:54 media1
crw-rw----  1 root   video   236,   0 Apr 21 12:54 vchiq
crw-rw----  1 root   video   240,   0 Apr 21 12:54 vcio
crw-rw----  1 root   video    10,  60 Apr 21 12:54 vcsm-cma
crw-rw----+ 1 root   video    81,   6 Apr 21 12:54 video0
crw-rw----+ 1 root   video    81,   4 Apr 21 12:54 video10
crw-rw----+ 1 root   video    81,   5 Apr 21 12:54 video11
crw-rw----+ 1 root   video    81,   7 Apr 21 12:54 video12
crw-rw----+ 1 root   video    81,   0 Apr 21 12:54 video13
crw-rw----+ 1 root   video    81,   1 Apr 21 12:54 video14
crw-rw----+ 1 root   video    81,   2 Apr 21 12:54 video15
crw-rw----+ 1 root   video    81,   3 Apr 21 12:54 video16

The raspistill and raspivid command should work too, although I forgot how I installed them.

raspistill -w 640 -h 480 -q 80 -o test.jpg
raspivid -w 640 -h 480 -o ./video.h264

Control Camera via OpenCV

We can control the camera via openCV too. Here is a demo code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import cv2
import time
# open camera
cap = cv2.VideoCapture('/dev/video0', cv2.CAP_V4L)

# set dimensions
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cap.set(cv2.CAP_PROP_FPS, 20)

# take frame
while True:
    startT = time.time()
    ret, frame = cap.read()
    endT = time.time()
    print("Time spent on capturing image: {}, ret = {}.".format(endT - startT, ret))

# write frame to file
cv2.imwrite('/home/ubuntu/workspace/tmp/test_image_1.jpg', frame)
# release camera
cap.release()

----- END -----

If you have questions about this post, you could find me on Discord.
Send me a message Subscribe to blog updates

Want some fun stuff?

/static/shopping_demo.png