How to solve the problem of python opencv calling the camera failure Windows

Stream saving:

  1. Contains Python Opencv binocular camera photo code (manual or automatic), which can be picked up by yourself;
  2. If your cv2.VideoCapture() function is stuck but does not report an error, open the Windows "Camera" application and you can see the camera image normally, and you can open the image with cv2.imshow() normally, please continue reading.

This semester, I took the media and cognition course taught by Teacher Fang Lu. The final project at the end of the semester involves the calibration of binocular cameras (maybe I will post some thoughts and experiences after finishing the project at the end of the semester?)

The assistant provided a piece of code for automatic shooting and saving of the binocular camera based on Python Opencv for us to use directly, but I encountered a strange phenomenon when running, the program stuck at cv2.VideoCapture(0) and could not run without error reporting , is simply stuck here. Put the code first:

import time

import cv2
import numpy as np

AUTO = False  # 自动拍照,或手动按s键拍照
INTERVAL = 2  # 自动拍照间隔

camera_0 = cv2.VideoCapture(0) # 代码运行的时候卡在这里!!
camera_1 = cv2.VideoCapture(1)

cv2.namedWindow("left")
cv2.namedWindow("right")

counter = 0
utc = time.time()
folder = "./SaveImage/"  # 拍照文件目录, 先新建再运行,否则无法保存


def shot(pos, frame):
    global counter
    path = folder + pos + "_" + str(counter) + ".jpg"
    cv2.imwrite(path, frame)
    print("snapshot saved into: " + path)


while True:
    ret_0, frame_0 = camera_0.read()
    ret_1, frame_1 = camera_1.read()
    left_frame = frame_0
    right_frame = frame_1
    cv2.imshow("left", left_frame)
    cv2.imshow("right", right_frame)
    now = time.time()

    if AUTO and now - utc >= INTERVAL:
        shot("left", left_frame)
        shot("right", right_frame)
        counter += 1
        utc = now

    key = cv2.waitKey(1)

    # press 's' in keyboard for capturing, 'q' for exit.
    if key == ord("q"):
        break
    elif key == ord("s"):
        shot("left", left_frame)
        shot("right", right_frame)
        counter += 1

camera_0.release()
camera_1.release()
cv2.destroyWindow("left")
cv2.destroyWindow("right")

This is quite contrary to common sense. I had a discussion with the teaching assistant, but to no avail.

The final solution is also quite strange, modify the stuck code to:

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

That's it. (Of course, each cv2.VideoCapture must be changed)

I don't know what's going on here. Some information I found said that the function of this parameter is "cannot use high-resolution camera without specifying it". Is it possible that the camera provided by the course is very high-definition and cannot be used without setting it? (Just guessing, if you know Wanwang, please enlighten me)? Because I basically don't know the opencv library, I feel that the opencv involved in this big job is just to take a photo, so I won't go into it.

I hope it can help some students who are stuck here like me~

(I hope my big homework can continue to advance ah ah ah ah ah ah)

2022.12.14 Supplement

Recently, I am going to use the newly installed Fedora 37 as a development platform, so I copied the previous code to Linux and tried it, and found that Linux does not seem to need to add this parameter... I am quite speechless orz

Guess you like

Origin blog.csdn.net/qq_53937391/article/details/128046952