TextDetectorCNN.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #if !UNITY_WSA_10_0
  2. using OpenCVForUnity.CoreModule;
  3. using OpenCVForUnity.UtilsModule;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. namespace OpenCVForUnity.TextModule
  8. {
  9. // C++: class TextDetectorCNN
  10. /**
  11. * TextDetectorCNN class provides the functionallity of text bounding box detection.
  12. * This class is representing to find bounding boxes of text words given an input image.
  13. * This class uses OpenCV dnn module to load pre-trained model described in CITE: LiaoSBWL17.
  14. * The original repository with the modified SSD Caffe version: https://github.com/MhLiao/TextBoxes.
  15. * Model can be downloaded from [DropBox](https://www.dropbox.com/s/g8pjzv2de9gty8g/TextBoxes_icdar13.caffemodel?dl=0).
  16. * Modified .prototxt file with the model description can be found in {code opencv_contrib/modules/text/samples/textbox.prototxt}.
  17. */
  18. public class TextDetectorCNN : TextDetector
  19. {
  20. protected override void Dispose(bool disposing)
  21. {
  22. try
  23. {
  24. if (disposing)
  25. {
  26. }
  27. if (IsEnabledDispose)
  28. {
  29. if (nativeObj != IntPtr.Zero)
  30. text_TextDetectorCNN_delete(nativeObj);
  31. nativeObj = IntPtr.Zero;
  32. }
  33. }
  34. finally
  35. {
  36. base.Dispose(disposing);
  37. }
  38. }
  39. protected internal TextDetectorCNN(IntPtr addr) : base(addr) { }
  40. // internal usage only
  41. public static new TextDetectorCNN __fromPtr__(IntPtr addr) { return new TextDetectorCNN(addr); }
  42. //
  43. // C++: void cv::text::TextDetectorCNN::detect(Mat inputImage, vector_Rect& Bbox, vector_float& confidence)
  44. //
  45. /**
  46. *
  47. *
  48. * param inputImage an image expected to be a CV_U8C3 of any size
  49. * param Bbox a vector of Rect that will store the detected word bounding box
  50. * param confidence a vector of float that will be updated with the confidence the classifier has for the selected bounding box
  51. */
  52. public override void detect(Mat inputImage, MatOfRect Bbox, MatOfFloat confidence)
  53. {
  54. ThrowIfDisposed();
  55. if (inputImage != null) inputImage.ThrowIfDisposed();
  56. if (Bbox != null) Bbox.ThrowIfDisposed();
  57. if (confidence != null) confidence.ThrowIfDisposed();
  58. Mat Bbox_mat = Bbox;
  59. Mat confidence_mat = confidence;
  60. text_TextDetectorCNN_detect_10(nativeObj, inputImage.nativeObj, Bbox_mat.nativeObj, confidence_mat.nativeObj);
  61. }
  62. //
  63. // C++: static Ptr_TextDetectorCNN cv::text::TextDetectorCNN::create(String modelArchFilename, String modelWeightsFilename)
  64. //
  65. public static TextDetectorCNN create(string modelArchFilename, string modelWeightsFilename)
  66. {
  67. return TextDetectorCNN.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(text_TextDetectorCNN_create_10(modelArchFilename, modelWeightsFilename)));
  68. }
  69. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  70. const string LIBNAME = "__Internal";
  71. #else
  72. const string LIBNAME = "opencvforunity";
  73. #endif
  74. // C++: void cv::text::TextDetectorCNN::detect(Mat inputImage, vector_Rect& Bbox, vector_float& confidence)
  75. [DllImport(LIBNAME)]
  76. private static extern void text_TextDetectorCNN_detect_10(IntPtr nativeObj, IntPtr inputImage_nativeObj, IntPtr Bbox_mat_nativeObj, IntPtr confidence_mat_nativeObj);
  77. // C++: static Ptr_TextDetectorCNN cv::text::TextDetectorCNN::create(String modelArchFilename, String modelWeightsFilename)
  78. [DllImport(LIBNAME)]
  79. private static extern IntPtr text_TextDetectorCNN_create_10(string modelArchFilename, string modelWeightsFilename);
  80. // native support for java finalize()
  81. [DllImport(LIBNAME)]
  82. private static extern void text_TextDetectorCNN_delete(IntPtr nativeObj);
  83. }
  84. }
  85. #endif