lebeg commented on a change in pull request #9799: Cleaned up image 
classification cpp example
URL: https://github.com/apache/incubator-mxnet/pull/9799#discussion_r168725663
 
 

 ##########
 File path: 
example/image-classification/predict-cpp/image-classification-predict.cc
 ##########
 @@ -32,252 +48,254 @@ const mx_float DEFAULT_MEAN = 117.0;
 // Read file to buffer
 class BufferFile {
  public :
-    std::string file_path_;
-    int length_;
-    char* buffer_;
-
-    explicit BufferFile(std::string file_path)
-    :file_path_(file_path) {
-
-        std::ifstream ifs(file_path.c_str(), std::ios::in | std::ios::binary);
-        if (!ifs) {
-            std::cerr << "Can't open the file. Please check " << file_path << 
". \n";
-            length_ = 0;
-            buffer_ = NULL;
-            return;
-        }
+  std::string file_path_;
+  int length_;
+  char* buffer_;
+
+  explicit BufferFile(std::string file_path)
+    : file_path_(file_path) {
+
+    std::ifstream ifs(file_path.c_str(), std::ios::in | std::ios::binary);
+    if (!ifs) {
+      std::cerr << "Can't open the file. Please check " << file_path << ". \n";
+      length_ = 0;
+      buffer_ = NULL;
+      return;
+    }
 
-        ifs.seekg(0, std::ios::end);
-        length_ = ifs.tellg();
-        ifs.seekg(0, std::ios::beg);
-        std::cout << file_path.c_str() << " ... "<< length_ << " bytes\n";
+    ifs.seekg(0, std::ios::end);
+    length_ = ifs.tellg();
+    ifs.seekg(0, std::ios::beg);
+    std::cout << file_path.c_str() << " ... " << length_ << " bytes\n";
 
-        buffer_ = new char[sizeof(char) * length_];
-        ifs.read(buffer_, length_);
-        ifs.close();
-    }
+    buffer_ = new char[sizeof(char) * length_];
+    ifs.read(buffer_, length_);
+    ifs.close();
+  }
 
-    int GetLength() {
-        return length_;
-    }
-    char* GetBuffer() {
-        return buffer_;
-    }
+  int GetLength() {
+    return length_;
+  }
 
-    ~BufferFile() {
-        if (buffer_) {
-          delete[] buffer_;
-          buffer_ = NULL;
-        }
+  char* GetBuffer() {
+    return buffer_;
+  }
+
+  ~BufferFile() {
+    if (buffer_) {
+      delete[] buffer_;
+      buffer_ = NULL;
     }
+  }
 };
 
 void GetImageFile(const std::string image_file,
                   mx_float* image_data, const int channels,
                   const cv::Size resize_size, const mx_float* mean_data = 
nullptr) {
-    // Read all kinds of file into a BGR color 3 channels image
-    cv::Mat im_ori = cv::imread(image_file, cv::IMREAD_COLOR);
+  // Read all kinds of file into a BGR color 3 channels image
+  cv::Mat im_ori = cv::imread(image_file, cv::IMREAD_COLOR);
 
-    if (im_ori.empty()) {
-        std::cerr << "Can't open the image. Please check " << image_file << ". 
\n";
-        assert(false);
-    }
-
-    cv::Mat im;
+  if (im_ori.empty()) {
+    std::cerr << "Can't open the image. Please check " << image_file << ". \n";
+    assert(false);
+  }
 
-    resize(im_ori, im, resize_size);
+  cv::Mat im;
 
-    int size = im.rows * im.cols * channels;
+  resize(im_ori, im, resize_size);
 
-    mx_float* ptr_image_r = image_data;
-    mx_float* ptr_image_g = image_data + size / 3;
-    mx_float* ptr_image_b = image_data + size / 3 * 2;
+  int size = im.rows * im.cols * channels;
 
-    float mean_b, mean_g, mean_r;
-    mean_b = mean_g = mean_r = DEFAULT_MEAN;
+  mx_float* ptr_image_r = image_data;
+  mx_float* ptr_image_g = image_data + size / 3;
+  mx_float* ptr_image_b = image_data + size / 3 * 2;
 
-    for (int i = 0; i < im.rows; i++) {
-        uchar* data = im.ptr<uchar>(i);
+  float mean_b, mean_g, mean_r;
+  mean_b = mean_g = mean_r = DEFAULT_MEAN;
 
-        for (int j = 0; j < im.cols; j++) {
-            if (mean_data) {
-                mean_r = *mean_data;
-                if (channels > 1) {
-                    mean_g = *(mean_data + size / 3);
-                    mean_b = *(mean_data + size / 3 * 2);
-                }
-               mean_data++;
-            }
-            if (channels > 1) {
-                *ptr_image_b++ = static_cast<mx_float>(*data++) - mean_b;
-                *ptr_image_g++ = static_cast<mx_float>(*data++) - mean_g;
-            }
+  for (int i = 0; i < im.rows; i++) {
+    uchar* data = im.ptr<uchar>(i);
 
-            *ptr_image_r++ = static_cast<mx_float>(*data++) - mean_r;;
+    for (int j = 0; j < im.cols; j++) {
+      if (mean_data) {
+        mean_r = *mean_data;
+        if (channels > 1) {
+          mean_g = *(mean_data + size / 3);
+          mean_b = *(mean_data + size / 3 * 2);
         }
+        mean_data++;
+      }
+      if (channels > 1) {
+        *ptr_image_b++ = static_cast<mx_float>(*data++) - mean_b;
+        *ptr_image_g++ = static_cast<mx_float>(*data++) - mean_g;
+      }
+
+      *ptr_image_r++ = static_cast<mx_float>(*data++) - mean_r;;
     }
+  }
 }
 
 // LoadSynsets
 // Code from : 
https://github.com/pertusa/mxnet_predict_cc/blob/master/mxnet_predict.cc
-std::vector<std::string> LoadSynset(std::string synset_file) {
-    std::ifstream fi(synset_file.c_str());
+std::vector <std::string> LoadSynset(std::string synset_file) {
+  std::ifstream fi(synset_file.c_str());
 
-    if ( !fi.is_open() ) {
-        std::cerr << "Error opening synset file " << synset_file << std::endl;
-        assert(false);
-    }
+  if (!fi.is_open()) {
+    std::cerr << "Error opening synset file " << synset_file << std::endl;
+    assert(false);
+  }
 
-    std::vector<std::string> output;
+  std::vector <std::string> output;
 
 Review comment:
   Thanks, this was a weird default setting on a new machine

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to