5

I am having trouble accessing my USB camera using OpenCV with python.

I get the following error message which I understand means no frame was captured?

error: OpenCV(3.4.1) C:\Miniconda3\conda-bld\opencv-suite_1533128839831\work\modules\highgui\src\window.cpp:356: error: (-215) size.width>0 && size.height>0 in function cv::imshow

My camera is the following: https://www.gophotonics.com/products/scientific-industrial-cameras/point-grey-research-inc/45-571-cm3-u3-50s5m-cs

Simple code as below. Any thoughts? Some help I'll appreciate

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
while(True):
    ret, frame = cap.read()

    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
4
  • Try putting '1' instead of '0' in VideoCapture. Like, cv2.VideoCapture(1). Commented Jul 17, 2020 at 17:34
  • Thanks for the reply. I do not think its an indexing problem, I have tried iterating up to index 500 and none work... Commented Jul 19, 2020 at 12:00
  • Is your usb camera working normally with your computer, without trying to access it from python? Like using some default usb option to access it. Just to check if it's compatible and it works fine normally. Commented Jul 19, 2020 at 15:55
  • It works using spinview, program provided by the company Commented Jul 20, 2020 at 18:55

2 Answers 2

7

The camera index most of the time is 0 as default with computers which come with an integrated camera, but if you're plugging a USB camera, its camera index could be 1 or 2, you can try with both of them.

But if you wanna get all camera index available, you can use the following simple script to find out:

import cv2
import numpy as np

all_camera_idx_available = []

for camera_idx in range(10):
    cap = cv2.VideoCapture(camera_idx)
    if cap.isOpened():
        print(f'Camera index available: {camera_idx}')
        all_camera_idx_available.append(camera_idx)
        cap.release()

The result would be a list like: [0,1,2,...] with all camera index available.

But if you're using a USB camera you have to keep in mind that some OpenCV functions won't work, take a look at this.

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

1 Comment

Thanks for the reply. I do not think its an indexing problem, I have tried iterating up to index 500 and none work...
0

If the cameras you are using are spinnaker cameras, you have to use the PySpin camera API to get images off the device.

Check out this link to get this API installed. No module named 'PySpin'

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.