int main( ) { //加载模型 ncnn::Net squeezenet; squeezenet.load_param("mobilefacenet/mobilefacenet.param"); squeezenet.load_model("mobilefacenet/mobilefacenet.bin"); string readImgPartDir = "D:\faceData\";//读取图片路径 string personName;//人名文件夹 string imgName;//具体图片名(*.jpg) string readImg;//图片完成路径 = 读取图片路径+人名文件夹+具体图片名 FILE* f = fopen("D:\Feature.txt", "wt"); if (f == NULL) { printf("文件打开失败! "); } else { printf("文件打开成功! "); } std::ifstream fin("D:\数据\餐饮测试\faceData\0.txt");//打开原始样本图片文件列表 string readLineName;//从txt中读取的名字 cv::Mat image; while (getline(fin, readLineName)) { //cout << "readLineName = "<< readLineName << endl; imgName = readLineName.substr(readLineName.find_last_of("\") + 1); //cout << "imgName = " << imgName << endl; personName = readLineName.substr(0, readLineName.find_first_of("\")); //cout << "personName = " << personName << endl; readImg = readImgPartDir + personName + "\" + imgName;//输入图片 //cout << "readImg = " << readImg << endl; image = cv::imread(readImg, 1); //cout << "channels = " << image.channels() << endl; //cout << "image w=" << image.cols << ",h=" << image.rows << endl; if (image.empty()) { printf("--(!) No captured frame -- Break!"); continue; } else { //float *featFace = new float[128]; ncnn::Extractor ex = squeezenet.create_extractor(); ncnn::Mat in = ncnn::Mat::from_pixels_resize(image.data, ncnn::Mat::PIXEL_BGR2RGB, image.cols, image.rows, 112, 112); ex.input("data", in); ncnn::Mat out; ex.extract("fc1", out); for (int j = 0; j < out.w; ++j) { //featFace[j] = out[j]; std::fprintf(f, "%f ", out[j]); } std::fprintf(f, "%s", ","); std::fprintf(f, "%s", personName); std::fprintf(f, "%s", " "); //delete[] featFace; } }//end while fclose(f); return 0; }