0

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)
6
  • Did you enable the camera in raspi-config? Did you load the Broadcom v4l2 module with sudo modprobe bcm2835-v4l2 Commented May 28, 2017 at 16:28
  • Yeah I enable the camera, and the broadcome v412 return an error modprobe: ERROR: ../libkmod/libkmod.c:557 kmod_search_moddep() could not open moddep file '/lib/modules/4.4.50-v7+/modules.dep.bin' Commented May 28, 2017 at 18:50
  • Can you shoot still, single frames with raspistill? Commented May 28, 2017 at 20:45
  • Its work fine, I must use "sudo rpi-update" and after "sudo modprobe bcm2835-v4l2" Commented May 28, 2017 at 21:14
  • 1
    I have summarised everything we have learned in an answer for future readers :-) Commented May 29, 2017 at 10:45

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.