We stream video from a webcam connected to a Raspberry Pi. Raspberry Pi. Connecting and working with a CSI camera How to connect an analog camera to raspberry pi

Brief instructions for connecting and using a hardware camera for Raspberry Pi Please note that the camera may be damaged by static electricity. Before removing the camera, it is recommended to touch a grounded object (for example, a radiator) to remove the charge. The camera is connected to the board using a flexible cable. The cable connector is located between the Ethernet and HDMI ports. The camera lens may be covered with a transparent protective film, which must be removed before use.

Enable camera support in Raspbian

Boot the Raspberry Pi and log in (default username is pi, password - raspberry). In the terminal, run the commands to update the Raspbian distribution to the latest version: sudo apt-get update

sudo apt-get upgrade

Camera software

raspivid console application for recording video from a camera.

raspistill Applications for receiving images from the camera parameters -o or –output specifies the output file name -t or –timeout video recording duration (default 5 seconds) -d or –demo recording in feature demonstration mode. All possible effects will be used

Examples: raspistill -o image.jpg- image capture in .jpg format

raspivid -o video.h264- capture 5 second video in h264 format

raspivid -o video.h264 -t 10000- capture 10 second video in h264 format

raspivid -o video.h264 -t 10000 -d- capture 10-second video in h264 format in demo mode

You can view all possible parameters for the raspivid and raspistill applications as follows: raspivid | less, raspistill | less

Use the cursor to move through the list or enter "q" to exit. Detailed descriptions of parameters for the camera software can be found

Streaming video over the network

To transfer video, run the command on the Raspberry Pi

raspivid -t 999999 -o - | nc 5001

To watch video on the client, you need to install mplayer and netcat and run them as follows: nc -l -p 5001 | mplayer -fps 31 -cache 1024 for Linux


The Raspberry Pi single board computer allows you to create really interesting and useful things: from multimedia centers to home automation systems. By connecting various modules to the Raspberry Pi, you can significantly expand the functionality of this minicomputer.



One of these additional plug-ins is a camera, which allows you to take pictures or shoot videos. Today there are a number of cameras for Raspberry Pi on the market, and in this article we will give a short review of them.


Let's look at six of the most popular cameras: ZeroCam Noir, ZeroCam FishEye, Raspberry Pi Compatible Fisheye Camera, Raspberry Pi Camera V2, Raspberry Pi Camera V2 Noir and Raspberry Pi Camera 1.3.



ZeroCam Noir is a camera module for Raspberry Pi Zero or Raspberry Pi Zero W, so if you want to use it in Raspberry Pi 3 or 2, you will need to use an adapter cable. This camera does not have an IR filter on the lens, making it ideal for low-light photography. Here are some of its main characteristics: 5 megapixel sensor, 2592 × 1944 pixels, 1080p at 30 FPS (or 60 FPS at 720p, 90 FPS at 480p), focal length 3.60 mm, 53.50 degrees horizontal, 41.41 degrees vertical, approx. board dimensions with camera: 60 x 11.4 x 5.1 mm.



This is the fisheye version of the ZeroCam, which means it has a wide-angle image. This camera is also made for Pi Zero or Pi Zero W, so to use it with another Pi panel you need an adapter cable.



This is a Raspberry Pi compatible fisheye camera that can be easily found on various online trading platforms such as AliExpress, TaoBao, eBay. It is characterized by a wide-angle view of 175º. It is based on the Omnivision 5647 sensor with a resolution of 5 megapixels (2592 x 1944 pixels).



This camera is equipped with an 8-megapixel Sony IMX219 image sensor with a fixed focal lens, capable of displaying 3280×2464 pixel static images, it supports 1080p30, 720p60 and 640×480p90 videos. The camera is compatible with all Raspberry Pi boards, but if you want to use it with the Pi Zero, you need an adapter cable.



This camera has all the features of the Raspberry Pi Camera V2 module, but it does not have an IR filter. This means that it is almost an ideal camera for shooting in the dark.



Raspberry Pi Camera 1.3 is the predecessor to the V2 module. It is equipped with a 5-megapixel OmniVision OV5647 sensor.

Comparison of field of view and image quality of cameras for Raspberry Pi

In this test, all cameras are installed at a distance of 1 meter from the test image. The results are as follows:











Comparison of image quality and color rendition of Raspberry Pi cameras when zooming











Comparison of shooting quality at night

The results below demonstrate the performance of several night-capable cameras capturing the same test image in very low light in the dark.







conclusions

All cameras perform a little better than expected from such cheap modules. Unfortunately, there's no all-in-one camera among them, and there's a trade-off to be made as there don't seem to be any wide-angle (fisheye) cameras with the IR cut filter removed. So if you want a wide angle, you'll need regular lighting, and conversely, you're unlikely to be able to shoot a wide angle at night.

Among the interesting additions to the Raspberry Pi is a video camera module designed to connect to the CSI bus using a ribbon cable.

The camera board itself is tiny. It measures only 25x20x9 mm, weighs only 3 grams and comes with a 15 cm ribbon cable.


The camera sensor is 5 MP, has a fixed focus lens and allows you to take photos with a resolution of 2592x1944. Video support: 1080p 30 frames per second (fps), 720p at 60 fps and 640x480 resolution at up to 90 fps. Full camera support has been added to Raspbian OS, which needs to be updated.

Say "chee-ee-iz" for Wheezy

Before moving further, you need an Internet connection. The camera module requires Raspian OS to be updated to the latest version so that the necessary drivers and tools can be installed.

If this is not a problem, then we can install the equipment.

Please note that the camera may be damaged by static charge. Before removing its gray anti-static bag from the camera, please ensure that you have eliminated your charge by touching a grounded object (such as a radiator or water pipe).

For the camera, the dedicated bus is the ribbon connector closest to the Ethernet port. It has a zero insertion force (ZIF) design: you only need to tighten the two side clamps to release the bracket.

You need to place the introduction tape from the edge of the connection near the Ethernet port to the end of the Pi's power connector. Hold the tape square in place and move the tabs apart to lock the tape in position. Please note in the photo that the flexible cable is inserted with silver contacts towards the HDMI port:


Connect everything to the Raspberry Pi and boot from the SD card, choosing to install the stock Raspbian "Wheezy" OS, then reboot. The default username and password are "pi" and "raspberry", respectively.

First we have to update the OS, to do this, open a terminal and enter the following two commands, pressing the return key after each one. They may take quite a few minutes to complete, so you might want to grab a cup of tea.

sudo apt-get update

sudo apt-get upgrade -y

Now we need to use the Raspberry Pi tuning tool to check the GPU memory allocation because we need at least 128 as a buffer for all the video data that is about to be flushed into memory. This actually activates the camera bus. Enter in terminal:

sudo raspi-config

First select "Advanced > Memory_split" - it should already be set to 128, but if not, make changes. The camera system seems to work fine even at 16, but 128 is recommended.

Finally, there is an option to support the camera Enable/Disable Camera (turn on/off the camera), select Enable (enable):



Select Finish (done), and then Reboot to reboot:


Shooting with Raspberry Pi

There are two command line tools to access the camera module - these are raspivid And raspistill .

The camera offers a good selection of graphic effects and general camera settings that can be applied live, both for photos and video. You are able to change the modes white balance, focus, photo type, exposure, ISO level and EXIF ​​data.

Below are example commands from the terminal and what they will do, the default capture time is 5 seconds, use the -t option to specify a longer period in milliseconds, so -t 20000 means "for 20 seconds".

Video

Displaying five seconds in demo mode:raspivid -d

Shows 10 s video in demo mode: raspivid -o video.h264 -t 10000 -d

Displaying 640x480 preview: raspivid -p 0,0,640,480

Capture 20 seconds of video in h264 format: raspivid -t 20000 -o video.h264

Using Image Options in a Command raspivid

Syntax: RaspiVid[options]

Help: Information for help
. Default 1920
. Default 1080
-b , - bitrate: The specified bitrate. Use bits per second (for example, 10Mbits/s would be -b 10000000 )
-o , - output: Output file (to write to stdout, use "-o - ")

-t , - timeout: Time (in ms) to take photos and turn off. If not specified, set to 5 seconds

-fps - frame rate: Set the frame rate per second for recording
-e , - penc: Show preview images *after* encoding (show compression artifacts)

<"x,y,w,h">

Photo

Capture an image in JPEG format: raspistill -о image.jpg

Make a photo 640x480: raspistill -o image.jpg -w 640 -h 480

Make a JPEG of reduced quality: raspistill -o image.jpg -q 5

Using Image Options in the RaspiStill Command

Syntax: RaspiStill [options]

Image Command Options

Help: Information for help
-w , -width: Set the width of the image
-h , - height: The specified image height
-q , - quality: Set JPEG quality<0 до 100>
-r , - raw: Add raw bayer data to JPEG metadata
-o , - output: Output file (to write to stdout, use "-o -"). If not specified, the file is not saved
-v , - verbose: Print verbose while running
-t , - timeout: Time (in ms) to take a photo and turn off (if not specified, then set to 5 seconds)
-th , - thumb: Set thumbnail parameters (x:y:quality)
-d , - demo: Start demo mode (loop through a number of camera parameters, no capture)
-e , - encoding: Encoding used for the output file (JPG, BMP, GIF, PNG)
-x , - exif: EXIF ​​tag to apply to captures (format as "key=value")
-tl , - timelapse: Interval shooting mode. Takes a photo every ms

Preview Command Options

P, - preview: Preview window options<"x,y,w,h">
-f , - fullscreen: Fullscreen viewing mode
-n , - no preview: Do not show preview window

Additional command options for video and image

Sh , - sharpness: Sets the image sharpness (-100 to +100)
-co , - contrast: Set the image contrast (-100 to +100)
-br , - brightness: Set the image brightness (0 to 100)
-sa , - saturation: Set the image saturation (-100 to +100)
-ISO , -ISO: Specified ISO for capture
-vs , - vstab: Enable video stabilization
-ev , -ev: Set exposure compensation
-ex , - exposure: Set exposure mode
-awb , -awb: Set AWB mode
-ifx , -imxfx: Set image effect
-cfx , - colfx: Set color effect (U:V)
-mm , - metering: Set exposure metering mode
-rot , - rotation: Rotate the image (0-359)
-hf , - hflip: Horizontal flip
-vf , - vflip: Vertical flip

The default folder for saving images is "/home/pi" so if you just need to play the saved video, it's easier to boot into the GUI with startx, open the "File Manager" and use "LXTerminal" for camera commands.

How to Stream Video from Raspberry Pi

To watch the channel on Linux

Install the required software by running the following command from the terminal:

Good day!

On New Year's Eve, I had the idea to build some kind of video surveillance. I had everything I needed on hand:

  • Raspberry Pi Model B single board computer
  • Webcam LOGITECH HD Webcam C270
After reading, I decided to develop the author’s idea a little.
Acquaintance
So, first, let's get acquainted with the main “component”:
Raspberry Pi appearance:

Characteristics:

  • Broadcom BCM2835 700MHz ARM1176JZFS processor with FPU and Videocore 4 GPU
  • GPU provides Open GL ES 2.0, hardware-accelerated OpenVG, and 1080p30 H.264 high-profile decode
  • GPU is capable of 1Gpixel/s, 1.5Gtexel/s or 24GFLOPS with texture filtering and DMA infrastructure
  • 512MB RAM
  • Boots from SD card, running a version of the Linux operating system
  • 10/100 BaseT Ethernet socket
  • HDMI video out socket
  • 2 x USB 2.0 sockets
  • RCA composite video out socket
  • SD card socket
  • Powered from microUSB socket
  • 3.5mm audio out jack
  • Raspberry Pi HD video camera connector
  • Size: 85.6 x 53.98 x 17mm"
pi@hall-pi ~ $ cat /proc/cpuinfo processor: 0 model name: ARMv6-compatible processor rev 7 (v6l) BogoMIPS: 2.00 Features: swp half thumb fastmult vfp edsp java tls CPU implementer: 0x41 CPU architecture: 7 CPU variant : 0x0 CPU part: 0xb76 CPU revision: 7 Hardware: BCM2708 Revision: 000e Serial: 000000005a82c372

A list of officially supported distributions can be found. I chose Raspbian without a graphical shell.

The installation process is quite simple and does not need a detailed description, so I will list the main facts that are worth paying attention to:

  1. Setting the time zone
  2. Setting the computer name
  3. Enabling SSH access
  4. System update
After completing all the necessary settings, you can begin.
Preparation
First, let's install all the necessary packages:
sudo apt-get install imagemagick libav-tools libjpeg8-dev subversion
Then download and assemble mjpg-streamer:
sudo svn co https://svn.code.sf.net/p/mjpg-streamer/code/mjpg-streamer/ mjpg-streamer cd mjpg-streamer make
Because We will store all data in the cloud, we will set up work with a remote file system via WebDAV:
sudo apt-get install davfs2 sudo mkdir /mnt/dav sudo mount -t davfs https://webdav.yandex.ru /mnt/dav -o uid=pi,gid=pi
In order not to enter your username and password every time, you need to add them to the file
/etc/davfs2/secrets
/mnt/dav user password
The working process
Let's add commands to /etc/rc.local to mount WebDAV and run the script for broadcasting to the network:
mount -t davfs https://webdav.yandex.ru /mnt/dav -o uid=pi,gid=pi cd /home/pi/mjpg-streamer && ./mjpg_streamer -i "./input_uvc.so" -o "./output_http.so -w ./www"
Now, by going to http://:8080/ we will get access to the camera. All that remains is to forward the port on the router and you can access the camera outside the local network.
Creating timelapse video
First of all, we need to get an image from the camera. Because it is already occupied (the image is broadcast by the web server), then we will use the opportunity to receive the current image from the web server:
curl http://localhost:8080/?action=snapshot > out.jpg
If we want to draw the date of the photo on the image, then we can use the convert command
timestamp=`stat -c %y out.jpg` convert out.jpg -fill black -fill white -pointsize 15 -draw "text 5.15 "$(timestamp:0:19)"" out_.jpg
Full version of the script:
#!/bin/bash filename=$(perl -e "print time") foldername=$(date --rfc-3339=date) curl http://localhost:8080/?action=snapshot > $filename timestamp=` stat -c %y $filename` mkdir /mnt/dav/out/$foldername convert $filename -fill black -fill white -pointsize 15 -draw "text 5.15 "$(timestamp:0:19)"" /mnt /dav/out/$foldername/$filename.jpg rm $filename
The video is assembled using the avconv command:
avconv -r 10 -i %06d.jpg -r 10 -vcodec mjpeg -qscale 1 out.avi
Full version of the video assembly script:
#!/bin/bash filename=$(date --rfc-3339=date) i=0 for f in `ls -tr /mnt/dav/out/$filename/*.jpg 2>/dev/null` do newf=`printf %06d $i`.jpg echo $f "-->" $newf mv $f $newf i=$((i+1)) done rmdir -R /mnt/dav/out/$filename/ avconv -r 10 -i %06d.jpg -r 10 -vcodec mjpeg -qscale 1 /mnt/dav/$filename.avi rm *.jpg
Now all that remains is to register the execution of scripts in the Cron scheduler:
* * * * * pi bash /home/pi/cam.sh 59 23 * * * pi bash /home/pi/build.sh
Example video
Conclusion
This approach helps eliminate the need to spend a lot of time watching videos, and also reduces the cost of the final product. Thanks to the presence of a full-fledged OS, it becomes possible to expand the functionality in the right direction.

One of the most popular Raspberry Pi projects is creating a video surveillance server with the ability to broadcast to the Internet. Many people use these systems as security systems (myself included), but there are other uses as well. If you come up with something interesting, you can write about it in the comments. A project like this would cost around £60-£70, but I used some extra components that I had lying around (and ended up costing nothing). I used Logitech Quickcam webcam in my project.

What we need:

  • Raspberry Pi
  • SD card 8 GB or more
  • Webcam
  • Internet access using Ethernet or WiFi adapter
  • USB hub with external power supply
  • Keyboard
  • Mouse (for WiFi settings)

So, let's start by installing the latest version of Debian "Wheezy" on the SD card, which you can. To install the image on a Windows computer, you can use the utility, and for Mac computers, use this program. (More about installing images)

After copying the image is complete, insert the SD card into the Raspberry Pi. We connect the network cable or wireless adapter, as well as the keyboard. Finally, don't forget to connect the USB hub and supply power to it.

After turning on the Raspberry Pi, set the time zone according to your location using the utility raspi-config.

Please note that to log in to a freshly installed system you must use your username pi with password raspberry.

If you are using a WiFi network connection:

To configure a wireless adapter and connect to WiFi using a graphical interface, you must enter the command:

After this, you can set up a wireless connection. When finished, click the mouse in the lower left corner of the screen and select Log out to return to the command line.

Now it's time to update the system. Type in the command line:

Sudo apt-get update sudo apt-get upgrade

and wait for completion. Now you need to install Motion - software that will broadcast video using the built-in web server. To do this, enter:

Sudo apt-get install motion

and wait. Once installation is complete, connect the webcam to a powered USB port on your hub.

Now you need to make changes to the Motion program configuration file. To do this, run the command:

Sudo nano /etc/motion/motion.conf

An editor will open with a configuration file to which you can make changes. The most important are these parameters:

Daemon = OFF (correct to ON - this parameter is somewhere at the beginning of the file) webcam_localhost = ON (correct to OFF - this parameter is somewhere at the end of the file)

Press Ctrl + X to save, then 'y' to confirm the operation and finally Enter to specify the file name.

Now you can start the Motion server:

Sudo service motion start

Wait about 60 seconds, then enter the Raspberry Pi's IP address into your browser. The best browser choice is the latest version of Firefox.

The address looks like this: 192.168.X.X:8081

Make sure port 8081 is listed at the end of the line to see the image. To access settings, use port 8080 instead of 8081.

Port 8081 – video Port 8080 – configuration web interface

Please note that you will only be able to access the camera within your local network. You will not be able to connect to your home video server from another location. To connect from outside, set up incoming connection redirection on your home router.

Congratulations! You've just set up your own CCTV server on your Raspberry Pi! I wish you success and feel free to share this article!

Note: if the video server shows a gray rectangle instead of an image, enter the ls /dev/video command in the terminal to determine the name of the video device. By default, /etc/motion/motion.conf is set to video0 . If your webcam has a different name, correct the configuration file, then restart Motion.

Attention! You repeat everything described in this article at your own peril and risk! I am not responsible for any consequences. This article is a guide only.