Check if camera feed is blank before using

This commit is contained in:
Epicalert 2021-06-16 18:53:00 +08:00
parent ed5b1c26e0
commit 5bea68564f
No known key found for this signature in database
GPG key ID: CAA46F858D0979BD

View file

@ -16,6 +16,19 @@ cv::dnn::Net dnnFaceDetector;
cv::VideoCapture vid; cv::VideoCapture vid;
cv::Mat frame, gray, small; 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() { void initCV() {
haarFaceDetector = cv::CascadeClassifier (resolvePath("cvdata/haarcascade_frontalface_alt2.xml")); haarFaceDetector = cv::CascadeClassifier (resolvePath("cvdata/haarcascade_frontalface_alt2.xml"));
dnnFaceDetector = cv::dnn::readNetFromCaffe( dnnFaceDetector = cv::dnn::readNetFromCaffe(
@ -31,9 +44,18 @@ void initCV() {
vid = cv::VideoCapture (i); vid = cv::VideoCapture (i);
if (vid.isOpened()) { if (vid.isOpened()) {
std::cout << "Camera " << i << " opened" << std::endl; std::cout << "Camera " << i << " opened" << std::endl;
// 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; break;
} }
}
} }
} }