Python OpenCV reading USB camera error problem solving

Error message:

Traceback (most recent call last):
  File "G:\Python Image Recognition Related Learning Video\Human Pose Detection.py", line 32, in <module>
    cv2.imshow('image_title',img)
cv2.error: OpenCV( 4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height> 0 in function 'cv::imshow'

 Software Environment:

Python 3.8.10

opencv 4.6.0.66

Hardware Configuration:

Intel Core i3-6100 @ 3.7GHz

Memory 8G

set display

usb camera

Problem code:

import cv2
cap = cv2.VideoCapture(0) # 读摄像头

# 设置图像大小640*480
cap.set(3,640)
cap.set(4,480)

while cap.isOpened():
    ret, img = cap.read()
    cv2.imshow('image_title',img)

    if cv2.waitKey(50) & 0xFF == ord('q'):
        cap.release()
        break
cv2.destroyAllWindows()

Troubleshooting:

Judging from the error message, it prompts size.width>0 && size.height>0 in function 'cv::imshow'. It can be seen that there is a problem with the imshow function. This is a function for displaying images. Since an error occurs, generally The problem is that there is a problem with the input parameters. Looking forward, size.width>0 && size.height>0, it can be speculated that the width or height of the input image data is 0, that is, the variable img is empty.

So, checking the source of the variable img, img comes from this sentence:

ret, img = cap.read()

ret indicates whether the read is successful, returns True if successful, and returns False if failed. img is the read image data. Since img is empty, it means that ret should be False at this time, that is, reading image data failed.

Let's go back and see where cap comes from. Cap is a VideoCapture object, which comes from this sentence:

cap = cv2.VideoCapture(0)

Here cv2.VideoCapture(0), where the parameter 0 means that the USB camera ID is 0. Next, you can try whether cap.isOpened() returns True. If not, it means that your USB camera ID is not 0. You can try other numbers 1, 2, 3, etc. until cap.isOpened() returns True.

On my side, cap.isOpened() returns True, indicating that the camera has been opened normally at this time.

So here comes the question, since the USB camera is turned on normally (my USB camera's working indicator is also on), why can't the image data be read?

I checked the Python environment, USB camera hardware, code statements, etc. I have been tossing around for a long time, and I have also searched a lot on Baidu, but I still have no clue. There is no problem with the hardware and the environment.

problem solved:

In the end, the problem was solved, so, there is this blog, to avoid pitfalls for friends who play image recognition!

The problem lies in this sentence:

cap = cv2.VideoCapture(0)

When we create the cap object, we only specify the camera ID, and do not specify the second parameter apiPreference. When the second parameter is not specified, it defaults to cv2.CAP_ANY, so the USB camera data cannot be read correctly. (Earlier versions of OpenCV may be ok. After all, most of the reference codes on the Internet do not specify the second parameter. In addition, the camera that comes with the notebook should not specify the second parameter.)

I added the second parameter as cv2.CAP_DSHOW to solve the problem. The changed statement is as follows:

cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)

The USB camera data can be read normally.

So far the problem is solved!

Here's the full modified code again:

import cv2
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW) # 读摄像头

# 设置图像大小640*480
cap.set(3,640)
cap.set(4,480)

while cap.isOpened():
    ret, img = cap.read()
    cv2.imshow('image_title',img)

    if cv2.waitKey(50) & 0xFF == ord('q'):
        cap.release()
        break
cv2.destroyAllWindows()

I hope it can help you little cuties who encounter the same problem~

You are also welcome to leave a message on my small website: space.bilibili.com/14596169

Guess you like

Origin blog.csdn.net/daochengzhuke/article/details/127913562