Reputation: 15
I want to use Opencv3 with my raspberryPi, but when I open the camera, and I test it, it's right that, don't work
import cv2
cap = cv2.VideoCapture(0)
while 1:
if cap.isOpened():
print("Work")
else:
print("Don't work)
Upvotes: 0
Views: 1396
Reputation: 207345
In general, if you want to use the camera on a Raspberry Pi, you should probably do the following:
Step 1 - Enable camera in raspi-config
Start raspi-config
with:
sudo raspi-config
and find the command to enable the camera - it differs from version to version. Enable the camera and allow the reboot.
Step 2 - Update the Raspberry Pi firmware
It's generally a good idea to ensure that the firmware is up-to-date so the latest, greatest kernel and drivers work.
sudo rpi-update
A reboot is required after that.
Step 3 - Check the camera can shoot still images
You can check this with raspistill
to take a photo as follows:
raspistill -o picture.jpg
Step 4 - Configure v4l2
If you additionally want to use OpenCV and shoot video, you will need to ensure that the v4l2
"Video for Linux" module is installed. You can do that with:
sudo modprobe bcm2835-v4l2
If that loads correctly, your should be able to access video from the camera in OpenCV. Rather than repeating the command after every boot, you can add a single line to the bottom of /etc/modules
so it looks like this:
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# SETCHELL v4l2 added for OpenCV video capture
bcm2835-v4l2
I generally put my surname in any system files I edit then I can easily find any files I have dinked around with if I make a mess, and I can also back them up, and also tell anyone I help out on StackOverflow of any system changes I have made - yes, I am a bit OCD!
If it still doesn't work, check your cable is correct - the silver connector must face the HDMI port.
Keywords: Raspberry Pi, raspi, RPi, camera, camera cable, still, photo, video, OpenCV, v4l, v4l2, Video for Linux, raspi-config, raspistill, rpi-update, bcm2835, bcm2835-v4l2, modules, modprobe
Upvotes: 4