【发布时间】:2015-02-03 16:05:06
【问题描述】:
我使用的是 OpenCV 2.4.10。
JNI - 训练面部识别
JNIEXPORT jlong JNICALL Java_com_sample_facialRecognition_DetectionBasedRecognition_nativeTrain
(JNIEnv * jenv, jstring pathIn)
{
vector<Mat> images;
vector<int> labels;
try {
std::string path;
std::string classlabel = "A";
GetJStringContent(jenv,pathIn,path);
if(!path.empty() && !classlabel.empty()) {
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
}
Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
model->train(images, labels);
model.addref(); //don't let it self-destroy here..
FaceRecognizer * pf = model.obj;
return (jlong) pf;
}
catch (...)
{
return 0;
}
}
Java - 训练识别器
mNativeRecognition = nativeTrain(getFilesDir().toString());
Java - 进行检测和识别
nativeDetect(mGray, faces, mNativeRecognition);
JNI - 识别
JNIEXPORT jint JNICALL Java_com_sample_facialRecognition_DetectionBasedTracker_nativeDetect
(JNIEnv * jenv, jclass, jlong thiz, jlong imageGray, jlong faces, jlong recog)
{
jint whoAreYou= 0;
try
{
vector<Rect> RectFaces;
((DetectionBasedTracker*)thiz)->process(*((Mat*)imageGray));
((DetectionBasedTracker*)thiz)->getObjects(RectFaces);
Ptr < FaceRecognizer > model = recog; //here is the problem
vector_Rect_to_Mat(RectFaces, *((Mat*)faces));
for (int i = 0; i < faces.size(); i++)
{
cv::Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
cv::Point pt2(faces[i].x, faces[i].y);
cv::Rect face_i = faces[i];
cv::Mat face = grayscaleFrame(face_i);
cv::Mat face_resized;
cv::resize(face, face_resized, cv::Size(100, 120), 1.0, 1.0, INTER_CUBIC);
whoAreYou = model->predict(face_resized);
}
}
catch (...)
{
//catch...
}
return whoAreYou;
}
所以我试图从 nativeTrain 转换存储的指针,并在一个函数中实时使用它来检测和识别人脸。如何在 nativeDetect 中将该指针转换回可用的 FaceRecognizer?
【问题讨论】:
-
试试:
FaceRecognizer * model = (FaceRecognizer *)recog; //here is the problem。如果你使用 cv::Ptr 版本,应该是:Ptr<FaceRecognizer> model( (FaceRecognizer *)recog);和 `model.addref();` 在函数的末尾。
标签: android opencv java-native-interface