FaceRecognizer.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //
  2. // This file is auto-generated. Please don't modify it!
  3. //
  4. #pragma once
  5. #ifdef __cplusplus
  6. //#import "opencv.hpp"
  7. #import "opencv2/face.hpp"
  8. #else
  9. #define CV_EXPORTS
  10. #endif
  11. #import <Foundation/Foundation.h>
  12. #import "Algorithm.h"
  13. @class IntVector;
  14. @class Mat;
  15. @class PredictCollector;
  16. NS_ASSUME_NONNULL_BEGIN
  17. // C++: class FaceRecognizer
  18. /**
  19. * Abstract base class for all face recognition models
  20. *
  21. * All face recognition models in OpenCV are derived from the abstract base class FaceRecognizer, which
  22. * provides a unified access to all face recongition algorithms in OpenCV.
  23. *
  24. * ### Description
  25. *
  26. * I'll go a bit more into detail explaining FaceRecognizer, because it doesn't look like a powerful
  27. * interface at first sight. But: Every FaceRecognizer is an Algorithm, so you can easily get/set all
  28. * model internals (if allowed by the implementation). Algorithm is a relatively new OpenCV concept,
  29. * which is available since the 2.4 release. I suggest you take a look at its description.
  30. *
  31. * Algorithm provides the following features for all derived classes:
  32. *
  33. * - So called "virtual constructor". That is, each Algorithm derivative is registered at program
  34. * start and you can get the list of registered algorithms and create instance of a particular
  35. * algorithm by its name (see Algorithm::create). If you plan to add your own algorithms, it is
  36. * good practice to add a unique prefix to your algorithms to distinguish them from other
  37. * algorithms.
  38. * - Setting/Retrieving algorithm parameters by name. If you used video capturing functionality from
  39. * OpenCV highgui module, you are probably familar with cv::cvSetCaptureProperty,
  40. * ocvcvGetCaptureProperty, VideoCapture::set and VideoCapture::get. Algorithm provides similar
  41. * method where instead of integer id's you specify the parameter names as text Strings. See
  42. * Algorithm::set and Algorithm::get for details.
  43. * - Reading and writing parameters from/to XML or YAML files. Every Algorithm derivative can store
  44. * all its parameters and then read them back. There is no need to re-implement it each time.
  45. *
  46. * Moreover every FaceRecognizer supports the:
  47. *
  48. * - **Training** of a FaceRecognizer with FaceRecognizer::train on a given set of images (your face
  49. * database!).
  50. * - **Prediction** of a given sample image, that means a face. The image is given as a Mat.
  51. * - **Loading/Saving** the model state from/to a given XML or YAML.
  52. * - **Setting/Getting labels info**, that is stored as a string. String labels info is useful for
  53. * keeping names of the recognized people.
  54. *
  55. * NOTE: When using the FaceRecognizer interface in combination with Python, please stick to Python 2.
  56. * Some underlying scripts like create_csv will not work in other versions, like Python 3. Setting the
  57. * Thresholds +++++++++++++++++++++++
  58. *
  59. * Sometimes you run into the situation, when you want to apply a threshold on the prediction. A common
  60. * scenario in face recognition is to tell, whether a face belongs to the training dataset or if it is
  61. * unknown. You might wonder, why there's no public API in FaceRecognizer to set the threshold for the
  62. * prediction, but rest assured: It's supported. It just means there's no generic way in an abstract
  63. * class to provide an interface for setting/getting the thresholds of *every possible* FaceRecognizer
  64. * algorithm. The appropriate place to set the thresholds is in the constructor of the specific
  65. * FaceRecognizer and since every FaceRecognizer is a Algorithm (see above), you can get/set the
  66. * thresholds at runtime!
  67. *
  68. * Here is an example of setting a threshold for the Eigenfaces method, when creating the model:
  69. *
  70. *
  71. * // Let's say we want to keep 10 Eigenfaces and have a threshold value of 10.0
  72. * int num_components = 10;
  73. * double threshold = 10.0;
  74. * // Then if you want to have a cv::FaceRecognizer with a confidence threshold,
  75. * // create the concrete implementation with the appropriate parameters:
  76. * Ptr<FaceRecognizer> model = EigenFaceRecognizer::create(num_components, threshold);
  77. *
  78. *
  79. * Sometimes it's impossible to train the model, just to experiment with threshold values. Thanks to
  80. * Algorithm it's possible to set internal model thresholds during runtime. Let's see how we would
  81. * set/get the prediction for the Eigenface model, we've created above:
  82. *
  83. *
  84. * // The following line reads the threshold from the Eigenfaces model:
  85. * double current_threshold = model->getDouble("threshold");
  86. * // And this line sets the threshold to 0.0:
  87. * model->set("threshold", 0.0);
  88. *
  89. *
  90. * If you've set the threshold to 0.0 as we did above, then:
  91. *
  92. *
  93. * //
  94. * Mat img = imread("person1/3.jpg", IMREAD_GRAYSCALE);
  95. * // Get a prediction from the model. Note: We've set a threshold of 0.0 above,
  96. * // since the distance is almost always larger than 0.0, you'll get -1 as
  97. * // label, which indicates, this face is unknown
  98. * int predicted_label = model->predict(img);
  99. * // ...
  100. *
  101. *
  102. * is going to yield -1 as predicted label, which states this face is unknown.
  103. *
  104. * ### Getting the name of a FaceRecognizer
  105. *
  106. * Since every FaceRecognizer is a Algorithm, you can use Algorithm::name to get the name of a
  107. * FaceRecognizer:
  108. *
  109. *
  110. * // Create a FaceRecognizer:
  111. * Ptr<FaceRecognizer> model = EigenFaceRecognizer::create();
  112. * // And here's how to get its name:
  113. * String name = model->name();
  114. *
  115. *
  116. * Member of `Face`
  117. */
  118. CV_EXPORTS @interface FaceRecognizer : Algorithm
  119. #ifdef __cplusplus
  120. @property(readonly)cv::Ptr<cv::face::FaceRecognizer> nativePtrFaceRecognizer;
  121. #endif
  122. #ifdef __cplusplus
  123. - (instancetype)initWithNativePtr:(cv::Ptr<cv::face::FaceRecognizer>)nativePtr;
  124. + (instancetype)fromNative:(cv::Ptr<cv::face::FaceRecognizer>)nativePtr;
  125. #endif
  126. #pragma mark - Methods
  127. //
  128. // void cv::face::FaceRecognizer::train(vector_Mat src, Mat labels)
  129. //
  130. /**
  131. * Trains a FaceRecognizer with given data and associated labels.
  132. *
  133. * @param src The training images, that means the faces you want to learn. The data has to be
  134. * given as a vector\<Mat\>.
  135. * @param labels The labels corresponding to the images have to be given either as a vector\<int\>
  136. * or a Mat of type CV_32SC1.
  137. *
  138. * The following source code snippet shows you how to learn a Fisherfaces model on a given set of
  139. * images. The images are read with imread and pushed into a std::vector\<Mat\>. The labels of each
  140. * image are stored within a std::vector\<int\> (you could also use a Mat of type CV_32SC1). Think of
  141. * the label as the subject (the person) this image belongs to, so same subjects (persons) should have
  142. * the same label. For the available FaceRecognizer you don't have to pay any attention to the order of
  143. * the labels, just make sure same persons have the same label:
  144. *
  145. *
  146. * // holds images and labels
  147. * vector<Mat> images;
  148. * vector<int> labels;
  149. * // using Mat of type CV_32SC1
  150. * // Mat labels(number_of_samples, 1, CV_32SC1);
  151. * // images for first person
  152. * images.push_back(imread("person0/0.jpg", IMREAD_GRAYSCALE)); labels.push_back(0);
  153. * images.push_back(imread("person0/1.jpg", IMREAD_GRAYSCALE)); labels.push_back(0);
  154. * images.push_back(imread("person0/2.jpg", IMREAD_GRAYSCALE)); labels.push_back(0);
  155. * // images for second person
  156. * images.push_back(imread("person1/0.jpg", IMREAD_GRAYSCALE)); labels.push_back(1);
  157. * images.push_back(imread("person1/1.jpg", IMREAD_GRAYSCALE)); labels.push_back(1);
  158. * images.push_back(imread("person1/2.jpg", IMREAD_GRAYSCALE)); labels.push_back(1);
  159. *
  160. *
  161. * Now that you have read some images, we can create a new FaceRecognizer. In this example I'll create
  162. * a Fisherfaces model and decide to keep all of the possible Fisherfaces:
  163. *
  164. *
  165. * // Create a new Fisherfaces model and retain all available Fisherfaces,
  166. * // this is the most common usage of this specific FaceRecognizer:
  167. * //
  168. * Ptr<FaceRecognizer> model = FisherFaceRecognizer::create();
  169. *
  170. *
  171. * And finally train it on the given dataset (the face images and labels):
  172. *
  173. *
  174. * // This is the common interface to train all of the available cv::FaceRecognizer
  175. * // implementations:
  176. * //
  177. * model->train(images, labels);
  178. *
  179. */
  180. - (void)train:(NSArray<Mat*>*)src labels:(Mat*)labels NS_SWIFT_NAME(train(src:labels:));
  181. //
  182. // void cv::face::FaceRecognizer::update(vector_Mat src, Mat labels)
  183. //
  184. /**
  185. * Updates a FaceRecognizer with given data and associated labels.
  186. *
  187. * @param src The training images, that means the faces you want to learn. The data has to be given
  188. * as a vector\<Mat\>.
  189. * @param labels The labels corresponding to the images have to be given either as a vector\<int\> or
  190. * a Mat of type CV_32SC1.
  191. *
  192. * This method updates a (probably trained) FaceRecognizer, but only if the algorithm supports it. The
  193. * Local Binary Patterns Histograms (LBPH) recognizer (see createLBPHFaceRecognizer) can be updated.
  194. * For the Eigenfaces and Fisherfaces method, this is algorithmically not possible and you have to
  195. * re-estimate the model with FaceRecognizer::train. In any case, a call to train empties the existing
  196. * model and learns a new model, while update does not delete any model data.
  197. *
  198. *
  199. * // Create a new LBPH model (it can be updated) and use the default parameters,
  200. * // this is the most common usage of this specific FaceRecognizer:
  201. * //
  202. * Ptr<FaceRecognizer> model = LBPHFaceRecognizer::create();
  203. * // This is the common interface to train all of the available cv::FaceRecognizer
  204. * // implementations:
  205. * //
  206. * model->train(images, labels);
  207. * // Some containers to hold new image:
  208. * vector<Mat> newImages;
  209. * vector<int> newLabels;
  210. * // You should add some images to the containers:
  211. * //
  212. * // ...
  213. * //
  214. * // Now updating the model is as easy as calling:
  215. * model->update(newImages,newLabels);
  216. * // This will preserve the old model data and extend the existing model
  217. * // with the new features extracted from newImages!
  218. *
  219. *
  220. * Calling update on an Eigenfaces model (see EigenFaceRecognizer::create), which doesn't support
  221. * updating, will throw an error similar to:
  222. *
  223. *
  224. * OpenCV Error: The function/feature is not implemented (This FaceRecognizer (FaceRecognizer.Eigenfaces) does not support updating, you have to use FaceRecognizer::train to update it.) in update, file /home/philipp/git/opencv/modules/contrib/src/facerec.cpp, line 305
  225. * terminate called after throwing an instance of 'cv::Exception'
  226. *
  227. *
  228. * NOTE: The FaceRecognizer does not store your training images, because this would be very
  229. * memory intense and it's not the responsibility of te FaceRecognizer to do so. The caller is
  230. * responsible for maintaining the dataset, he want to work with.
  231. */
  232. - (void)update:(NSArray<Mat*>*)src labels:(Mat*)labels NS_SWIFT_NAME(update(src:labels:));
  233. //
  234. // int cv::face::FaceRecognizer::predict(Mat src)
  235. //
  236. - (int)predict_label:(Mat*)src NS_SWIFT_NAME(predict(src:));
  237. //
  238. // void cv::face::FaceRecognizer::predict(Mat src, int& label, double& confidence)
  239. //
  240. /**
  241. * Predicts a label and associated confidence (e.g. distance) for a given input image.
  242. *
  243. * @param src Sample image to get a prediction from.
  244. * @param label The predicted label for the given image.
  245. * @param confidence Associated confidence (e.g. distance) for the predicted label.
  246. *
  247. * The suffix const means that prediction does not affect the internal model state, so the method can
  248. * be safely called from within different threads.
  249. *
  250. * The following example shows how to get a prediction from a trained model:
  251. *
  252. *
  253. * using namespace cv;
  254. * // Do your initialization here (create the cv::FaceRecognizer model) ...
  255. * // ...
  256. * // Read in a sample image:
  257. * Mat img = imread("person1/3.jpg", IMREAD_GRAYSCALE);
  258. * // And get a prediction from the cv::FaceRecognizer:
  259. * int predicted = model->predict(img);
  260. *
  261. *
  262. * Or to get a prediction and the associated confidence (e.g. distance):
  263. *
  264. *
  265. * using namespace cv;
  266. * // Do your initialization here (create the cv::FaceRecognizer model) ...
  267. * // ...
  268. * Mat img = imread("person1/3.jpg", IMREAD_GRAYSCALE);
  269. * // Some variables for the predicted label and associated confidence (e.g. distance):
  270. * int predicted_label = -1;
  271. * double predicted_confidence = 0.0;
  272. * // Get the prediction and associated confidence from the model
  273. * model->predict(img, predicted_label, predicted_confidence);
  274. *
  275. */
  276. - (void)predict:(Mat*)src label:(int*)label confidence:(double*)confidence NS_SWIFT_NAME(predict(src:label:confidence:));
  277. //
  278. // void cv::face::FaceRecognizer::predict(Mat src, Ptr_PredictCollector collector)
  279. //
  280. /**
  281. * - if implemented - send all result of prediction to collector that can be used for somehow custom result handling
  282. * @param src Sample image to get a prediction from.
  283. * @param collector User-defined collector object that accepts all results
  284. *
  285. * To implement this method u just have to do same internal cycle as in predict(InputArray src, CV_OUT int &label, CV_OUT double &confidence) but
  286. * not try to get "best@ result, just resend it to caller side with given collector
  287. */
  288. - (void)predict_collect:(Mat*)src collector:(PredictCollector*)collector NS_SWIFT_NAME(predict(src:collector:));
  289. //
  290. // void cv::face::FaceRecognizer::write(String filename)
  291. //
  292. /**
  293. * Saves a FaceRecognizer and its model state.
  294. *
  295. * Saves this model to a given filename, either as XML or YAML.
  296. * @param filename The filename to store this FaceRecognizer to (either XML/YAML).
  297. *
  298. * Every FaceRecognizer overwrites FaceRecognizer::save(FileStorage& fs) to save the internal model
  299. * state. FaceRecognizer::save(const String& filename) saves the state of a model to the given
  300. * filename.
  301. *
  302. * The suffix const means that prediction does not affect the internal model state, so the method can
  303. * be safely called from within different threads.
  304. */
  305. - (void)write:(NSString*)filename NS_SWIFT_NAME(write(filename:));
  306. //
  307. // void cv::face::FaceRecognizer::read(String filename)
  308. //
  309. /**
  310. * Loads a FaceRecognizer and its model state.
  311. *
  312. * Loads a persisted model and state from a given XML or YAML file . Every FaceRecognizer has to
  313. * overwrite FaceRecognizer::load(FileStorage& fs) to enable loading the model state.
  314. * FaceRecognizer::load(FileStorage& fs) in turn gets called by
  315. * FaceRecognizer::load(const String& filename), to ease saving a model.
  316. */
  317. - (void)read:(NSString*)filename NS_SWIFT_NAME(read(filename:));
  318. //
  319. // void cv::face::FaceRecognizer::setLabelInfo(int label, String strInfo)
  320. //
  321. /**
  322. * Sets string info for the specified model's label.
  323. *
  324. * The string info is replaced by the provided value if it was set before for the specified label.
  325. */
  326. - (void)setLabelInfo:(int)label strInfo:(NSString*)strInfo NS_SWIFT_NAME(setLabelInfo(label:strInfo:));
  327. //
  328. // String cv::face::FaceRecognizer::getLabelInfo(int label)
  329. //
  330. /**
  331. * Gets string information by label.
  332. *
  333. * If an unknown label id is provided or there is no label information associated with the specified
  334. * label id the method returns an empty string.
  335. */
  336. - (NSString*)getLabelInfo:(int)label NS_SWIFT_NAME(getLabelInfo(label:));
  337. //
  338. // vector_int cv::face::FaceRecognizer::getLabelsByString(String str)
  339. //
  340. /**
  341. * Gets vector of labels by string.
  342. *
  343. * The function searches for the labels containing the specified sub-string in the associated string
  344. * info.
  345. */
  346. - (IntVector*)getLabelsByString:(NSString*)str NS_SWIFT_NAME(getLabelsByString(str:));
  347. @end
  348. NS_ASSUME_NONNULL_END