Facemark.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.FaceModule
  7. {
  8. // C++: class Facemark
  9. /**
  10. * Abstract base class for all facemark models
  11. *
  12. * To utilize this API in your program, please take a look at the REF: tutorial_table_of_content_facemark
  13. * ### Description
  14. *
  15. * Facemark is a base class which provides universal access to any specific facemark algorithm.
  16. * Therefore, the users should declare a desired algorithm before they can use it in their application.
  17. *
  18. * Here is an example on how to declare a facemark algorithm:
  19. * <code>
  20. * // Using Facemark in your code:
  21. * Ptr&lt;Facemark&gt; facemark = createFacemarkLBF();
  22. * </code>
  23. *
  24. * The typical pipeline for facemark detection is as follows:
  25. * <ul>
  26. * <li>
  27. * Load the trained model using Facemark::loadModel.
  28. * </li>
  29. * <li>
  30. * Perform the fitting on an image via Facemark::fit.
  31. * </li>
  32. * </ul>
  33. */
  34. public class Facemark : Algorithm
  35. {
  36. protected override void Dispose(bool disposing)
  37. {
  38. try
  39. {
  40. if (disposing)
  41. {
  42. }
  43. if (IsEnabledDispose)
  44. {
  45. if (nativeObj != IntPtr.Zero)
  46. face_Facemark_delete(nativeObj);
  47. nativeObj = IntPtr.Zero;
  48. }
  49. }
  50. finally
  51. {
  52. base.Dispose(disposing);
  53. }
  54. }
  55. protected internal Facemark(IntPtr addr) : base(addr) { }
  56. // internal usage only
  57. public static new Facemark __fromPtr__(IntPtr addr) { return new Facemark(addr); }
  58. //
  59. // C++: void cv::face::Facemark::loadModel(String model)
  60. //
  61. /**
  62. * A function to load the trained model before the fitting process.
  63. * param model A string represent the filename of a trained model.
  64. *
  65. * &lt;B&gt;Example of usage&lt;/B&gt;
  66. * <code>
  67. * facemark-&gt;loadModel("../data/lbf.model");
  68. * </code>
  69. */
  70. public void loadModel(string model)
  71. {
  72. ThrowIfDisposed();
  73. face_Facemark_loadModel_10(nativeObj, model);
  74. }
  75. //
  76. // C++: bool cv::face::Facemark::fit(Mat image, vector_Rect faces, vector_vector_Point2f& landmarks)
  77. //
  78. /**
  79. * Detect facial landmarks from an image.
  80. * param image Input image.
  81. * param faces Output of the function which represent region of interest of the detected faces.
  82. * Each face is stored in cv::Rect container.
  83. * param landmarks The detected landmark points for each faces.
  84. *
  85. * &lt;B&gt;Example of usage&lt;/B&gt;
  86. * <code>
  87. * Mat image = imread("image.jpg");
  88. * std::vector&lt;Rect&gt; faces;
  89. * std::vector&lt;std::vector&lt;Point2f&gt; &gt; landmarks;
  90. * facemark-&gt;fit(image, faces, landmarks);
  91. * </code>
  92. * return automatically generated
  93. */
  94. public bool fit(Mat image, MatOfRect faces, List<MatOfPoint2f> landmarks)
  95. {
  96. ThrowIfDisposed();
  97. if (image != null) image.ThrowIfDisposed();
  98. if (faces != null) faces.ThrowIfDisposed();
  99. Mat faces_mat = faces;
  100. Mat landmarks_mat = new Mat();
  101. bool retVal = face_Facemark_fit_10(nativeObj, image.nativeObj, faces_mat.nativeObj, landmarks_mat.nativeObj);
  102. Converters.Mat_to_vector_vector_Point2f(landmarks_mat, landmarks);
  103. landmarks_mat.release();
  104. return retVal;
  105. }
  106. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  107. const string LIBNAME = "__Internal";
  108. #else
  109. const string LIBNAME = "opencvforunity";
  110. #endif
  111. // C++: void cv::face::Facemark::loadModel(String model)
  112. [DllImport(LIBNAME)]
  113. private static extern void face_Facemark_loadModel_10(IntPtr nativeObj, string model);
  114. // C++: bool cv::face::Facemark::fit(Mat image, vector_Rect faces, vector_vector_Point2f& landmarks)
  115. [DllImport(LIBNAME)]
  116. [return: MarshalAs(UnmanagedType.U1)]
  117. private static extern bool face_Facemark_fit_10(IntPtr nativeObj, IntPtr image_nativeObj, IntPtr faces_mat_nativeObj, IntPtr landmarks_mat_nativeObj);
  118. // native support for java finalize()
  119. [DllImport(LIBNAME)]
  120. private static extern void face_Facemark_delete(IntPtr nativeObj);
  121. }
  122. }