ノートパソコンの標準搭載と、USBのWEBカメラを接続した状態で開発テストをしています。
PythonのOpenCVで、接続済みのWEBカメラの番号を調べるコードを実行してみました。
import cv2
for i1 in range(0, 20):
cap1 = cv2.VideoCapture( i1, cv2.CAP_DSHOW )
if cap1.isOpened():
print("VideoCapture(", i1, ") : Found")
else:
print("VideoCapture(", i1, ") : None")
cap1.release()
すると、外部のWEBカメラを接続していない場合(標準搭載のカメラのみの場合)は、0と1のみがFound
で
外部のWEBカメラを接続している場合は、0と1と2がFound
となっています。
つまり、2番にWEBカメラは接続されているかと思います。
次のC++のオープンソースのコードで、第1引数を2にすると画面が真っ暗な状態でカメラ映像が表示されます。
setInputCamera(2, FLAG_camRes);
第1引数を0または1とすると、ノートパソコン標準搭載のカメラで自分の顔が映し出されます。
setInputCamera(0, FLAG_camRes);
setInputCamera(1, FLAG_camRes);
その他の映像キャプチャデバイスは接続されていないため、次のように3以上の数字を入力すると、ソフトウェアを起動するだけでクラッシュします。
setInputCamera(3, FLAG_camRes);
setInputCamera(4, FLAG_camRes);
err = app.setInputCamera(2, FLAG_camRes);
NvCV_Status App::setInputCamera(int index, const std::string& resStr) {
if (!_vidIn.open(index))
return NvFromAppErr(APP_ERR_OPEN);
if (!resStr.empty()) {
int n, width, height;
n = sscanf(resStr.c_str(), "%d%*[xX]%d", &width, &height);
switch (n) {
case 2:
break; // We have read both width and height
case 1:
height = width;
width = (int)(height * (4. / 3.) + .5);
break;
default:
height = 0;
width = 0;
break;
}
if (width) _vidIn.set(CV_CAP_PROP_FRAME_WIDTH, width);
if (height) _vidIn.set(CV_CAP_PROP_FRAME_HEIGHT, height);
_inFile = "webcam";
_inFile += std::to_string(index);
}
_videoWidth = (unsigned)_vidIn.get(CV_CAP_PROP_FRAME_WIDTH);
_videoHeight = (unsigned)_vidIn.get(CV_CAP_PROP_FRAME_HEIGHT);
_frameRate = _vidIn.get(CV_CAP_PROP_FPS);
// Rounding the frame rate is required because OpenCV does not support all frame rates when writing video
static const int fps_precision = 1000;
_frameRate = static_cast<int>((_frameRate + 0.5) * fps_precision) / static_cast<double>(fps_precision);
if (FLAG_verbose)
printf("Camera capture resolution set to %dx%d @ %4.1f fps\n", _videoWidth, _videoHeight, _frameRate);
return NVCV_SUCCESS;
}
どうしたら画面を正常に映し出せるのでしょうか?