Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

how to implement neural network using opencv and java

I have to do a project that recognize images using opencv machine learning. I should try many methods. I tried KNN and SVM but i don't know how to implement ANN. Can someone help me? This is the KNN example

public   void train() {

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

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

                String path = Environment.getExternalStorageDirectory().toString()
                        + "/Pictures/images/" + i + ".jpg";

                Mat img = Imgcodecs.imread(path);

                Log.i(TAG,"error"+i+img.empty());


                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(i)));

            }



            Mat response = new Mat();
            Mat tmp;
            tmp = train_labels.reshape(1, 1); // make continuous
            tmp.convertTo(response, CvType.CV_32FC1); // Convert to float
            KNearest knn = KNearest.create();
            knn.train(trainData, Ml.ROW_SAMPLE, train_labels);

            // For Storing training data

            String path = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS).toString();

            Bitmap bmpOut1 = Bitmap.createBitmap(trainData.cols(),
                    trainData.rows(), Bitmap.Config.ARGB_8888);
            Bitmap bmpOut2 = Bitmap.createBitmap(response.cols(), response.rows(),
                    Bitmap.Config.ARGB_8888);

            response.convertTo(response, CvType.CV_8UC1);
            trainData.convertTo(trainData, CvType.CV_8UC1);

            Utils.matToBitmap(trainData, bmpOut1);
            Utils.matToBitmap(response, bmpOut2);
            // File file = new File(path);
            // file.mkdirs();
            File file = new File(path, "train.png");
            File file2 = new File(path, "response.png");

            OutputStream fout = null;
            OutputStream fout2 = null;

            try {
                fout = new FileOutputStream(file);
                fout2 = new FileOutputStream(file2);

                BufferedOutputStream bos = new BufferedOutputStream(fout);
                BufferedOutputStream bos2 = new BufferedOutputStream(fout2);

                bmpOut1.compress(Bitmap.CompressFormat.PNG, 100, bos);
                bos.flush();
                bos.close();
                bmpOut1.recycle();

                bmpOut2.compress(Bitmap.CompressFormat.PNG, 100, bos2);
                bos2.flush();
                bos2.close();
                bmpOut2.recycle();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            catch (IOException e) {
                e.printStackTrace();
            }



            // For Accessing training data in BMP file
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inScaled = false;
            Bitmap blankBitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.train, o);


            Mat trainData2 = new Mat();
            Utils.bitmapToMat(blankBitmap, trainData2);

            Bitmap blankBitmap2 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.response, o);


            Mat response2 = new Mat();
            Utils.bitmapToMat(blankBitmap2, response2);

            Mat response3 = new Mat();
            Mat trainData3 = new Mat();
             // 1. change the number of channels
            Imgproc.cvtColor(response2, response2, Imgproc.COLOR_BGRA2GRAY);

            Imgproc.cvtColor(trainData2, trainData2, Imgproc.COLOR_BGRA2GRAY);
            response2.convertTo(response3, CvType.CV_32FC1);
            trainData2.convertTo(trainData3, CvType.CV_32FC1);
            // instantiate the KNN object
            KNearest knn2 = KNearest.create();
            knn2.train(trainData3, Ml.ROW_SAMPLE, response3);



                Mat img = Imgcodecs.imread("/storage/emulated/0/DCIM/Camera/IMG.jpg");
                Log.i(TAG, "error1 img" + img.empty());

                Mat test = new Mat();
                Imgproc.resize(img, test, new Size(25, 25));
                test.convertTo(test, CvType.CV_32FC1);
                Mat results = new Mat();
                Mat responses = new Mat();
                Mat dists = new Mat();

                float label = knn.findNearest(test.reshape(1, 1),1, results,responses, dists);

                 if (label==0)
                {
                text.setText("Le reve");
                }
                 else if (label==1) {
                        text.setText("Guernica");
                    } 
                 else if (label==2) {
                        text.setText("La cène");
                    } 
                 else if (label==3) {
                        text.setText("La joconde");
                    } 
                 else if (label==4) {
                        text.setText("la vierge et sainte anne");
                    }
                 else if (label==5) {
                        text.setText("les joueurs de cartes ");
                    } 
                else
                text.setText("");
            }