我在android studio中构建了一个简单的项目,并包含了OpenCV,以便训练SVM(支持向量机)或ANN(人工神经网络)。一切似乎进展顺利,包括数据创建,培训和受过训练的数据检查,除了保存。每当我保存一个opencv ml-object(如ann.save(...)或svm.save(...))时,android studio崩溃。
当我使用
行提取支持向量时classifier.getSupportVectors()
数字似乎是真的。但是,当我移过处的断点时,应用程序崩溃了
classifier.save("C:\\foo\\trentsvm.txt");
在logCat中我挖掘了以下反馈:
07-04 14:36:10.939 25258-25258 / com.example.tbrandsa.opencvtest A / libc: 致命信号11(SIGSEGV),代码2,故障地址0x7f755f53f0 in tid 25258(ndsa.opencvtest)[07-04 14:36:10.942 439:439 W /] debuggerd:处理请求:pid = 25258 uid = 10227 gid = 10227 tid = 25258
如果我试图保存人工神经网络(ANN),我会得到类似的错误,请参阅下面的更新。
我尝试将文件保存为XML和txt,以及" C:\ trentsvm。 someformat "以及" trentsvm。 someformat &#34 ;.我在Eclipse java项目中也遇到了同样的错误。高痛,没有收获。完整代码如下。你能帮忙吗?
PS:我使用OpenCv版本3.2.0。和Android Studio 2.3.2
// I based this code on stuff i found online. Not sure if all is as important or good.
// Purpose: multilabel classification - digit recognition for android app.
// Create data and labels for a digit recognition algorithm
int numTargets = 10; // (0-9 => 10 types of labels)
int totalSamples = 100; // Could have been number of images of digits
int totalIndicators = 10; // Could have been number of properites per digit image.
Mat labels = new Mat(totalSamples,1, CvType.CV_16S);
Mat data = new Mat(totalSamples, totalIndicators,CvType.CV_16S);
// Fill with dummy values:
for (int s = 0; s<totalSamples; s++)
{
int someLabel = s%numTargets;
labels.put(s,0, (double)someLabel);
for (int m = 0; m<totalIndicators; m++)
{
int someDataValue = (s%numTargets)*totalIndicators + m;
data.put(s, m, (double)someDataValue);
}
}
data.convertTo(data, CvType.CV_32F);
labels.convertTo(labels, CvType.CV_32S);
SVM classifier = SVM.create();
TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER,100,0.1);
classifier.setKernel(SVM.LINEAR);
classifier.setType(SVM.C_SVC); //We choose here the type CvSVM::C_SVC that can be used for n-class classification (n >= 2).
classifier.setGamma(0.5);
classifier.setNu(0.5);
classifier.setC(1);
classifier.setTermCriteria(criteria);
classifier.train(data, Ml.ROW_SAMPLE, labels);
// Check how trained SVM predicts the training data
Mat estimates = new Mat(totalSamples, 1, CvType.CV_32F);
classifier.predict(data, estimates, StatModel.RAW_OUTPUT);
for (int i = 0; i<totalSamples; i++)
{
double l = labels.get(i, 0)[0];
double e = estimates.get(i, 0)[0];
System.out.print("\n fact: "+l+", estimat: "+e);
}
Mat suppV = classifier.getSupportVectors();
try {
if (classifier.isTrained()){
// It crashes at the next line!
classifier.save("C:\\foo\\trentsvm.txt");
}
}
catch (Exception e)
{
}
7月5日更新:根据ZdaR的建议,我尝试使用手机内地址,但它没有解决问题。
String address = Environment.getExternalStorageDirectory().getPath()+"/trentsvm.xml";
// address now has value "storage/emulated/0/trentsvm.xml"
classifier.save(address);
在logcat中:
07-05 14:50:12.420 11743-11743 / com.example.tbrandsa.opencv2 A / libc: 致命信号11(SIGSEGV),代码2,tid中的故障地址0x7d517f1990 11743(brandsa.opencv2)
[07-05 14:50:12.424 3134:3134 W /] debuggerd:处理 要求:pid = 11743 uid = 10319 gid = 10319 tid = 11743
7月6日更新: 当我在eclipse中运行相同的脚本并使用调试器时(JUnit 4,VM参数:-Djava.library.path = C:\ Users \ tbrandsa \ Downloads \ opencv \ build \ java \ x64; src \ test \ jniLibs,)在没有设备的情况下在PC上进行调试,捕获的异常&#34; e&#34;说以下
原因=异常, detailMessage =&#34;未知异常&#34; , 栈跟踪=&GT; StackTraceElement [0], suppressExeptions = Collections $ UnmodifiableRandomAccessList,
7月13日更新: 我只是尝试使用人工神经网络(ANN),并且在尝试保存时崩溃。
错误:
致命信号11(SIGSEGV),代码1,故障地址0x15a57e688000c in tid 8507(brandsa.opencv2)debuggerd:处理请求:pid = 8507 uid = 10319 gid = 10319 tid = 8507
代码:
// Mat data is of size 100*20*CV_32FC1,
// Mat labels is of size 100*1*CV_32FC1
// layerSizes is of size 3*1*CV_8UC1
int[] hiddenLayers = {10};
Mat layerSizes = new Mat(2 + hiddenLayers.length,1,CvType.CV_8U);
layerSizes.put(0, 0, data.width());
for (int l = 0; l< hiddenLayers.length; l++){
layerSizes.put(1 + l, 0,hiddenLayers[l]);}
layerSizes.put(1 + hiddenLayers.length, 0,labels.width());
ANN_MLP ann = ANN_MLP.create();
ann.setLayerSizes(layerSizes);
ann.setActivationFunction(ANN_MLP.SIGMOID_SYM);
ann.train(data, Ml.ROW_SAMPLE , labels);
ann.save("/storage/emulated/0/Pictures/no.rema.priceagent.test/trentann.xml");