classify.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright (c) 2009, Willow Garage, Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. * * Neither the name of Willow Garage, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. /**
  36. * @file demo_classify.cpp
  37. * @brief Feature extraction and classification.
  38. * @author Yida Wang
  39. */
  40. #include <opencv2/cnn_3dobj.hpp>
  41. #include <opencv2/features2d.hpp>
  42. #include <iomanip>
  43. using namespace cv;
  44. using namespace std;
  45. using namespace cv::cnn_3dobj;
  46. /**
  47. * @function listDir
  48. * @brief Making all files names under a directory into a list
  49. */
  50. static void listDir(const char *path, std::vector<String>& files, bool r)
  51. {
  52. DIR *pDir;
  53. struct dirent *ent;
  54. char childpath[512];
  55. pDir = opendir(path);
  56. memset(childpath, 0, sizeof(childpath));
  57. while ((ent = readdir(pDir)) != NULL)
  58. {
  59. if (ent->d_type & DT_DIR)
  60. {
  61. if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0 || strcmp(ent->d_name, ".DS_Store") == 0)
  62. {
  63. continue;
  64. }
  65. if (r)
  66. {
  67. sprintf(childpath, "%s/%s", path, ent->d_name);
  68. listDir(childpath,files,false);
  69. }
  70. }
  71. else
  72. {
  73. if (strcmp(ent->d_name, ".DS_Store") != 0)
  74. files.push_back(ent->d_name);
  75. }
  76. }
  77. sort(files.begin(),files.end());
  78. };
  79. /**
  80. * @function featureWrite
  81. * @brief Writing features of gallery images into binary files
  82. */
  83. static int featureWrite(const Mat &features, const String &fname)
  84. {
  85. ofstream ouF;
  86. ouF.open(fname.c_str(), std::ofstream::binary);
  87. if (!ouF)
  88. {
  89. cerr << "failed to open the file : " << fname << endl;
  90. return 0;
  91. }
  92. for (int r = 0; r < features.rows; r++)
  93. {
  94. ouF.write(reinterpret_cast<const char*>(features.ptr(r)), features.cols*features.elemSize());
  95. }
  96. ouF.close();
  97. return 1;
  98. }
  99. /**
  100. * @function main
  101. */
  102. int main(int argc, char** argv)
  103. {
  104. const String keys = "{help | | This sample will extract features from reference images and target image for classification. You can add a mean_file if there little variance in data such as human faces, otherwise it is not so useful}"
  105. "{src_dir | ../data/images_all/ | Source direction of the images ready for being used for extract feature as gallery.}"
  106. "{caffemodel | ../../testdata/cv/3d_triplet_iter_30000.caffemodel | caffe model for feature exrtaction.}"
  107. "{network_forIMG | ../../testdata/cv/3d_triplet_testIMG.prototxt | Network definition file used for extracting feature from a single image and making a classification}"
  108. "{mean_file | no | The mean file generated by Caffe from all gallery images, this could be used for mean value substraction from all images. If you want to use the mean file, you can set this as ../data/images_mean/triplet_mean.binaryproto.}"
  109. "{target_img | ../data/images_all/4_78.png | Path of image waiting to be classified.}"
  110. "{feature_blob | feat | Name of layer which will represent as the feature, in this network, ip1 or feat is well.}"
  111. "{num_candidate | 15 | Number of candidates in gallery as the prediction result.}"
  112. "{device | CPU | Device type: CPU or GPU}"
  113. "{dev_id | 0 | Device id}"
  114. "{gallery_out | 0 | Option on output binary features on gallery images}";
  115. /* get parameters from comand line */
  116. cv::CommandLineParser parser(argc, argv, keys);
  117. parser.about("Feature extraction and classification");
  118. if (parser.has("help"))
  119. {
  120. parser.printMessage();
  121. return 0;
  122. }
  123. String src_dir = parser.get<String>("src_dir");
  124. String caffemodel = parser.get<String>("caffemodel");
  125. String network_forIMG = parser.get<String>("network_forIMG");
  126. String mean_file = parser.get<String>("mean_file");
  127. String target_img = parser.get<String>("target_img");
  128. String feature_blob = parser.get<String>("feature_blob");
  129. int num_candidate = parser.get<int>("num_candidate");
  130. String device = parser.get<String>("device");
  131. int gallery_out = parser.get<int>("gallery_out");
  132. /* Initialize a net work with Device */
  133. cv::cnn_3dobj::descriptorExtractor descriptor(device);
  134. std::cout << "Using" << descriptor.getDeviceType() << std::endl;
  135. /* Load net with the caffe trained net work parameter and structure */
  136. if (strcmp(mean_file.c_str(), "no") == 0)
  137. descriptor.loadNet(network_forIMG, caffemodel);
  138. else
  139. descriptor.loadNet(network_forIMG, caffemodel, mean_file);
  140. std::vector<String> name_gallery;
  141. /* List the file names under a given path */
  142. listDir(src_dir.c_str(), name_gallery, false);
  143. if (gallery_out)
  144. {
  145. ofstream namelist_out("gallelist.txt");
  146. /* Writing name of the reference images. */
  147. for (unsigned int i = 0; i < name_gallery.size(); i++)
  148. namelist_out << name_gallery.at(i) << endl;
  149. }
  150. for (unsigned int i = 0; i < name_gallery.size(); i++)
  151. {
  152. name_gallery[i] = src_dir + name_gallery[i];
  153. }
  154. std::vector<cv::Mat> img_gallery;
  155. cv::Mat feature_reference;
  156. for (unsigned int i = 0; i < name_gallery.size(); i++)
  157. {
  158. img_gallery.push_back(cv::imread(name_gallery[i]));
  159. }
  160. /* Extract feature from a set of images */
  161. descriptor.extract(img_gallery, feature_reference, feature_blob);
  162. if (gallery_out)
  163. {
  164. std::cout << std::endl << "---------- Features of gallery images ----------" << std::endl;
  165. /* Print features of the reference images. */
  166. for (int i = 0; i < feature_reference.rows; i++)
  167. std::cout << feature_reference.row(i) << endl;
  168. std::cout << std::endl << "---------- Saving features of gallery images into feature.bin ----------" << std::endl;
  169. featureWrite(feature_reference, "feature.bin");
  170. }
  171. else
  172. {
  173. std::cout << std::endl << "---------- Prediction for " << target_img << " ----------" << std::endl;
  174. cv::Mat img = cv::imread(target_img);
  175. std::cout << std::endl << "---------- Features of gallery images ----------" << std::endl;
  176. std::vector<std::pair<String, float> > prediction;
  177. /* Print features of the reference images. */
  178. for (int i = 0; i < feature_reference.rows; i++)
  179. std::cout << feature_reference.row(i) << endl;
  180. cv::Mat feature_test;
  181. descriptor.extract(img, feature_test, feature_blob);
  182. /* Initialize a matcher which using L2 distance. */
  183. cv::BFMatcher matcher(NORM_L2);
  184. std::vector<std::vector<cv::DMatch> > matches;
  185. /* Have a KNN match on the target and reference images. */
  186. matcher.knnMatch(feature_test, feature_reference, matches, num_candidate);
  187. /* Print feature of the target image waiting to be classified. */
  188. std::cout << std::endl << "---------- Features of target image: " << target_img << "----------" << endl << feature_test << std::endl;
  189. /* Print the top N prediction. */
  190. std::cout << std::endl << "---------- Prediction result(Distance - File Name in Gallery) ----------" << std::endl;
  191. for (size_t i = 0; i < matches[0].size(); ++i)
  192. {
  193. std::cout << i << " - " << std::fixed << std::setprecision(2) << name_gallery[matches[0][i].trainIdx] << " - \"" << matches[0][i].distance << "\"" << std::endl;
  194. }
  195. }
  196. return 0;
  197. }