1. 程式人生 > >C++遍歷獲取資料夾下面所有檔名

C++遍歷獲取資料夾下面所有檔名

#include <iostream>  
#include <stdlib.h>  
#include <stdio.h>  
#include <string.h>  
#ifdef linux  
#include <unistd.h>  
#include <dirent.h>  
#endif  
#ifdef WIN32  
#include <direct.h>  
#include <io.h>  
#endif  
using namespace std;  
vector<string
>
getFiles(string cate_dir) { vector<string> files;//存放檔名 #ifdef WIN32 _finddata_t file; long lf; //輸入資料夾路徑 if ((lf=_findfirst(cate_dir.c_str(), &file)) == -1) { cout<<cate_dir<<" not found!!!"<<endl; } else { while
(_findnext(lf, &file) == 0) { //輸出檔名 //cout<<file.name<<endl; if (strcmp(file.name, ".") == 0 || strcmp(file.name, "..") == 0) continue; files.push_back(file.name); } } _findclose(lf); #endif
#ifdef linux DIR *dir; struct dirent *ptr; char base[1000]; if ((dir=opendir(cate_dir.c_str())) == NULL) { perror("Open dir error..."); exit(1); } while ((ptr=readdir(dir)) != NULL) { if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0) ///current dir OR parrent dir continue; else if(ptr->d_type == 8) ///file //printf("d_name:%s/%s\n",basePath,ptr->d_name); files.push_back(ptr->d_name); else if(ptr->d_type == 10) ///link file //printf("d_name:%s/%s\n",basePath,ptr->d_name); continue; else if(ptr->d_type == 4) ///dir { files.push_back(ptr->d_name); /* memset(base,'\0',sizeof(base)); strcpy(base,basePath); strcat(base,"/"); strcat(base,ptr->d_nSame); readFileList(base); */ } } closedir(dir); #endif //排序,按從小到大排序 sort(files.begin(), files.end()); return files; }

返回的是一個vector,呼叫的話就是files[i]
比如zqh資料夾下面有1.jpg,2.jpg,那麼files[0]=”1.jpg”,files[1]=”2.jpg”
以下是做影象分類的時候,呼叫caffe的classification.bin(改動以後的),遍歷資料夾下的所有圖片,並將每張圖片屬於每一類的概率寫入csv檔案。

#include <caffe/caffe.hpp>
#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif  // USE_OPENCV
#include <algorithm>
#include <iosfwd>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <fstream>  
#include <unistd.h>  
#include <dirent.h>
#include <stdlib.h>      
#ifdef USE_OPENCV
using namespace caffe;  // NOLINT(build/namespaces)
using std::string;
using namespace std;
ofstream outFile;

/* Pair (label, confidence) representing a prediction. */
typedef std::pair<string, float> Prediction;

class Classifier {
 public:
  Classifier(const string& model_file,
             const string& trained_file,
             const string& mean_file,
             const string& label_file);

  std::vector<Prediction> Classify(const cv::Mat& img, const string file,int N = 5);

 private:
  void SetMean(const string& mean_file);

  std::vector<float> Predict(const cv::Mat& img);

  void WrapInputLayer(std::vector<cv::Mat>* input_channels);

  void Preprocess(const cv::Mat& img,
                  std::vector<cv::Mat>* input_channels);

 private:
  shared_ptr<Net<float> > net_;
  cv::Size input_geometry_;
  int num_channels_;
  cv::Mat mean_;
  std::vector<string> labels_;
};

Classifier::Classifier(const string& model_file,
                       const string& trained_file,
                       const string& mean_file,
                       const string& label_file) {
#ifdef CPU_ONLY
  Caffe::set_mode(Caffe::CPU);
#else
  Caffe::set_mode(Caffe::GPU);
#endif

  /* Load the network. */
  net_.reset(new Net<float>(model_file, TEST));
  net_->CopyTrainedLayersFrom(trained_file);

  CHECK_EQ(net_->num_inputs(), 1) << "Network should have exactly one input.";
  CHECK_EQ(net_->num_outputs(), 1) << "Network should have exactly one output.";

  Blob<float>* input_layer = net_->input_blobs()[0];
  num_channels_ = input_layer->channels();
  CHECK(num_channels_ == 3 || num_channels_ == 1)
    << "Input layer should have 1 or 3 channels.";
  input_geometry_ = cv::Size(input_layer->width(), input_layer->height());

  /* Load the binaryproto mean file. */
  SetMean(mean_file);

  /* Load labels. */
  std::ifstream labels(label_file.c_str());
  CHECK(labels) << "Unable to open labels file " << label_file;
  string line;
  while (std::getline(labels, line))
    labels_.push_back(string(line));

  Blob<float>* output_layer = net_->output_blobs()[0];
  CHECK_EQ(labels_.size(), output_layer->channels())
    << "Number of labels is different from the output layer dimension.";
}

static bool PairCompare(const std::pair<float, int>& lhs,
                        const std::pair<float, int>& rhs) {
  return lhs.first > rhs.first;
}

/* Return the indices of the top N values of vector v. */
static std::vector<int> Argmax(const std::vector<float>& v, int N) {
  std::vector<std::pair<float, int> > pairs;
  for (size_t i = 0; i < v.size(); ++i)
    pairs.push_back(std::make_pair(v[i], i));
  std::partial_sort(pairs.begin(), pairs.begin() + N, pairs.end(), PairCompare);

  std::vector<int> result;
  for (int i = 0; i < N; ++i)
    result.push_back(pairs[i].second);
  return result;
}

/* Return the top N predictions. */
std::vector<Prediction> Classifier::Classify(const cv::Mat& img, const string file,int N) {
  std::vector<float> output = Predict(img);
  int i;
  double sum=0;
  int cnt=0;
  int b=file.find('.'); 
  for(i=0;i<output.size();i++)
  {
    sum+=output[i];
    if(i)
    {
        outFile << file.substr(0,b) << ',' << i << ',' << output[i] << endl;  
    }
    if(i==29)
    {
        outFile << file.substr(0,b) << ',' << 30 << ',' << output[0] << endl;
    }
  }
  if(sum > 1)
  {
      cnt++;
      cout << cnt << endl;
  }
  N = std::min<int>(labels_.size(), N);
  std::vector<int> maxN = Argmax(output, N);
  std::vector<Prediction> predictions;
  for (int i = 0; i < N; ++i) {
    int idx = maxN[i];
    predictions.push_back(std::make_pair(labels_[idx], output[idx]));
  }

  return predictions;
}

/* Load the mean file in binaryproto format. */
void Classifier::SetMean(const string& mean_file) {
  BlobProto blob_proto;
  ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);

  /* Convert from BlobProto to Blob<float> */
  Blob<float> mean_blob;
  mean_blob.FromProto(blob_proto);
  CHECK_EQ(mean_blob.channels(), num_channels_)
    << "Number of channels of mean file doesn't match input layer.";

  /* The format of the mean file is planar 32-bit float BGR or grayscale. */
  std::vector<cv::Mat> channels;
  float* data = mean_blob.mutable_cpu_data();
  for (int i = 0; i < num_channels_; ++i) {
    /* Extract an individual channel. */
    cv::Mat channel(mean_blob.height(), mean_blob.width(), CV_32FC1, data);
    channels.push_back(channel);
    data += mean_blob.height() * mean_blob.width();
  }

  /* Merge the separate channels into a single image. */
  cv::Mat mean;
  cv::merge(channels, mean);

  /* Compute the global mean pixel value and create a mean image
   * filled with this value. */
  cv::Scalar channel_mean = cv::mean(mean);
  mean_ = cv::Mat(input_geometry_, mean.type(), channel_mean);
}

std::vector<float> Classifier::Predict(const cv::Mat& img) {
  Blob<float>* input_layer = net_->input_blobs()[0];
  input_layer->Reshape(1, num_channels_,
                       input_geometry_.height, input_geometry_.width);
  /* Forward dimension change to all layers. */
  net_->Reshape();

  std::vector<cv::Mat> input_channels;
  WrapInputLayer(&input_channels);
  double mul=0.00390625;
  cv::Mat out_img=img*mul;
  Preprocess(out_img, &input_channels);

  net_->Forward();

  /* Copy the output layer to a std::vector */
  Blob<float>* output_layer = net_->output_blobs()[0];
  const float* begin = output_layer->cpu_data();
  const float* end = begin + output_layer->channels();
  return std::vector<float>(begin, end);
}

/* Wrap the input layer of the network in separate cv::Mat objects
 * (one per channel). This way we save one memcpy operation and we
 * don't need to rely on cudaMemcpy2D. The last preprocessing
 * operation will write the separate channels directly to the input
 * layer. */
void Classifier::WrapInputLayer(std::vector<cv::Mat>* input_channels) {
  Blob<float>* input_layer = net_->input_blobs()[0];

  int width = input_layer->width();
  int height = input_layer->height();
  float* input_data = input_layer->mutable_cpu_data();
  for (int i = 0; i < input_layer->channels(); ++i) {
    cv::Mat channel(height, width, CV_32FC1, input_data);
    input_channels->push_back(channel);
    input_data += width * height;
  }
}

void Classifier::Preprocess(const cv::Mat& img,
                            std::vector<cv::Mat>* input_channels) {
  /* Convert the input image to the input image format of the network. */
  cv::Mat sample;
  if (img.channels() == 3 && num_channels_ == 1)
    cv::cvtColor(img, sample, cv::COLOR_BGR2GRAY);
  else if (img.channels() == 4 && num_channels_ == 1)
    cv::cvtColor(img, sample, cv::COLOR_BGRA2GRAY);
  else if (img.channels() == 4 && num_channels_ == 3)
    cv::cvtColor(img, sample, cv::COLOR_BGRA2BGR);
  else if (img.channels() == 1 && num_channels_ == 3)
    cv::cvtColor(img, sample, cv::COLOR_GRAY2BGR);
  else
    sample = img;

  cv::Mat sample_resized;
  if (sample.size() != input_geometry_)
    cv::resize(sample, sample_resized, input_geometry_);
  else
    sample_resized = sample;

  cv::Mat sample_float;
  if (num_channels_ == 3)
    sample_resized.convertTo(sample_float, CV_32FC3);
  else
    sample_resized.convertTo(sample_float, CV_32FC1);

  cv::Mat sample_normalized;
  cv::subtract(sample_float, mean_, sample_normalized);

  /* This operation will write the separate BGR planes directly to the
   * input layer of the network because it is wrapped by the cv::Mat
   * objects in input_channels. */
  cv::split(sample_normalized, *input_channels);

  CHECK(reinterpret_cast<float*>(input_channels->at(0).data)
        == net_->input_blobs()[0]->cpu_data())
    << "Input channels are not wrapping the input layer of the network.";
}
vector<string> getFiles(string cate_dir)  
{
    vector<string> files;
    DIR *dir;  
    struct dirent *ptr;   

    if ((dir=opendir(cate_dir.c_str())) == NULL)  
        {  
        perror("Open dir error...");  
                exit(1);  
        }  

    while ((ptr=readdir(dir)) != NULL)  
    {  
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///current dir OR parrent dir  
                continue;  
        else if(ptr->d_type == 8)    ///file  
            //printf("d_name:%s/%s\n",basePath,ptr->d_name);  
            files.push_back(ptr->d_name);  
        else if(ptr->d_type == 10)    ///link file  
            //printf("d_name:%s/%s\n",basePath,ptr->d_name);  
            continue;  
        else if(ptr->d_type == 4)    ///dir  
        {  
            files.push_back(ptr->d_name);  
            /* 
                memset(base,'\0',sizeof(base)); 
                strcpy(base,basePath); 
                strcat(base,"/"); 
                strcat(base,ptr->d_nSame); 
                readFileList(base); 
            */  
        }  
    }  
    closedir(dir);    
    sort(files.begin(), files.end());  
    return files;  
}  
int main(int argc, char** argv) {
  if (argc != 5) {
    std::cerr << "Usage: " << argv[0]
              << " deploy.prototxt network.caffemodel"
              << " mean.binaryproto labels.txt img.jpg" << std::endl;
    return 1;
  }
  outFile.open("/home/zq/test.csv", ios::out); 
  ::google::InitGoogleLogging(argv[0]);

  string model_file   = argv[1];
  string trained_file = argv[2];
  string mean_file    = argv[3];
  string label_file   = argv[4];
  Classifier classifier(model_file, trained_file, mean_file, label_file);
  string filepath = "data/pigface/test/";
  vector<string> files; 
  files=getFiles(filepath);
  int size = files.size();
  cout << size << endl;  
  int j;
  for(j=0;j<size;j++)
  {
  cv::Mat img = cv::imread(filepath+files[j], -1);
  CHECK(!img.empty()) << "Unable to decode image " << files[j];
  std::vector<Prediction> predictions = classifier.Classify(img,files[j]);

  /* Print the top N predictions. 
  for (size_t i = 0; i < predictions.size(); ++i) {
    Prediction p = predictions[i];
    std::cout << std::fixed << std::setprecision(4) << p.second << " - \""
              << p.first << "\"" << std::endl;
  }
  */
  }
  outFile.close();
}
#else
int main(int argc, char** argv) {
  LOG(FATAL) << "This example requires OpenCV; compile with USE_OPENCV.";
}
#endif  // USE_OPENCV

相關推薦

C++獲取資料下面所有檔名

#include <iostream> #include <stdlib.h> #include <stdio.h> #include <string.h> #ifdef linux #incl

C++ 某個資料所有檔案

#include<iostream> #include<string> #include<io.h> #include<cstdio> #include<cstdlib> #include<cstring> using namespace

一個資料下面所有檔名包括資料

/** * 遍歷資料夾下所有的檔案,包括資料夾 * @param dirPath */ public static void printDirFiles(String dirPath, int index) {

c#一個資料下的所有檔案包括子資料

using System; using System.IO; class ListAllFilesDemo { public static void Main()    {     Console.Writ

遞迴某個資料及其所有資料下的所有檔案

public class MyTest5 {     public static void main(String[] args) {         String rootDir = "G:"+File.se

使用C++獲取資料所有檔名(windows環境)

由於經常有讀取一個資料夾中的很多隨機編號的檔案,很多時候需要讀取某些特定格式的所有檔案。 下面的程式碼可以讀取指定檔案家中的所有檔案和資料夾中格式為jpg的檔案 windows平臺程式碼:#if 1 #include <io.h> #include

Chrome本地資料所有檔案

前言 支援Chrome瀏覽器,不支援IE。IE可通過ActiveX方式輕鬆實現。其他系列瀏覽器未測試,親們測試了可以評論告訴我。 程式碼 <!DOCTYPE html> <html lang="zh"> <head> <me

C語言獲取資料所有檔名並列印到txt中

程式如下: /********************************************************************************************

C/C++ 獲取資料所有檔名 windows和linux通用

參考: ################################################# 利用C/C++編寫程式以獲取資料夾內所有子檔名,以下程式參考網路上諸多博文: 標頭檔案如下: #include <iostream> #inclu

C++獲取資料所有檔名稱的三種方式

1 利用dos命令把資料夾下所有檔名存入指定檔案,再從檔案讀取,存入vector中 string imglist_file = "test_result\\imglist.txt";//儲存檔名稱列表 string temp_imgname; stri

python 一個資料所有檔名稱

用python遍歷一個資料夾下所有檔名稱 可以用字典的方式,程式可參考程式“E:\untitled\weather\extact_copy.py” #coding=utf-8 import os import os.path rootdir = "E:\\weather_d

資料所有檔案,並輸出儲存在txt

#include<iostream> #include "io.h" #include<fstream> using namespace std; ofstream MyFile("FileName.txt"); bool transfer(st

Matlab獲取資料所有檔名並將資料按矩陣賦值給變數

fileFolder=fullfile('D:\MATLAB\bin\trc'); dirOutput=dir(fullfile(fileFolder,'*.trc')); fileNames={dirOutput.name}; n = 1; for i=fileNames

OpenCV獲取資料所有檔名

一、OpenCV中有實現遍歷資料夾下所有檔案的類Directory,它裡面包括3個成員函式:(1)、GetListFiles:遍歷指定資料夾下的所有檔案,不包括指定資料夾內的資料夾;(2)、GetListFolders:遍歷指定資料夾下的所有資料夾,不包括指定資料夾下的檔

利用python.os.listdir獲取資料所有檔名

利用內建os的listdir可以獲取資料夾中的檔名,從而可以利用檔名對資料做進一步的處理,比如:在貓狗識別的練習過程中,需要通過檔名對貓狗資料進行分類。 def get_file(file_dir): dogs = [] cats = []

Python初學-----資料獲取資料下面檔案的所有行數

一次特殊需求,需要獲取專案裡面程式碼檔案的行數,一個指令碼搞定 import os from os import path rootPath=os.getcwd() print(rootPath

c#遞迴指定資料內的所有檔案(包含子資料

c#程式碼: public class DirectoryAllFiles { static List<FileInformation> FileList = n

C#中指定資料下的檔案或子檔案

今天用到查詢給定路徑下的所有檔案,記錄一下: 1、引入IO名稱空間 using System.IO; 2、用到的主要方法: DirectoryInfo folder = new DirectoryInfo(filePath); DirectoryInfo.GetFiles():獲取目錄中所有

c++刷題 檔案檢視器(有格式輸出資料目錄)

主要思路:每一層都先有符號標記再遞迴 #include <iostream> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #in

C/C++獲取資料所有檔案的名字及全路徑

在實際中,會遇到需要通過程式獲取當前路徑下所有檔案的名稱,以下函式可以實現獲取所給路徑下檔名 及檔案對應的全路徑;需要包含文章末尾的兩個標頭檔案#include <vector> #include"dirent.h" #include"unistd.h" usi