Capture Image with Webcam on Raspberry Pi


Python is an easy-to-use programming language. simplicity makes the Python favored by many people and is used as a recommendation for someone who is just starting to learn a programming language. this time we will use python to take pictures with webcam on Raspberry Pi.

For easier programming, we need to find whether there is a library in Python to handle USB cameras or webcam. it turns out I did not find it when googling :(, but there is an application available to capture images from the webcam that is fswebcam. for installing is quite easy that is by typing the following command on the terminal:

sudo apt-get update
sudo apt-get install fswebcam
once completed without error, then to capture quite easily by typing the following command:
fswebcam picture_name.jpg

if you access Raspberry Pi with SSH remote via putty on windows, then to see the picture, there are three things you need to do:

1. Enable forwarding X11.

on putty click Connection - & gt; SSH - & gt; X11, and check Enable X11 Forwarding check list. after that go to Session and type IP Address Raspberry PI and click Open.

2. Install X server on windows (Xming).

so images can appear on windows. then we need to install X Server on Windows that is Xming. Xming can be downloaded on the following link:

https://sourceforge.net/projects/xming/files/Xming/6.9.0.31/Xming-6-9-0-31-setup.exe/download

install Xming, after Xming is downloaded and run Xming application.

3. Install image viewer terminal (feh).

after the above two steps done, it is necessary to install the terminal opening application terminal, one of them is feh, to install feh can be done with the following command on the terminal Raspberry Pi:

sudo apt-get install feh

make sure there is no error in installing.

after doing the three steps above, then next that we will open the picture. for it can be done by typing the command as follows:

feh picture_name.jpg

and blah .... images will appear on your computer screen ..

If the results of all black images, usually webcam has not adapted to the environment to adjust the brightness. so need to add option -S, with adjusted to skip frame, I try with number 20 so frame 21 will be taken picture. example with -S 20 ie with the following command:

fswebcam -s 20 picture_name.jpg

After successfully capture the image, then open the image with the application feh.

feh namagambar.jpg



After successfully capture the image then we will create a shell script to capture the image. the purpose of making this shell script is so that later program python can execute this shell script to capture images from webcam. following the shell script I created:

#!/bin/bash
DATE=`date '+%Y-%m-%d-%H-%M-%S'`".jpg"
fswebcam -S 20 /home/pi/webcam/$DATE
exit 0

on the terminal you can use nano text editor to create a file. example to make shell script above I use command
nano capture.sh
after the text editor ready to fill the shell script above. after typing the script above the save with the command ctrl + o it will exit confirm the name to be saved. if you still use capture.sh then you just have to press 'y' after finished with ctrl + x. the capture.sh file created does not yet have the execution attribute. so we can not run the shell script. so that the capture.sh file has an execution attribute then it can be done with the following command:
chmod +x capture.sh
to run the shell script. with the following commands:
./capture.sh


make sure your terminal is in one directory with file capture.sh. you should be able to capture the image by running the shell script above.

then we will create a python script to run the capture.sh script. I make it as below:

import subprocess
subprocess.call(['/home/pi/capture.sh'])

I named it with capture.py. make sure the file capture.sh path is adjusted on the second line. to run the python script can with the following commands:

python capture.py

then the webcam will capture the image. and save image in folder /home/pi/webcam with format name YYYY-MM-DD-HH-MM-SS.jpg

ok. thats from me. thank you for reading :)
Capture Image with Webcam on Raspberry Pi Capture Image with Webcam on Raspberry Pi Reviewed by rayakyfa on June 14, 2018 Rating: 5

No comments:

Powered by Blogger.