JNIEXPORT jboolean JNICALL Java_com_yaoyumeng_asmlibrary_ASMFit_nativeDetectOne (JNIEnv * jenv, jclass, jlong imageGray, jlong face) { BEGINT(); Mat image = *((Mat*)imageGray); std::vector<asm_shape> det_shapes; if(!detect_all_faces(det_shapes, image)){ ENDT("CascadeDetector CANNOT detect any face"); return false; } int iSelectedFace = 0; double x0 = image.cols/2., y0 = image.rows/2.; double x, y, d, D = 1e307; for (int i = 0; i < det_shapes.size(); i++) { x = (det_shapes[i][0].x + det_shapes[i][1].x) / 2.; y = (det_shapes[i][0].y + det_shapes[i][1].y) / 2.; d = sqrt((x-x0)*(x-x0) + (y-y0)*(y-y0)); if (d < D) { D = d; iSelectedFace = i; } } asm_shape shape = det_shapes[iSelectedFace],detshape; InitShapeFromDetBox(shape, detshape, fit_asm.GetMappingDetShape(), fit_asm.GetMeanFaceWidth()); shape_to_Mat(&shape, 1, *((Mat*)face)); ENDT("CascadeDetector detects central face"); return true; }
JNIEXPORT jboolean JNICALL Java_com_yaoyumeng_asmlibrary_ASMFit_nativeDetectAll (JNIEnv * jenv, jclass, jlong imageGray, jlong faces) { Mat image = *((Mat*)imageGray); BEGINT(); std::vector<asm_shape> det_shapes; if(!detect_all_faces(det_shapes, image)){ ENDT("CascadeDetector CANNOT detect any face"); return false; } LOGD("CascadeDetector found %d faces", det_shapes.size()); asm_shape* shapes = new asm_shape[det_shapes.size()]; for( int i = 0; i < det_shapes.size(); i++ ) { InitShapeFromDetBox(shapes[i], det_shapes[i], fit_asm.GetMappingDetShape(), fit_asm.GetMeanFaceWidth()); } shape_to_Mat(shapes, det_shapes.size(), *((Mat*)faces)); delete []shapes; ENDT("CascadeDetector detect"); return true; }
bool InitAsm() { if(! g_AsmFit.Read(ASMFN)) { CString msg; msg.Format("Can't load ASM model file %s.", ASMFN); //::MessageBox(NULL, msg, "message", MB_OK | MB_ICONWARNING); MessageBox1(msg); return false; } g_FDcascade = (CvHaarClassifierCascade*)cvLoad(FDFN, 0, 0, 0); if (!g_FDcascade) { CString msg; msg.Format("Can't load %s.", _T(FDFN)); ::MessageBox1(msg); return false; } g_FDstorage = cvCreateMemStorage(0); if (!g_FDstorage) { ::MessageBox1("Can't create memory storage for face detection."); return false; } return true; }
JNIEXPORT void JNICALL Java_com_yaoyumeng_asmlibrary_ASMFit_nativeInitShape(JNIEnv * jenv, jclass, jlong faces) { Mat faces1 = *((Mat*)faces); int nFaces = faces1.rows; asm_shape* detshapes = new asm_shape[nFaces]; asm_shape* shapes = new asm_shape[nFaces]; Mat_to_shape(detshapes, nFaces, faces1); for(int i = 0; i < nFaces; i++) { InitShapeFromDetBox(shapes[i], detshapes[i], fit_asm.GetMappingDetShape(), fit_asm.GetMeanFaceWidth()); } shape_to_Mat(shapes, nFaces, *((Mat*)faces)); delete []detshapes; delete []shapes; }
JNIEXPORT jboolean JNICALL Java_com_yaoyumeng_asmlibrary_ASMFit_nativeReadModel (JNIEnv * jenv, jclass, jstring jFileName) { LOGD("nativeReadModel enter"); const char* filename = jenv->GetStringUTFChars(jFileName, NULL); jboolean result = false; try { if(fit_asm.Read(filename) == true) result = true; } catch (...) { LOGD("nativeReadModel caught unknown exception"); jclass je = jenv->FindClass("java/lang/Exception"); jenv->ThrowNew(je, "Unknown exception in JNI code"); } LOGD("nativeReadModel %s exit %d", filename, result); return result; }
JNIEXPORT jboolean JNICALL Java_com_yaoyumeng_asmlibrary_ASMFit_nativeVideoFitting (JNIEnv * jenv, jclass, jlong imageGray, jlong shapes0, jlong frame, jlong n_iteration) { IplImage image = *(Mat*)imageGray; Mat shapes1 = *(Mat*)shapes0; bool flag = false; if(shapes1.rows == 1) { asm_shape shape; BEGINT(); Mat_to_shape(&shape, 1, shapes1); flag = fit_asm.ASMSeqSearch(shape, &image, frame, false, n_iteration); shape_to_Mat(&shape, 1, *((Mat*)shapes0)); ENDT("nativeVideoFitting"); } return flag; }
JNIEXPORT void JNICALL Java_com_yaoyumeng_asmlibrary_ASMFit_nativeFitting (JNIEnv * jenv, jclass, jlong imageGray, jlong shapes0, jlong n_iteration) { IplImage image = *(Mat*)imageGray; Mat shapes1 = *(Mat*)shapes0; int nFaces = shapes1.rows; asm_shape* shapes = new asm_shape[nFaces]; BEGINT(); Mat_to_shape(shapes, nFaces, shapes1); fit_asm.Fitting2(shapes, nFaces, &image, n_iteration); shape_to_Mat(shapes, nFaces, *((Mat*)shapes0)); ENDT("nativeFitting"); //for(int i = 0; i < shapes[0].NPoints(); i++) // LOGD("points: (%f, %f)", shapes[0][i].x, shapes[0][i].y); delete []shapes; }
bool AsmDetectFeaturePoints( IplImage *pic8 ) { int nFaces; CvSeq *pFaces; bool bResize = false; IplImage *picCopy = pic8; if (pic8->width > 700 || pic8->height > 700) // if the picture is too big { pic8 = cvCreateImage(cvGetSize(pic8)/2, 8, 1); cvPyrDown(picCopy, pic8); bResize = true; } pFaces = cvHaarDetectObjects( pic8, g_FDcascade, g_FDstorage, 1.1, 3, 0 | CV_HAAR_DO_CANNY_PRUNING | CV_HAAR_FIND_BIGGEST_OBJECT | //CV_HAAR_DO_ROUGH_SEARCH | 0, cvSize(20, 20)); nFaces = pFaces->total; if (nFaces == 0) // use a looser standard to detect again { pFaces = cvHaarDetectObjects( pic8, g_FDcascade, g_FDstorage, 1.1, 1, 0 | CV_HAAR_DO_CANNY_PRUNING | CV_HAAR_FIND_BIGGEST_OBJECT | //CV_HAAR_DO_ROUGH_SEARCH | 0, cvSize(20, 20)); nFaces = pFaces->total; } if (nFaces == 0) { if (bResize) cvReleaseImage(&pic8); return false; } g_detshape.Resize(2); g_faceRc = *(CvRect*)cvGetSeqElem(pFaces, 0); if (bResize) { g_faceRc.x *= 2; g_faceRc.y *= 2; g_faceRc.width *= 2; g_faceRc.height *= 2; } g_detshape[0].x = float(g_faceRc.x); g_detshape[0].y = float(g_faceRc.y); g_detshape[1].x = float(g_faceRc.x+g_faceRc.width); g_detshape[1].y = float(g_faceRc.y+g_faceRc.height); InitShapeFromDetBox(g_shape, g_detshape, g_AsmFit.GetMappingDetShape(), g_AsmFit.GetMeanFaceWidth()); g_AsmFit.Fitting(g_shape, picCopy); // fit ASM model //DrawPoints(pic8, g_shape); if (bResize) cvReleaseImage(&pic8); return true; }
JNIEXPORT jboolean JNICALL Java_com_yaoyumeng_asmlibrary_ASMFit_nativeFastDetectAll (JNIEnv * jenv, jclass, jlong imageGray, jlong faces) { if(!track) return false; BEGINT(); vector<Rect> RectFaces; try{ Mat image = *(Mat*)imageGray; LOGD("image: (%d, %d)", image.cols, image.rows); track->process(image); track->getObjects(RectFaces); } catch(cv::Exception& e) { LOGD("nativeFastDetectAll caught cv::Exception: %s", e.what()); jclass je = jenv->FindClass("org/opencv/core/CvException"); if(!je) je = jenv->FindClass("java/lang/Exception"); jenv->ThrowNew(je, e.what()); } catch (...) { LOGD("nativeFastDetectAll caught unknown exception"); jclass je = jenv->FindClass("java/lang/Exception"); jenv->ThrowNew(je, "Unknown exception in JNI code"); } int nFaces = RectFaces.size(); if(nFaces <= 0){ ENDT("FastCascadeDetector CANNOT detect any face"); return false; } LOGD("FastCascadeDetector found %d faces", nFaces); asm_shape* detshapes = new asm_shape[nFaces]; for(int i = 0; i < nFaces; i++){ Rect r = RectFaces[i]; detshapes[i].Resize(2); detshapes[i][0].x = r.x; detshapes[i][0].y = r.y; detshapes[i][1].x = r.x+r.width; detshapes[i][1].y = r.y+r.height; } asm_shape* shapes = new asm_shape[nFaces]; for(int i = 0; i < nFaces; i++) { InitShapeFromDetBox(shapes[i], detshapes[i], fit_asm.GetMappingDetShape(), fit_asm.GetMeanFaceWidth()); } shape_to_Mat(shapes, nFaces, *((Mat*)faces)); delete []detshapes; delete []shapes; ENDT("FastCascadeDetector detect"); return true; }