opencv VideoCapture相机索引超出范围

pes8fvy9  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(1428)

当我运行下面的脚本时

import os

import cv2

DATA_DIR = './data'
if not os.path.exists(DATA_DIR):
    os.makedirs(DATA_DIR)

number_of_classes = 3
dataset_size = 100

cap = cv2.VideoCapture(2)
for j in range(number_of_classes):
    if not os.path.exists(os.path.join(DATA_DIR, str(j))):
        os.makedirs(os.path.join(DATA_DIR, str(j)))

    print('Collecting data for class {}'.format(j))

    done = False
    while True:
        ret, frame = cap.read()
        if ret:
            cv2.putText(frame, 'Ready? Press "Q" ! :)', (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3,
                    cv2.LINE_AA)
        cv2.imshow('frame', frame)
        if cv2.waitKey(25) == ord('q'):
            break
        else:
            break
        

    counter = 0
    while counter < dataset_size:
        ret, frame = cap.read()
        cv2.imshow('frame', frame)
        cv2.waitKey(25)
        cv2.imwrite(os.path.join(DATA_DIR, str(j), '{}.jpg'.format(counter)), frame)

        counter += 1
   
           

cap.release()
cv2.destroyAllWindows()

获取此错误,请帮助!
[ERROR:0@0.156] global obsensor_uvc_stream_channel.cpp:156 cv::obsensor::getStreamChannelGroup摄像机索引超出范围
outof range正在收集0类Traceback的数据(最近一次调用是最后一次):文件“d:\Final Year Project\Proj_By_Me\collect_imgs.py”,第26行,在cv2.imshow('frame',frame)(-215:Assertion f cv2.error:OpenCV(4.7.0)D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:971:错误:(-215:Assert失败)size.width〉0 && size.height〉0 in function 'cv::imshow'

2skhul33

2skhul331#

1.添加此导入-〉from pathlib import Path

  1. cv2.videocapture(2)用于连接到系统的外部摄像头,cv2.videocapture(0)用于内部摄像头。我使用'0',它工作了。
    从这些我能够修复它。

相关问题