Check if camera feed is blank before using
This commit is contained in:
parent
ed5b1c26e0
commit
5bea68564f
26
src/cv.cpp
26
src/cv.cpp
|
@ -16,6 +16,19 @@ cv::dnn::Net dnnFaceDetector;
|
|||
cv::VideoCapture vid;
|
||||
cv::Mat frame, gray, small;
|
||||
|
||||
bool checkBlankFrame(cv::Mat mat) {
|
||||
float firstPixel = mat.at<float>(0);
|
||||
for (int j = 0; j < mat.total(); j++) {
|
||||
if (mat.at<float>(j) != firstPixel) {
|
||||
// different pixel found
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// all pixels are the same
|
||||
return true;
|
||||
}
|
||||
|
||||
void initCV() {
|
||||
haarFaceDetector = cv::CascadeClassifier (resolvePath("cvdata/haarcascade_frontalface_alt2.xml"));
|
||||
dnnFaceDetector = cv::dnn::readNetFromCaffe(
|
||||
|
@ -31,9 +44,18 @@ void initCV() {
|
|||
vid = cv::VideoCapture (i);
|
||||
if (vid.isOpened()) {
|
||||
std::cout << "Camera " << i << " opened" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
// check if camera is giving solid colored frames
|
||||
cv::Mat testFrame;
|
||||
vid.read(testFrame);
|
||||
if (checkBlankFrame(testFrame)) {
|
||||
// reject if giving blank frames
|
||||
std::cout << "Camera " << i << " feed blank!" << std::endl;
|
||||
} else {
|
||||
std::cout << "Using camera " << i << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue