简体   繁体   English

我无法从OpenCV的摄像头捕获帧

[英]I can't capture frames from camera in OpenCV

I am trying to detect eyes but I have another problem. 我试图检测眼睛,但是我还有另一个问题。 I cannot display the camera frame. 我无法显示相机框。 The problem can be obvious but I am newbie. 问题可能很明显,但我是新手。 A part of my code below: 我的部分代码如下:

Here is my EyeDetection.h 这是我的EyeDetection.h

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>

using namespace cv;

class EyeDetection {
private:
    CascadeClassifier eye_cascade, eyepair_cascade;
public:
    EyeDetection();
    void detect();
}; 

Here is my EyeDetection.cpp 这是我的EyeDetection.cpp

#include "EyeDetection.h"

EyeDetection::EyeDetection() {
    eye_cascade.load("haarcascade_eye.xml");
    eyepair_cascade.load("haarcascade_mcs_eyepair_big.xml");
}

void EyeDetection::detect()
{
    VideoCapture webcam(1); //Webcam number is 1
    if (eyepair_cascade.empty() || eye_cascade.empty() || !(webcam).isOpened())
        return;

    webcam.set(CV_CAP_PROP_FRAME_WIDTH, 800);
    webcam.set(CV_CAP_PROP_FRAME_HEIGHT, 600);

    Mat frame;
    while (1) {
        webcam >> frame;
        if (frame.empty()) continue;
        imshow("asad", frame);
    }
}

And here is my Source.cpp(main): 这是我的Source.cpp(main):

#include "EyeDetection.h"

using namespace cv;

int main(int argc, char** argv)
{
    EyeDetection e = EyeDetection();
    e.detect();
    return 0;
}

It does not show the camera frame, it shows just a blank gray window. 它不显示相机框架,而仅显示空白的灰色窗口。 What is the problem? 问题是什么?

  1. Please check your camera id 1. If you have only one connected camera then please replace the camera id 0 instead 1. 请检查您的摄像机ID1。如果只有一台已连接的摄像机,请更换摄像机ID 0代替1。

cv2.VideoCapture(1) to cv2.VideoCapture(0) cv2.VideoCapture(1)cv2.VideoCapture(0)

  1. add the waitKey() line add this line at the end of loop 添加waitKey()行在循环末尾添加此行

` `

while (1) {
        webcam >> frame;
        if (frame.empty()) continue;
        imshow("asad", frame);
        cv2.waitkey(0)
    }

` Maybe to check about waitKey() should be needed for you. `也许您应该检查一下waitKey()。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM