DetectionBasedTracker_jni.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include <DetectionBasedTracker_jni.h>
  2. #include <opencv2/core.hpp>
  3. #include <opencv2/objdetect.hpp>
  4. #include <string>
  5. #include <vector>
  6. #include <android/log.h>
  7. #define LOG_TAG "FaceDetection/DetectionBasedTracker"
  8. #define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
  9. using namespace std;
  10. using namespace cv;
  11. inline void vector_Rect_to_Mat(vector<Rect>& v_rect, Mat& mat)
  12. {
  13. mat = Mat(v_rect, true);
  14. }
  15. class CascadeDetectorAdapter: public DetectionBasedTracker::IDetector
  16. {
  17. public:
  18. CascadeDetectorAdapter(cv::Ptr<cv::CascadeClassifier> detector):
  19. IDetector(),
  20. Detector(detector)
  21. {
  22. LOGD("CascadeDetectorAdapter::Detect::Detect");
  23. CV_Assert(detector);
  24. }
  25. void detect(const cv::Mat &Image, std::vector<cv::Rect> &objects)
  26. {
  27. LOGD("CascadeDetectorAdapter::Detect: begin");
  28. LOGD("CascadeDetectorAdapter::Detect: scaleFactor=%.2f, minNeighbours=%d, minObjSize=(%dx%d), maxObjSize=(%dx%d)", scaleFactor, minNeighbours, minObjSize.width, minObjSize.height, maxObjSize.width, maxObjSize.height);
  29. Detector->detectMultiScale(Image, objects, scaleFactor, minNeighbours, 0, minObjSize, maxObjSize);
  30. LOGD("CascadeDetectorAdapter::Detect: end");
  31. }
  32. virtual ~CascadeDetectorAdapter()
  33. {
  34. LOGD("CascadeDetectorAdapter::Detect::~Detect");
  35. }
  36. private:
  37. CascadeDetectorAdapter();
  38. cv::Ptr<cv::CascadeClassifier> Detector;
  39. };
  40. struct DetectorAgregator
  41. {
  42. cv::Ptr<CascadeDetectorAdapter> mainDetector;
  43. cv::Ptr<CascadeDetectorAdapter> trackingDetector;
  44. cv::Ptr<DetectionBasedTracker> tracker;
  45. DetectorAgregator(cv::Ptr<CascadeDetectorAdapter>& _mainDetector, cv::Ptr<CascadeDetectorAdapter>& _trackingDetector):
  46. mainDetector(_mainDetector),
  47. trackingDetector(_trackingDetector)
  48. {
  49. CV_Assert(_mainDetector);
  50. CV_Assert(_trackingDetector);
  51. DetectionBasedTracker::Parameters DetectorParams;
  52. tracker = makePtr<DetectionBasedTracker>(mainDetector, trackingDetector, DetectorParams);
  53. }
  54. };
  55. JNIEXPORT jlong JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject
  56. (JNIEnv * jenv, jclass, jstring jFileName, jint faceSize)
  57. {
  58. LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject enter");
  59. const char* jnamestr = jenv->GetStringUTFChars(jFileName, NULL);
  60. string stdFileName(jnamestr);
  61. jlong result = 0;
  62. LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject");
  63. try
  64. {
  65. cv::Ptr<CascadeDetectorAdapter> mainDetector = makePtr<CascadeDetectorAdapter>(
  66. makePtr<CascadeClassifier>(stdFileName));
  67. cv::Ptr<CascadeDetectorAdapter> trackingDetector = makePtr<CascadeDetectorAdapter>(
  68. makePtr<CascadeClassifier>(stdFileName));
  69. result = (jlong)new DetectorAgregator(mainDetector, trackingDetector);
  70. if (faceSize > 0)
  71. {
  72. mainDetector->setMinObjectSize(Size(faceSize, faceSize));
  73. //trackingDetector->setMinObjectSize(Size(faceSize, faceSize));
  74. }
  75. }
  76. catch(const cv::Exception& e)
  77. {
  78. LOGD("nativeCreateObject caught cv::Exception: %s", e.what());
  79. jclass je = jenv->FindClass("org/opencv/core/CvException");
  80. if(!je)
  81. je = jenv->FindClass("java/lang/Exception");
  82. jenv->ThrowNew(je, e.what());
  83. }
  84. catch (...)
  85. {
  86. LOGD("nativeCreateObject caught unknown exception");
  87. jclass je = jenv->FindClass("java/lang/Exception");
  88. jenv->ThrowNew(je, "Unknown exception in JNI code of DetectionBasedTracker.nativeCreateObject()");
  89. return 0;
  90. }
  91. LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject exit");
  92. return result;
  93. }
  94. JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDestroyObject
  95. (JNIEnv * jenv, jclass, jlong thiz)
  96. {
  97. LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDestroyObject");
  98. try
  99. {
  100. if(thiz != 0)
  101. {
  102. ((DetectorAgregator*)thiz)->tracker->stop();
  103. delete (DetectorAgregator*)thiz;
  104. }
  105. }
  106. catch(const cv::Exception& e)
  107. {
  108. LOGD("nativeestroyObject caught cv::Exception: %s", e.what());
  109. jclass je = jenv->FindClass("org/opencv/core/CvException");
  110. if(!je)
  111. je = jenv->FindClass("java/lang/Exception");
  112. jenv->ThrowNew(je, e.what());
  113. }
  114. catch (...)
  115. {
  116. LOGD("nativeDestroyObject caught unknown exception");
  117. jclass je = jenv->FindClass("java/lang/Exception");
  118. jenv->ThrowNew(je, "Unknown exception in JNI code of DetectionBasedTracker.nativeDestroyObject()");
  119. }
  120. LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDestroyObject exit");
  121. }
  122. JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStart
  123. (JNIEnv * jenv, jclass, jlong thiz)
  124. {
  125. LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStart");
  126. try
  127. {
  128. ((DetectorAgregator*)thiz)->tracker->run();
  129. }
  130. catch(const cv::Exception& e)
  131. {
  132. LOGD("nativeStart caught cv::Exception: %s", e.what());
  133. jclass je = jenv->FindClass("org/opencv/core/CvException");
  134. if(!je)
  135. je = jenv->FindClass("java/lang/Exception");
  136. jenv->ThrowNew(je, e.what());
  137. }
  138. catch (...)
  139. {
  140. LOGD("nativeStart caught unknown exception");
  141. jclass je = jenv->FindClass("java/lang/Exception");
  142. jenv->ThrowNew(je, "Unknown exception in JNI code of DetectionBasedTracker.nativeStart()");
  143. }
  144. LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStart exit");
  145. }
  146. JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStop
  147. (JNIEnv * jenv, jclass, jlong thiz)
  148. {
  149. LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStop");
  150. try
  151. {
  152. ((DetectorAgregator*)thiz)->tracker->stop();
  153. }
  154. catch(const cv::Exception& e)
  155. {
  156. LOGD("nativeStop caught cv::Exception: %s", e.what());
  157. jclass je = jenv->FindClass("org/opencv/core/CvException");
  158. if(!je)
  159. je = jenv->FindClass("java/lang/Exception");
  160. jenv->ThrowNew(je, e.what());
  161. }
  162. catch (...)
  163. {
  164. LOGD("nativeStop caught unknown exception");
  165. jclass je = jenv->FindClass("java/lang/Exception");
  166. jenv->ThrowNew(je, "Unknown exception in JNI code of DetectionBasedTracker.nativeStop()");
  167. }
  168. LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStop exit");
  169. }
  170. JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeSetFaceSize
  171. (JNIEnv * jenv, jclass, jlong thiz, jint faceSize)
  172. {
  173. LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeSetFaceSize -- BEGIN");
  174. try
  175. {
  176. if (faceSize > 0)
  177. {
  178. ((DetectorAgregator*)thiz)->mainDetector->setMinObjectSize(Size(faceSize, faceSize));
  179. //((DetectorAgregator*)thiz)->trackingDetector->setMinObjectSize(Size(faceSize, faceSize));
  180. }
  181. }
  182. catch(const cv::Exception& e)
  183. {
  184. LOGD("nativeStop caught cv::Exception: %s", e.what());
  185. jclass je = jenv->FindClass("org/opencv/core/CvException");
  186. if(!je)
  187. je = jenv->FindClass("java/lang/Exception");
  188. jenv->ThrowNew(je, e.what());
  189. }
  190. catch (...)
  191. {
  192. LOGD("nativeSetFaceSize caught unknown exception");
  193. jclass je = jenv->FindClass("java/lang/Exception");
  194. jenv->ThrowNew(je, "Unknown exception in JNI code of DetectionBasedTracker.nativeSetFaceSize()");
  195. }
  196. LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeSetFaceSize -- END");
  197. }
  198. JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect
  199. (JNIEnv * jenv, jclass, jlong thiz, jlong imageGray, jlong faces)
  200. {
  201. LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect");
  202. try
  203. {
  204. vector<Rect> RectFaces;
  205. ((DetectorAgregator*)thiz)->tracker->process(*((Mat*)imageGray));
  206. ((DetectorAgregator*)thiz)->tracker->getObjects(RectFaces);
  207. *((Mat*)faces) = Mat(RectFaces, true);
  208. }
  209. catch(const cv::Exception& e)
  210. {
  211. LOGD("nativeCreateObject caught cv::Exception: %s", e.what());
  212. jclass je = jenv->FindClass("org/opencv/core/CvException");
  213. if(!je)
  214. je = jenv->FindClass("java/lang/Exception");
  215. jenv->ThrowNew(je, e.what());
  216. }
  217. catch (...)
  218. {
  219. LOGD("nativeDetect caught unknown exception");
  220. jclass je = jenv->FindClass("java/lang/Exception");
  221. jenv->ThrowNew(je, "Unknown exception in JNI code DetectionBasedTracker.nativeDetect()");
  222. }
  223. LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect END");
  224. }