5

I want to grab live images from a camera which is connected via USB with my computer.

I am using an Industrial Camera with usb port.

At device manager the camera is shown with its name and id so I think it is connected to PC.

I ran a 'findcam' program but it is not showing any existance of camera

import cv2

cap = cv2.VideoCapture(0)


while True:
    ret, frame = cap.read()
    cv2.imshow('Live Video', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

The given code which i tried is running for webcam on my laptop but when the same code i run on my PC with an external camera.

it constantly showing an error.

The Error:

Traceback (most recent call last):
  
File "C:/Users/Admin/PycharmProjects/industrialcamera/ICvideocapture.py", line 11, in <module>

cv2.imshow('Live Video', frame)

cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:352: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

I tried changing Index -1 , 0 , 1 but the error is constant

please help to find, weather it is my PC problem or camera problem or is their is any other way to stream(in python)

Thank You

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
54TY4M
  • 61
  • 1
  • 7

4 Answers4

1

I started your code on my PC and it works fine. Try to set the camera resolution manually, if you have an error with size.width and size.height, something like this:

cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,920)
Peter Nazarenko
  • 411
  • 1
  • 7
  • 16
  • I tried your updates but it still shows the same error and my above code is running perfectly for in build webcam in my laptop but not for external camera. You tried with external camera or by in build webcam – 54TY4M Aug 23 '19 at 04:30
  • I tried with external USB camera (also a kind of industrial). Can you try your external camera on different PCs? – Peter Nazarenko Aug 23 '19 at 11:51
  • Worked like a charm! Finally a live feed instead of a black window. Thank you! – radzi0_0 Jan 08 '21 at 23:48
0

You may have installed the wrong version of OpenCV which doesn't support video. Uninstall everything of opencv and then run:

pip install opencv-python

(Source: cv2.VideoCapture.open() always returns FALSE)

bb1950328
  • 1,403
  • 11
  • 18
  • I am using pycharm for codeing so is their any other way or plugins which i need to install. I have install opnecv_python for this program – 54TY4M Aug 20 '19 at 07:36
0

Try to install opencv-contrib-python via pip: pip install opencv-contrib-python or maybe try to upgrade the version of it : pip install opencv-python --upgrade

RyanPython
  • 412
  • 1
  • 5
  • 14
0

you can debugg by checking the ret value :

import cv2
cap = cv2.VideoCapture(0)
counter = 0
while True:.       
    ret, frame = cap.read()
    if ret :
        cv2.imshow('Live Video', frame)
        print(f" frame {counter} : ok ")
    if not ret :
        print(f" frame {counter} : ...")
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
Issam Dak
  • 1
  • 1