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.
Install fswebcam package
sudo apt-get install fswebcam
Enter the command
fswebcam followed by a filename and a picture will be taken using the webcam, and saved with the filename specified:
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”.
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
fswebcam -r 1280x720 image2.jpg
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.
#!/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
- “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.
- Open file Error
- USB Disconnected frequently.
- Change the camera.
- 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.