Neural network implementation using opencv and java

asked 2016-05-30 14:18:38 -0500

nadia gravatar image

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

edit retag flag offensive close merge delete

Comments

hello @nadia could you update if you managed to fix this error and thank you

Tomna gravatar imageTomna ( 2017-05-22 10:18:38 -0500 )edit