1

I tried making this code work with the raspberry pi cam. how do you make the cv2.VideoCapture(0) recognise the raspberry pi camera as the designated camera

import cv2

def diffImg(t0, t1, t2):
   d1 = cv2.absdiff(t2, t1)
   d2 = cv2.absdiff(t1, t0)
   return cv2.bitwise_and(d1, d2)

cam = cv2.VideoCapture(0)  

winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)

t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)

while True:
   cv2.imshow( winName, diffImg(t_minus, t, t_plus) )

# Read next image
t_minus = t
t = t_plus
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)

key = cv2.waitKey(10)
if key == 27:
   cv2.destroyWindow(winName)
   break

 print ("Goodbye")
amit sabag
  • 49
  • 1
  • 1
  • 7

5 Answers5

4

You cannot use cv2.VideoCapture() for RaspiCam.

The cv2.VideoCapture() is only for USB camera, not for CSI camera.

If you want to use RaspiCam for capturing, you can refer this tutorial

Trevor
  • 5,074
  • 2
  • 16
  • 42
  • Thanks. But on my laptop, `cv2.VideoCapture()` works with integrated webcam. Why it should fail with integrated webcam of Raspberry Pi ? – kiranpradeep May 30 '15 at 15:29
  • 1
    Yes, because your integrated webcam could be consider as a usb camera. The Raspi Camera is another type of hardware (serial interface), and the official OpenCV is not support to detect this type of camera. – Trevor May 30 '15 at 16:10
  • 2
    As [this answer](http://stackoverflow.com/a/37530016/1135819) shows, it is actually possible after loading a kernel module. – betabandido Nov 17 '16 at 09:32
  • cv2.VideoCapture also works for IP cameras. The the raspberry pi camera is the odd duck here. – MattK Dec 09 '16 at 15:28
  • 3
    cv2.VideoCapture() can be used with the Raspberry Pi camera (not using RaspiCam software) but instead by running `sudo modprobe bcm2835-v4l2` which makes the Raspberry Pi camera visible (which you can see with `ls -ltrh /dev/video*` ). – user3567174 Mar 08 '17 at 16:11
2

Some time ago, I developed on a rasperry pi with raspicam, an interface for opencv. I thought that the video capture in pure cv only works for usb devices

You can download raspicam under http://sourceforge.net/projects/raspicam/files/

kguest
  • 3,730
  • 3
  • 28
  • 31
501 - not implemented
  • 2,508
  • 4
  • 35
  • 70
0

From what I can understand, you need to find the location # of the raspberry pi camera and change

cam = cv2.VideoCapture(0)

to

cam = cv2.VideoCapture(Camera#) 
Ben Morris
  • 605
  • 4
  • 23
0

Try the following:

video_capture = cv2.VideoCapture(
            get_jetson_gstreamer_source(), cv2.CAP_GSTREAMER
        )
Amit Yadav
  • 2,613
  • 3
  • 15
  • 50
Mahmut
  • 1
-1

The problem is that you are not coding safely.

If you had check the return of the method, you would know instantly that 0 is not the index of your camera:

import sys

cam = cv2.VideoCapture(0)  
if not cam:
    print("!!! Failed VideoCapture: invalid parameter!")
    sys.exit()

Try numbers > 0.

Community
  • 1
  • 1
karlphillip
  • 87,606
  • 33
  • 227
  • 395