Capturing image uses a webcam in Raspberry Pi

Ever since this tiny, less than credit-card-sized computer, Raspberry Pi zero w, it’s appeared in the market, it has caught the imagination of every electronics and computer hobbyist around the world. The powerful Linux operating system combined with 40 input-output (I/O) pins can do many amazing things out-of-the-box. This article explains how to interface a USB camera and Wi-Fi with Raspberry Pi.It also to capture the image.

  • Install fswebcam.
  • Basic image capturing usage.
  • Set specify a resolution.
  • Set full a resolution with no banner. 
  • Bash script.
  • Python with Bash script.
  • Time lapse using CRON.

3-raspi_webcam

Install fswebcam package

sudo apt-get install fswebcam
 
Basic image capturing usage
 

Enter the command fswebcam followed by a filename and a picture will be taken using the webcam, and saved with the filename specified:

 fswebcam imagename.jpg 

After capture image the command will show like below and also automatically adjusting the size. When you  are entering wrong resolution.

--- Opening /dev/video0...
Trying source module v4l2...
/dev/video0 opened.
No input was specified, using the first.
Adjusting resolution from 384x288 to 352x288.
--- Capturing frame...
Corrupt JPEG data: 2 extraneous bytes before marker 0xd4
Captured frame in 0.00 seconds.
--- Processing captured image...
Writing JPEG image to 'image.jpg'.

The captured image is located on “home/pi”.

 

Set a specific resolution
 

The webcam used in this example has a resolution of 1280 x 720 so to specify the resolution I want the image to be taken in, use the -r flag:

 fswebcam -r 1280x720 image2.jpg 
 
Set full resolution with no banner
 

We need to add the “–no-banner” in command.

 fswebcam -r 1280x720 --no-banner image3.jpg 

Now the picture is taken at full resolution with no banner.

 

Bash Script
 
You can write a Bash script which takes a picture with the webcam. The script below saves the images in the /home/pi/webcam directory, so create the webcam subdirectory.
 
To create a script, open up your editor of choice and write the following example code.
#!/bin/bash

DATE=$(date +"%Y-%m-%d_%H%M")

fswebcam -r 1280x720 --no-banner /home/pi/webcam/$DATE.jpg

This script will take a picture and name the file with a timestamp. Say we saved it as webcam.sh in webcam folder, we would first make the file executable:

chmod +x webcam.sh

Run the bash script. first set the path then run it.

cd webcam

./webcam.sh

Python with Bash script

Run the bash script through python script.

import os
import datetime
import sys
import time
import subprocess

# read the absolute path
script_dir = os.path.dirname(__file__)
# call the .sh to capture the image
os.system('./webcam.sh')
#get the date and time, set the date and time as a filename.
currentdate = datetime.datetime.now().strftime("%Y-%m-%d_%H%M")
# create the real path
rel_path = currentdate +".jpg"
#  join the absolute path and created file name
abs_file_path = os.path.join(script_dir, rel_path)
print abs_file_path
The next post will show you the voice command to capture the image. Will soon post the content.any doubt don’t hesitate to ask me in the comment.
Issue’s:
I was trying to run the script continuously at the time I found the issue. Here, I show the error message
  • “Device resource busy /dev/video0”
  • “Unable to find the compatible palette format”

lsusb   # command to get the list of connected devices

Through that I got the web camera model – Logitech C270

After, I am trying to reproduce the problem. Below statistics I have taken to reproduce the issue.

  1. Open file Error
  2. USB Disconnected frequently.
  3. Change the camera.
  4. Power supply problem.

So, I am taking this above four points to analysis that. Then 1st point I solved. 2nd point, I am trying to reset the USB port after capturing the image. That was also failing. 3rd point, Change the high configuration camera. It’s working fine without fail.

After finding the solution. The problem was power supply is not enough for the web camera.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s