1 package org.opencv.samples.opencv_mobilenet;
2 
3 import android.content.Context;
4 import android.content.res.AssetManager;
5 import android.os.Bundle;
6 import android.support.v7.app.AppCompatActivity;
7 import android.util.Log;
8 
9 import org.opencv.android.BaseLoaderCallback;
10 import org.opencv.android.CameraBridgeViewBase;
11 import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
12 import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
13 import org.opencv.android.LoaderCallbackInterface;
14 import org.opencv.android.OpenCVLoader;
15 import org.opencv.core.Core;
16 import org.opencv.core.Mat;
17 import org.opencv.core.Point;
18 import org.opencv.core.Scalar;
19 import org.opencv.core.Size;
20 import org.opencv.dnn.Net;
21 import org.opencv.dnn.Dnn;
22 import org.opencv.imgproc.Imgproc;
23 
24 import java.io.BufferedInputStream;
25 import java.io.File;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 
29 public class MainActivity extends AppCompatActivity implements CvCameraViewListener2 {
30 
31     // Initialize OpenCV manager.
32     private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
33         @Override
34         public void onManagerConnected(int status) {
35             switch (status) {
36                 case LoaderCallbackInterface.SUCCESS: {
37                     Log.i(TAG, "OpenCV loaded successfully");
38                     mOpenCvCameraView.enableView();
39                     break;
40                 }
41                 default: {
42                     super.onManagerConnected(status);
43                     break;
44                 }
45             }
46         }
47     };
48 
49     @Override
onResume()50     public void onResume() {
51         super.onResume();
52         OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, this, mLoaderCallback);
53     }
54 
55     @Override
onCreate(Bundle savedInstanceState)56     protected void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58         setContentView(R.layout.activity_main);
59 
60         // Set up camera listener.
61         mOpenCvCameraView = (CameraBridgeViewBase)findViewById(R.id.CameraView);
62         mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
63         mOpenCvCameraView.setCvCameraViewListener(this);
64     }
65 
66     // Load a network.
onCameraViewStarted(int width, int height)67     public void onCameraViewStarted(int width, int height) {
68         String proto = getPath("MobileNetSSD_deploy.prototxt", this);
69         String weights = getPath("MobileNetSSD_deploy.caffemodel", this);
70         net = Dnn.readNetFromCaffe(proto, weights);
71         Log.i(TAG, "Network loaded successfully");
72     }
73 
onCameraFrame(CvCameraViewFrame inputFrame)74     public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
75         final int IN_WIDTH = 300;
76         final int IN_HEIGHT = 300;
77         final float WH_RATIO = (float)IN_WIDTH / IN_HEIGHT;
78         final double IN_SCALE_FACTOR = 0.007843;
79         final double MEAN_VAL = 127.5;
80         final double THRESHOLD = 0.2;
81 
82         // Get a new frame
83         Mat frame = inputFrame.rgba();
84         Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2RGB);
85 
86         // Forward image through network.
87         Mat blob = Dnn.blobFromImage(frame, IN_SCALE_FACTOR,
88                 new Size(IN_WIDTH, IN_HEIGHT),
89                 new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), /*swapRB*/false, /*crop*/false);
90         net.setInput(blob);
91         Mat detections = net.forward();
92 
93         int cols = frame.cols();
94         int rows = frame.rows();
95 
96         detections = detections.reshape(1, (int)detections.total() / 7);
97 
98         for (int i = 0; i < detections.rows(); ++i) {
99             double confidence = detections.get(i, 2)[0];
100             if (confidence > THRESHOLD) {
101                 int classId = (int)detections.get(i, 1)[0];
102 
103                 int left   = (int)(detections.get(i, 3)[0] * cols);
104                 int top    = (int)(detections.get(i, 4)[0] * rows);
105                 int right  = (int)(detections.get(i, 5)[0] * cols);
106                 int bottom = (int)(detections.get(i, 6)[0] * rows);
107 
108                 // Draw rectangle around detected object.
109                 Imgproc.rectangle(frame, new Point(left, top), new Point(right, bottom),
110                                   new Scalar(0, 255, 0));
111                 String label = classNames[classId] + ": " + confidence;
112                 int[] baseLine = new int[1];
113                 Size labelSize = Imgproc.getTextSize(label, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, 1, baseLine);
114 
115                 // Draw background for label.
116                 Imgproc.rectangle(frame, new Point(left, top - labelSize.height),
117                                   new Point(left + labelSize.width, top + baseLine[0]),
118                                   new Scalar(255, 255, 255), Imgproc.FILLED);
119                 // Write class name and confidence.
120                 Imgproc.putText(frame, label, new Point(left, top),
121                         Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(0, 0, 0));
122             }
123         }
124         return frame;
125     }
126 
onCameraViewStopped()127     public void onCameraViewStopped() {}
128 
129     // Upload file to storage and return a path.
getPath(String file, Context context)130     private static String getPath(String file, Context context) {
131         AssetManager assetManager = context.getAssets();
132 
133         BufferedInputStream inputStream = null;
134         try {
135             // Read data from assets.
136             inputStream = new BufferedInputStream(assetManager.open(file));
137             byte[] data = new byte[inputStream.available()];
138             inputStream.read(data);
139             inputStream.close();
140 
141             // Create copy file in storage.
142             File outFile = new File(context.getFilesDir(), file);
143             FileOutputStream os = new FileOutputStream(outFile);
144             os.write(data);
145             os.close();
146             // Return a path to file which may be read in common way.
147             return outFile.getAbsolutePath();
148         } catch (IOException ex) {
149             Log.i(TAG, "Failed to upload a file");
150         }
151         return "";
152     }
153 
154     private static final String TAG = "OpenCV/Sample/MobileNet";
155     private static final String[] classNames = {"background",
156             "aeroplane", "bicycle", "bird", "boat",
157             "bottle", "bus", "car", "cat", "chair",
158             "cow", "diningtable", "dog", "horse",
159             "motorbike", "person", "pottedplant",
160             "sheep", "sofa", "train", "tvmonitor"};
161 
162     private Net net;
163     private CameraBridgeViewBase mOpenCvCameraView;
164 }
165