C++ 函数返回值-1,因此无法访问文件属性。我如何纠正这个问题?
此代码是使用OpenCV进行面部识别的培训师代码。它裁剪并均衡图像,使其仅包含人脸。 发生的唯一错误是stat()函数返回值-1,并且未访问D:\Project\Original下的文件 以下内容的输出为: -1无法访问文件夹1 请帮助我理解为什么会发生这种情况。当使用OpenCV 2.4.9在代码块16.01上编译相同的代码(有微小的更改)时,不会发生此错误C++ 函数返回值-1,因此无法访问文件属性。我如何纠正这个问题?,c++,visual-studio,opencv,visual-studio-2015,opencv3.1,C++,Visual Studio,Opencv,Visual Studio 2015,Opencv3.1,此代码是使用OpenCV进行面部识别的培训师代码。它裁剪并均衡图像,使其仅包含人脸。 发生的唯一错误是stat()函数返回值-1,并且未访问D:\Project\Original下的文件 以下内容的输出为: -1无法访问文件夹1 请帮助我理解为什么会发生这种情况。当使用OpenCV 2.4.9在代码块16.01上编译相同的代码(有微小的更改)时,不会发生此错误 #include <iostream> #include <fcntl.h> #include <sys/
#include <iostream>
#include <fcntl.h>
#include <sys/stat.h>
#include <string>
#include <string.h>
#include <fstream>
#include <string.h>
#include <direct.h>
#include <dirent.h>
#include <opencv/cv.h>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/objdetect.hpp>
using namespace std;
using namespace cv;
char* IMG_SEP = "_";
int MAX_IMAGE_NAME_SIZE = 100;
int IM_HEIGHT = 200;
int IM_WIDTH = 200;
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
vector<string> fileNames;
vector<string> outputLabel;
vector<string> outputPath;
char* srcDirName = "D:/Project/Original";
char* dstDirName = "D:/Project/Cropped";
char* CSV_PATH = "D:/Project/Cropped/myCSV.csv";
void getFileNames(char* path) {
DIR *dir;
struct stat info;
struct dirent *ent;
if ((dir = opendir(path)) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if ((!strcmp(ent->d_name, ".") == 0) && (!strcmp(ent->d_name, "..") == 0)) {
char* temp = (char*)malloc(256);
sprintf_s(temp, 100, "%s%s", path, ent->d_name);
// THIS IS WHERE THE DEBUGGER GOES. STAT RETURNS -1
if (stat(temp, &info) != 0) {
cout << stat(temp, &info) << " ";
cout << "Cannot access " << ent->d_name << "\n";
}
if (info.st_mode & S_IFREG) {
string temp1 = ent->d_name;
temp1 = temp1.substr(0, temp1.rfind(IMG_SEP));
temp1 = dstDirName + temp1;
_mkdir(temp1.c_str());
fileNames.push_back(temp);
}
else if (info.st_mode & S_IFDIR) {
char* temp1 = (char*)malloc(256);
sprintf_s(temp1, 100, "%s/%s", dstDirName, ent->d_name);
_mkdir(temp1);
strcat_s(temp, sizeof temp, "/");
getFileNames(temp);
}
}
}
closedir(dir);
}
else {
cout << "Could not open the required image directory";
}
}
void printVector(vector<string> images) {
for (int i = 0; i < images.size(); i++) {
cout << images[i] << "\n";
}
}
void initializeDetector() {
if (!face_cascade.load(face_cascade_name)) {
cout << "Error while loading the face detection cascade classifier";
}
if (!eyes_cascade.load(eyes_cascade_name)) {
cout << "Error while loading the eye detection cascade classifier";
}
}
void detectAndStore(Mat image, String imageName) {
vector<Rect> faces;
vector<Rect> eyes;
Mat frame_grey;
cvtColor(image, frame_grey, CV_BGR2GRAY);
equalizeHist(frame_grey, frame_grey);
face_cascade.detectMultiScale(frame_grey, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
//if(faces.size() == 1){
for (size_t i = 0; i < faces.size(); i++) {
Mat faceROI = frame_grey(faces[i]);
eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
if (eyes.size() < 1) {
continue;
}
resize(faceROI, faceROI, Size(IM_WIDTH, IM_HEIGHT), 1.0, 1.0, INTER_CUBIC);
String fileName = imageName;
int posBeg = fileName.rfind('/') + 1;
String label = fileName.substr(posBeg, MAX_IMAGE_NAME_SIZE);
int posEnd = label.rfind('.');
String labelNameWithNum = label.substr(0, posEnd);
cout << "Cropping and storing " << labelNameWithNum << "\n";
int pos = labelNameWithNum.rfind(IMG_SEP);
String labelNameWithoutNum = labelNameWithNum.substr(0, pos);
String destination = dstDirName + labelNameWithoutNum + "/" + label;
outputPath.push_back(destination);
outputLabel.push_back(labelNameWithoutNum);
equalizeHist(faceROI, faceROI);
imwrite(destination, faceROI);
break;
}
}
void processImages() {
initializeDetector();
for (int i = 0; i < fileNames.size(); i++) {
Mat image = imread(fileNames[i], CV_LOAD_IMAGE_COLOR);
detectAndStore(image, fileNames[i]);
}
}
void createCSV() {
ofstream myFile;
myFile.open(CSV_PATH, ios_base::out);
for (int i = 0; i < outputPath.size(); i++) {
myFile << outputPath[i] << ";" << outputLabel[i] << "\n";
}
myFile.close();
}
int main() {
getFileNames(srcDirName);
processImages();
createCSV();
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间cv;
char*IMG_SEP=“”;
int MAX_IMAGE_NAME_SIZE=100;
int IM_高度=200;
int IM_WIDTH=200;
字符串face_cascade_name=“haarcascade_frontalface_alt.xml”;
字符串eyes\u cascade\u name=“haarcascade\u eye\u tree\u eyegories.xml”;
层叠式分级机;
级联效应;
矢量文件名;
向量输出标签;
向量输出路径;
char*srcDirName=“D:/Project/Original”;
char*dstDirName=“D:/Project/Cropped”;
char*CSV_PATH=“D:/Project/crapped/myCSV.CSV”;
void getFileNames(char*path){
DIR*DIR;
结构统计信息;
结构导向;
如果((dir=opendir(path))!=NULL){
while((ent=readdir(dir))!=NULL){
如果(!strcmp(ent->d_name,“.”=0)和(!strcmp(ent->d_name,“…”)==0)){
char*temp=(char*)malloc(256);
sprintf_s(临时,100,“%s%s”,路径,ent->d_名称);
//这就是调试器去的地方。STAT返回-1
如果(统计(温度和信息)!=0){
coutsprintf_s(温度,100,“%s%s”,路径,ent->d_名称);
->
sprintf_s(温度,100,“%s/%s”,路径,ent->d_名称);
(添加斜杠)
这是通过在失败的stat点检查“temp”来调试的
由于dirent没有随MSVC提供,可能是您粘贴的dirent.h中的一个错误。但是您稍后使用ent->d_名称执行了%s/%s,并且“path”来自您的硬编码常量,因此我认为您在从“代码块”移植时出错既然您使用的是OpenCV 3.1,那么就可以使用。这样就避免了像您这样复杂的代码及其错误
您的getFileNames
基本上减少为:
vector<cv::String> fileNames; // This must be a cv::String now
...
void getFileNames(std::string path)
{
cv::glob(path,fileNames,false);
}
矢量文件名;//现在必须是cv::字符串
...
void getfilename(std::string路径)
{
cv::glob(路径、文件名,false);
}
您可以看到在许多其他答案中使用了glob
,例如..您是否尝试了CreateFile()
以及GetLastError()呢
而不是stat?您确定文件夹具有正确的权限吗?权限正确。我将尝试createfile和getlasterror。谢谢:)我收到错误:调试断言失败!表达式:L“String not null terminated”在添加斜杠后。因为您编写了“sizeof temp”这是4。你的意思是写256。对于你这种情况的人,我建议你立即停止使用C字符串,并学习如何使用std::strings。除非你有更具体的需要,否则请看miki的答案。非常感谢你的帮助:)在包括了斜线和更改临时大小之后,我在srcDirName和dstDirName的末尾添加了斜线。然后每次我调试时,我的显示驱动程序都开始崩溃。我卸载了驱动程序,它工作得很好:)非常感谢您的帮助:)使用glob似乎是一个更好的选择。如果有人更新cv::glob
@sturkmen的文档就可以了是的,文档中的一个示例可能会有所帮助(比如berak),但我没有时间做这个。