Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Neural network implementation using opencv and java

I should use opencv with java language but when i tried to implement neural network i got errors in this instruction ann.train(trainData, Ml.ROW_SAMPLE,train_labels);

        // Creating Training Data
                Mat trainData = new Mat();
        Mat train_labels = new Mat();

                for (int i = 0; i <5; i++) {


                String path = Environment.getExternalStorageDirectory().toString()
                        + "/Pic/folder/" + i + ".png";

                Mat img = Imgcodecs.imread(path);

                img.convertTo(img, CvType.CV_32FC1); // Convert to float
                Size dsize = new Size(25, 25);
                Imgproc.resize(img, img, dsize);

                img.convertTo(img, CvType.CV_32FC1);
                Mat imgResized = img.reshape(1, 1); // make continuous

                trainData.push_back(imgResized);
                // add 1 item
                train_labels
                        .push_back(new Mat(1, 1, CvType.CV_32SC1, new Scalar(0)));

            }
                for (int i = 6; i <15; i++) {


                    String path = Environment.getExternalStorageDirectory().toString()
                            + "/Pictures/train/" + i + ".png";

                    Mat img = Imgcodecs.imread(path);



                    img.convertTo(img, CvType.CV_32FC1); // Convert to float
                    Size dsize = new Size(25, 25);
                    Imgproc.resize(img, img, dsize);

                    img.convertTo(img, CvType.CV_32FC1);
                    Mat imgResized = img.reshape(1, 1); // make continuous

                    trainData.push_back(imgResized);
                    // add 1 item
                    train_labels
                            .push_back(new Mat(1, 1, CvType.CV_32SC1, new Scalar(1)));

                }

                Mat response = new Mat();
                Mat tmp;
                tmp = train_labels.reshape(1, 1); // make continuous
                tmp.convertTo(response, CvType.CV_32FC1); // Convert to float
                 ANN_MLP ann=ANN_MLP.create();

                    TermCriteria criteria = new TermCriteria(TermCriteria.EPS +TermCriteria.MAX_ITER,100,0.1);
                    ann.setTermCriteria(criteria);
                    ann.train(trainData, Ml.ROW_SAMPLE,train_labels);

this is the error: 05-30 19:40:20.003: E/cv::error()(12928): OpenCV Error: Unspecified error (The network has not been created. Use method create or the appropriate constructor) in void cv::ml::ANN_MLPImpl::prepare_to_train(const cv::Mat&, const cv::Mat&, cv::Mat&, int), file /home/maksim/workspace/android-pack/opencv/modules/ml/src/ann_mlp.cpp, line 661