videoio: fix CAP_IMAGES with non-numbered file

This commit is contained in:
Maksim Shabunin 2023-07-06 21:08:17 +03:00
parent b8e3bc9dd8
commit 8931f08362

View File

@ -302,59 +302,58 @@ bool CvCapture_Images::open(const std::string& _filename)
if (filename_pattern.empty()) if (filename_pattern.empty())
{ {
filename_pattern = _filename; filename_pattern = _filename;
cv::String filename = _filename.c_str(); if (!utils::fs::exists(filename_pattern))
if (!utils::fs::exists(filename))
{ {
CV_LOG_INFO(NULL, "CAP_IMAGES: File does not exist: " << filename_pattern);
close();
return false; return false;
} }
if (!haveImageReader(filename)) if (!haveImageReader(filename_pattern))
{ {
CV_LOG_INFO(NULL, "CAP_IMAGES: Stop scanning. Can't read image file: " << filename); CV_LOG_INFO(NULL, "CAP_IMAGES: File is not an image: " << filename_pattern);
close();
return false;
} }
length = 1; length = 1;
// grab frame to enable properties retrieval
bool grabRes = grabFrame();
grabbedInOpen = true;
currentframe = 0;
return grabRes;
} }
else
// determine the length of the sequence
for (length = 0; ;)
{ {
cv::String filename = cv::format(filename_pattern.c_str(), (int)(offset + length)); // determine the length of the sequence
if (!utils::fs::exists(filename)) for (length = 0; ;)
{ {
if (length == 0 && offset == 0) // allow starting with 0 or 1 cv::String filename = cv::format(filename_pattern.c_str(), (int)(offset + length));
if (!utils::fs::exists(filename))
{ {
offset++; if (length == 0 && offset == 0) // allow starting with 0 or 1
continue; {
offset++;
continue;
}
CV_LOG_INFO(NULL, "CAP_IMAGES: File does not exist: " << filename);
break;
} }
break;
if(!haveImageReader(filename))
{
CV_LOG_INFO(NULL, "CAP_IMAGES: File is not an image: " << filename);
break;
}
length++;
} }
if(!haveImageReader(filename)) if (length == 0)
{ {
CV_LOG_INFO(NULL, "CAP_IMAGES: Stop scanning. Can't read image file: " << filename); close();
break; return false;
} }
length++; firstframe = offset;
} }
if (length == 0)
{
close();
return false;
}
firstframe = offset;
// grab frame to enable properties retrieval // grab frame to enable properties retrieval
bool grabRes = grabFrame(); bool grabRes = CvCapture_Images::grabFrame();
grabbedInOpen = true; grabbedInOpen = true;
currentframe = 0; currentframe = 0;
return grabRes; return grabRes;
} }