Path: blob/master/samples/android/face-detection/jni/DetectionBasedTracker_jni.cpp
16344 views
#include <DetectionBasedTracker_jni.h>1#include <opencv2/core.hpp>2#include <opencv2/objdetect.hpp>34#include <string>5#include <vector>67#include <android/log.h>89#define LOG_TAG "FaceDetection/DetectionBasedTracker"10#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))1112using namespace std;13using namespace cv;1415inline void vector_Rect_to_Mat(vector<Rect>& v_rect, Mat& mat)16{17mat = Mat(v_rect, true);18}1920class CascadeDetectorAdapter: public DetectionBasedTracker::IDetector21{22public:23CascadeDetectorAdapter(cv::Ptr<cv::CascadeClassifier> detector):24IDetector(),25Detector(detector)26{27LOGD("CascadeDetectorAdapter::Detect::Detect");28CV_Assert(detector);29}3031void detect(const cv::Mat &Image, std::vector<cv::Rect> &objects)32{33LOGD("CascadeDetectorAdapter::Detect: begin");34LOGD("CascadeDetectorAdapter::Detect: scaleFactor=%.2f, minNeighbours=%d, minObjSize=(%dx%d), maxObjSize=(%dx%d)", scaleFactor, minNeighbours, minObjSize.width, minObjSize.height, maxObjSize.width, maxObjSize.height);35Detector->detectMultiScale(Image, objects, scaleFactor, minNeighbours, 0, minObjSize, maxObjSize);36LOGD("CascadeDetectorAdapter::Detect: end");37}3839virtual ~CascadeDetectorAdapter()40{41LOGD("CascadeDetectorAdapter::Detect::~Detect");42}4344private:45CascadeDetectorAdapter();46cv::Ptr<cv::CascadeClassifier> Detector;47};4849struct DetectorAgregator50{51cv::Ptr<CascadeDetectorAdapter> mainDetector;52cv::Ptr<CascadeDetectorAdapter> trackingDetector;5354cv::Ptr<DetectionBasedTracker> tracker;55DetectorAgregator(cv::Ptr<CascadeDetectorAdapter>& _mainDetector, cv::Ptr<CascadeDetectorAdapter>& _trackingDetector):56mainDetector(_mainDetector),57trackingDetector(_trackingDetector)58{59CV_Assert(_mainDetector);60CV_Assert(_trackingDetector);6162DetectionBasedTracker::Parameters DetectorParams;63tracker = makePtr<DetectionBasedTracker>(mainDetector, trackingDetector, DetectorParams);64}65};6667JNIEXPORT jlong JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject68(JNIEnv * jenv, jclass, jstring jFileName, jint faceSize)69{70LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject enter");71const char* jnamestr = jenv->GetStringUTFChars(jFileName, NULL);72string stdFileName(jnamestr);73jlong result = 0;7475LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject");7677try78{79cv::Ptr<CascadeDetectorAdapter> mainDetector = makePtr<CascadeDetectorAdapter>(80makePtr<CascadeClassifier>(stdFileName));81cv::Ptr<CascadeDetectorAdapter> trackingDetector = makePtr<CascadeDetectorAdapter>(82makePtr<CascadeClassifier>(stdFileName));83result = (jlong)new DetectorAgregator(mainDetector, trackingDetector);84if (faceSize > 0)85{86mainDetector->setMinObjectSize(Size(faceSize, faceSize));87//trackingDetector->setMinObjectSize(Size(faceSize, faceSize));88}89}90catch(cv::Exception& e)91{92LOGD("nativeCreateObject caught cv::Exception: %s", e.what());93jclass je = jenv->FindClass("org/opencv/core/CvException");94if(!je)95je = jenv->FindClass("java/lang/Exception");96jenv->ThrowNew(je, e.what());97}98catch (...)99{100LOGD("nativeCreateObject caught unknown exception");101jclass je = jenv->FindClass("java/lang/Exception");102jenv->ThrowNew(je, "Unknown exception in JNI code of DetectionBasedTracker.nativeCreateObject()");103return 0;104}105106LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject exit");107return result;108}109110JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDestroyObject111(JNIEnv * jenv, jclass, jlong thiz)112{113LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDestroyObject");114115try116{117if(thiz != 0)118{119((DetectorAgregator*)thiz)->tracker->stop();120delete (DetectorAgregator*)thiz;121}122}123catch(cv::Exception& e)124{125LOGD("nativeestroyObject caught cv::Exception: %s", e.what());126jclass je = jenv->FindClass("org/opencv/core/CvException");127if(!je)128je = jenv->FindClass("java/lang/Exception");129jenv->ThrowNew(je, e.what());130}131catch (...)132{133LOGD("nativeDestroyObject caught unknown exception");134jclass je = jenv->FindClass("java/lang/Exception");135jenv->ThrowNew(je, "Unknown exception in JNI code of DetectionBasedTracker.nativeDestroyObject()");136}137LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDestroyObject exit");138}139140JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStart141(JNIEnv * jenv, jclass, jlong thiz)142{143LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStart");144145try146{147((DetectorAgregator*)thiz)->tracker->run();148}149catch(cv::Exception& e)150{151LOGD("nativeStart caught cv::Exception: %s", e.what());152jclass je = jenv->FindClass("org/opencv/core/CvException");153if(!je)154je = jenv->FindClass("java/lang/Exception");155jenv->ThrowNew(je, e.what());156}157catch (...)158{159LOGD("nativeStart caught unknown exception");160jclass je = jenv->FindClass("java/lang/Exception");161jenv->ThrowNew(je, "Unknown exception in JNI code of DetectionBasedTracker.nativeStart()");162}163LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStart exit");164}165166JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStop167(JNIEnv * jenv, jclass, jlong thiz)168{169LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStop");170171try172{173((DetectorAgregator*)thiz)->tracker->stop();174}175catch(cv::Exception& e)176{177LOGD("nativeStop caught cv::Exception: %s", e.what());178jclass je = jenv->FindClass("org/opencv/core/CvException");179if(!je)180je = jenv->FindClass("java/lang/Exception");181jenv->ThrowNew(je, e.what());182}183catch (...)184{185LOGD("nativeStop caught unknown exception");186jclass je = jenv->FindClass("java/lang/Exception");187jenv->ThrowNew(je, "Unknown exception in JNI code of DetectionBasedTracker.nativeStop()");188}189LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStop exit");190}191192JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeSetFaceSize193(JNIEnv * jenv, jclass, jlong thiz, jint faceSize)194{195LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeSetFaceSize -- BEGIN");196197try198{199if (faceSize > 0)200{201((DetectorAgregator*)thiz)->mainDetector->setMinObjectSize(Size(faceSize, faceSize));202//((DetectorAgregator*)thiz)->trackingDetector->setMinObjectSize(Size(faceSize, faceSize));203}204}205catch(cv::Exception& e)206{207LOGD("nativeStop caught cv::Exception: %s", e.what());208jclass je = jenv->FindClass("org/opencv/core/CvException");209if(!je)210je = jenv->FindClass("java/lang/Exception");211jenv->ThrowNew(je, e.what());212}213catch (...)214{215LOGD("nativeSetFaceSize caught unknown exception");216jclass je = jenv->FindClass("java/lang/Exception");217jenv->ThrowNew(je, "Unknown exception in JNI code of DetectionBasedTracker.nativeSetFaceSize()");218}219LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeSetFaceSize -- END");220}221222223JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect224(JNIEnv * jenv, jclass, jlong thiz, jlong imageGray, jlong faces)225{226LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect");227228try229{230vector<Rect> RectFaces;231((DetectorAgregator*)thiz)->tracker->process(*((Mat*)imageGray));232((DetectorAgregator*)thiz)->tracker->getObjects(RectFaces);233*((Mat*)faces) = Mat(RectFaces, true);234}235catch(cv::Exception& e)236{237LOGD("nativeCreateObject caught cv::Exception: %s", e.what());238jclass je = jenv->FindClass("org/opencv/core/CvException");239if(!je)240je = jenv->FindClass("java/lang/Exception");241jenv->ThrowNew(je, e.what());242}243catch (...)244{245LOGD("nativeDetect caught unknown exception");246jclass je = jenv->FindClass("java/lang/Exception");247jenv->ThrowNew(je, "Unknown exception in JNI code DetectionBasedTracker.nativeDetect()");248}249LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect END");250}251252253