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