[Practice 4] Raspberry Pi Camera
Фев 15, 2020 в 21:36  •  17 мин  •  читали 778 раз

Last time we finished ROS installing and optimized workflow a bit with sshfs.

We will definitely return to ROS, but today we`re gonna talk about photo and video capturing with RPi and its camera module.


Part 1


For playing with photo/video capturing we need a special device, which i mentioned above as camera module. Here it is:




We have a Raspberry Pi Zero W, so we will need another cable (the bundled one is not compatible):


The total cost of the kit I got was about 690 rubles (590 for camera and 100 for cable).


The connection is pretty clear too. On both boards there is simply no place where else we can plug this cable. The main thing is not to insert it upside down. It should turn out like this:



And now we come to the most interesting part.

Connect to the raspberry pi via ssh How to do it.


Camera is not configured by default, so we need to configure it. Run this command:

$ sudo raspi-config


Then we will see an configuration tool:


Go to Interfacing Options > P1 Camera > And switch the appropriate setting to Yes


Now we must reboot for the changes to take effect. We can just unplug the usb cable and plug it again or we can run this command:

$ sudo reboot


The SSH session will drop off, this is normal behavior.

Now we need to connect again via SSH.


We can do a little check:

$ vcgencmd get_camera


Expected result is:

supported=1 detected=1


Now we can see how it works:

$ raspistill -o /home/pi/test.jpg


Also, this command supports specifying delay before capturing via -t <time in ms> option.

Result:



BTW, It`s my dog. His name is Milk (because he`s white just like milk)

"Well, anybody can take a picture" - you say, "What about the video?"


Part 2


The RPi has the capability to record video on the camera. Similarly, using just one command:

$ raspivid -t 1000 -o /home/pi/test.h264


This command will record a video named test.h264 with a duration of 1 second. As you can see, everything is quite simple. However, it is very rare for a video to come in handy as a recorded file. Much more often we want to stream video. And we will talk about this now.

This command records a video named "test.h264" with a duration of 1 second (1000 milliseconds).

As you can see, everything is quite simple. We took a photo, recorded a video, now let`s talk about video streaming.


Add a repo:

$ curl http://www.linux-projects.org/listing/uv4l_repo/lpkey.asc | sudo apt-key add -
$ echo 'deb http://www.linux-projects.org/listing/uv4l_repo/raspbian/stretch stretch main' | sudo tee -a /etc/apt/sources.list


Take a look to the link. Yes, It`s version for Raspbian Stretch. Alas, there is no version for Raspbian Buster. But trust me, it works too.


You can check the version here


Install uv4l, uv4l-raspicam, uv4l-raspicam-extras and uv4l-webrtc packages:

$ sudo apt-get update 
$ sudo apt-get install uv4l uv4l-raspicam uv4l-raspicam-extras uv4l-webrtc


Now when we restart the RPi, if you navigate to <raspberry IP address>:8080 in your browser, you will see the webrtc gui. Let's try.



My board`s address is 192.168.0.108. I go the address and see this:



If we click on the second button, we will go to the stream:


BTW, I changed my settings because RPi is too weak for such a task, so stream frame you see above is 320x240 resolution and about 20 fps.


Now let`s try to write a code in python.


Part 3


Firstly, check the python availability:


If you don`t have python, you must install it.

$ sudo apt-get install python3


Also, we need picamera package

$ sudo apt-get install python-picamera


And now it`s time to code:

%lang(python)%
from picamera import PiCamera
from time import sleep

camera = PiCamera()
sleep(3)
camera.capture('/home/pi/pyimage.jpg')


Save a file and run it via python:

$ python test.py


after 3 seconds the file will appear

After 3 seconds the file pyimage.jpg will appear in /home/pi directory. We did it!


That`s all for today. No doubt we will return to this topic, because 320х240 stream is not cool at all!

Thank you for reading!

Копирование материалов допускается только с разрешения автора (vladivanov.dev@gmail.com) в письменной форме.
(Copying of materials is allowed only with the written permission of the author)
Похожие статьи