Raspberry Pi Camera and OpenCV: can't open camera by index

Viewed 10478

I've got a weird problem:

I've installed the OpenCV lib on my Pi. I have a Pi Cam connexted to the Pi (i am able to list all video devices and am able to take a picture with raspistill)

But when i try to take a video feed from opencv with python

from flask import Flask, render_template, Response
import cv2

app = Flask(__name__)

cap = cv2.VideoCapture(1)

I get the error:

[ WARN:0] global /tmp/pip-wheel-qd18ncao/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video0): can't open camera by index

I tried it with different index (from -1 up to 13) but nothing works.

Any hints ?

1 Answers

I had a similar problem, try to specify the video backend, like:

cap = cv2.VideoCapture(index, cv2.CAP_V4L)

Index can be set to -1 to be automatically detected. You should also need to enable this module if your raspberry is not recent:

sudo modprobe bcm2835-v4l2

Take also a look here, where a similar problem is described.

Related