|
|
|
@ -3,15 +3,17 @@ package org.opencv.samples.opencv_mobilenet; |
|
|
|
|
import android.content.Context; |
|
|
|
|
import android.content.res.AssetManager; |
|
|
|
|
import android.os.Bundle; |
|
|
|
|
import android.support.v7.app.AppCompatActivity; |
|
|
|
|
import android.util.Log; |
|
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
|
|
import org.opencv.android.CameraActivity; |
|
|
|
|
import org.opencv.android.CameraBridgeViewBase; |
|
|
|
|
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; |
|
|
|
|
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; |
|
|
|
|
import org.opencv.android.OpenCVLoader; |
|
|
|
|
import org.opencv.core.Core; |
|
|
|
|
import org.opencv.core.Mat; |
|
|
|
|
import org.opencv.core.MatOfByte; |
|
|
|
|
import org.opencv.core.Point; |
|
|
|
|
import org.opencv.core.Scalar; |
|
|
|
|
import org.opencv.core.Size; |
|
|
|
@ -19,13 +21,12 @@ import org.opencv.dnn.Net; |
|
|
|
|
import org.opencv.dnn.Dnn; |
|
|
|
|
import org.opencv.imgproc.Imgproc; |
|
|
|
|
|
|
|
|
|
import java.io.BufferedInputStream; |
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.FileOutputStream; |
|
|
|
|
import java.io.InputStream; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import android.widget.Toast; |
|
|
|
|
import java.util.Collections; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity implements CvCameraViewListener2 { |
|
|
|
|
public class MainActivity extends CameraActivity implements CvCameraViewListener2 { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onResume() { |
|
|
|
@ -46,6 +47,17 @@ public class MainActivity extends AppCompatActivity implements CvCameraViewListe |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mModelBuffer = loadFileFromResource(R.raw.mobilenet_iter_73000); |
|
|
|
|
mConfigBuffer = loadFileFromResource(R.raw.deploy); |
|
|
|
|
if (mModelBuffer == null || mConfigBuffer == null) { |
|
|
|
|
Log.e(TAG, "Failed to load model from resources"); |
|
|
|
|
} else |
|
|
|
|
Log.i(TAG, "Model files loaded successfully"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
net = Dnn.readNet("caffe", mModelBuffer, mConfigBuffer); |
|
|
|
|
Log.i(TAG, "Network loaded successfully"); |
|
|
|
|
|
|
|
|
|
setContentView(R.layout.activity_main); |
|
|
|
|
|
|
|
|
|
// Set up camera listener.
|
|
|
|
@ -54,12 +66,30 @@ public class MainActivity extends AppCompatActivity implements CvCameraViewListe |
|
|
|
|
mOpenCvCameraView.setCvCameraViewListener(this); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onPause() |
|
|
|
|
{ |
|
|
|
|
super.onPause(); |
|
|
|
|
if (mOpenCvCameraView != null) |
|
|
|
|
mOpenCvCameraView.disableView(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected List<? extends CameraBridgeViewBase> getCameraViewList() { |
|
|
|
|
return Collections.singletonList(mOpenCvCameraView); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void onDestroy() { |
|
|
|
|
super.onDestroy(); |
|
|
|
|
if (mOpenCvCameraView != null) |
|
|
|
|
mOpenCvCameraView.disableView(); |
|
|
|
|
|
|
|
|
|
mModelBuffer.release(); |
|
|
|
|
mConfigBuffer.release(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Load a network.
|
|
|
|
|
public void onCameraViewStarted(int width, int height) { |
|
|
|
|
String proto = getPath("MobileNetSSD_deploy.prototxt", this); |
|
|
|
|
String weights = getPath("MobileNetSSD_deploy.caffemodel", this); |
|
|
|
|
net = Dnn.readNetFromCaffe(proto, weights); |
|
|
|
|
Log.i(TAG, "Network loaded successfully"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { |
|
|
|
@ -71,6 +101,7 @@ public class MainActivity extends AppCompatActivity implements CvCameraViewListe |
|
|
|
|
final double THRESHOLD = 0.2; |
|
|
|
|
|
|
|
|
|
// Get a new frame
|
|
|
|
|
Log.d(TAG, "handle new frame!"); |
|
|
|
|
Mat frame = inputFrame.rgba(); |
|
|
|
|
Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2RGB); |
|
|
|
|
|
|
|
|
@ -117,32 +148,27 @@ public class MainActivity extends AppCompatActivity implements CvCameraViewListe |
|
|
|
|
|
|
|
|
|
public void onCameraViewStopped() {} |
|
|
|
|
|
|
|
|
|
// Upload file to storage and return a path.
|
|
|
|
|
private static String getPath(String file, Context context) { |
|
|
|
|
AssetManager assetManager = context.getAssets(); |
|
|
|
|
|
|
|
|
|
BufferedInputStream inputStream = null; |
|
|
|
|
private MatOfByte loadFileFromResource(int id) { |
|
|
|
|
byte[] buffer; |
|
|
|
|
try { |
|
|
|
|
// Read data from assets.
|
|
|
|
|
inputStream = new BufferedInputStream(assetManager.open(file)); |
|
|
|
|
byte[] data = new byte[inputStream.available()]; |
|
|
|
|
inputStream.read(data); |
|
|
|
|
inputStream.close(); |
|
|
|
|
|
|
|
|
|
// Create copy file in storage.
|
|
|
|
|
File outFile = new File(context.getFilesDir(), file); |
|
|
|
|
FileOutputStream os = new FileOutputStream(outFile); |
|
|
|
|
os.write(data); |
|
|
|
|
os.close(); |
|
|
|
|
// Return a path to file which may be read in common way.
|
|
|
|
|
return outFile.getAbsolutePath(); |
|
|
|
|
} catch (IOException ex) { |
|
|
|
|
Log.i(TAG, "Failed to upload a file"); |
|
|
|
|
// load cascade file from application resources
|
|
|
|
|
InputStream is = getResources().openRawResource(id); |
|
|
|
|
|
|
|
|
|
int size = is.available(); |
|
|
|
|
buffer = new byte[size]; |
|
|
|
|
int bytesRead = is.read(buffer); |
|
|
|
|
is.close(); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
Log.e(TAG, "Failed to ONNX model from resources! Exception thrown: " + e); |
|
|
|
|
(Toast.makeText(this, "Failed to ONNX model from resources!", Toast.LENGTH_LONG)).show(); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
return ""; |
|
|
|
|
|
|
|
|
|
return new MatOfByte(buffer); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static final String TAG = "OpenCV/Sample/MobileNet"; |
|
|
|
|
private static final String TAG = "OpenCV-MobileNet"; |
|
|
|
|
private static final String[] classNames = {"background", |
|
|
|
|
"aeroplane", "bicycle", "bird", "boat", |
|
|
|
|
"bottle", "bus", "car", "cat", "chair", |
|
|
|
@ -150,6 +176,8 @@ public class MainActivity extends AppCompatActivity implements CvCameraViewListe |
|
|
|
|
"motorbike", "person", "pottedplant", |
|
|
|
|
"sheep", "sofa", "train", "tvmonitor"}; |
|
|
|
|
|
|
|
|
|
private Net net; |
|
|
|
|
private MatOfByte mConfigBuffer; |
|
|
|
|
private MatOfByte mModelBuffer; |
|
|
|
|
private Net net; |
|
|
|
|
private CameraBridgeViewBase mOpenCvCameraView; |
|
|
|
|
} |
|
|
|
|