diff --git a/samples/android/CMakeLists.txt b/samples/android/CMakeLists.txt index 360a2189b8..f4fe882447 100644 --- a/samples/android/CMakeLists.txt +++ b/samples/android/CMakeLists.txt @@ -12,6 +12,7 @@ add_subdirectory(qr-detection) add_subdirectory(image-manipulations) add_subdirectory(camera-calibration) add_subdirectory(color-blob-detection) +add_subdirectory(mobilenet-objdetect) add_subdirectory(tutorial-1-camerapreview) add_subdirectory(tutorial-2-mixedprocessing) add_subdirectory(tutorial-3-cameracontrol) diff --git a/samples/android/mobilenet-objdetect/CMakeLists.txt b/samples/android/mobilenet-objdetect/CMakeLists.txt index 4e41ebfb28..85abfae6a7 100644 --- a/samples/android/mobilenet-objdetect/CMakeLists.txt +++ b/samples/android/mobilenet-objdetect/CMakeLists.txt @@ -1,5 +1,25 @@ set(sample example-mobilenet-objdetect) +ocv_download(FILENAME "mobilenet_iter_73000.caffemodel" + HASH "bbcb3b6a0afe1ec89e1288096b5b8c66" + URL + "${OPENCV_MOBILENET_SSD_WEIGHTS_URL}" + "$ENV{OPENCV_MOBILENET_SSD_WEIGHTS_URL}" + "https://raw.githubusercontent.com/chuanqi305/MobileNet-SSD/97406996b1eee2d40eb0a00ae567cf41e23369f9/mobilenet_iter_73000.caffemodel" + DESTINATION_DIR "${CMAKE_CURRENT_LIST_DIR}/res/raw" + ID OPENCV_MOBILENET_SSD_WEIGHTS + STATUS res) + +ocv_download(FILENAME "deploy.prototxt" + HASH "f1978dc4fe20c680e850ce99830c5945" + URL + "${OPENCV_MOBILENET_SSD_CONFIG_URL}" + "$ENV{OPENCV_MOBILENET_SSD_CONFIG_URL}" + "https://raw.githubusercontent.com/chuanqi305/MobileNet-SSD/97406996b1eee2d40eb0a00ae567cf41e23369f9/deploy.prototxt" + DESTINATION_DIR "${CMAKE_CURRENT_LIST_DIR}/res/raw" + ID OPENCV_MOBILENET_SSD_CONFIG + STATUS res) + add_android_project(${sample} "${CMAKE_CURRENT_SOURCE_DIR}" LIBRARY_DEPS "${OPENCV_ANDROID_LIB_DIR}" SDK_TARGET 11 "${ANDROID_SDK_TARGET}") if(TARGET ${sample}) add_dependencies(opencv_android_examples ${sample}) diff --git a/samples/android/mobilenet-objdetect/gradle/AndroidManifest.xml b/samples/android/mobilenet-objdetect/gradle/AndroidManifest.xml index 473a5db3d0..ef50659a2d 100644 --- a/samples/android/mobilenet-objdetect/gradle/AndroidManifest.xml +++ b/samples/android/mobilenet-objdetect/gradle/AndroidManifest.xml @@ -1,15 +1,11 @@ + package="org.opencv.samples.opencv_mobilenet"> + android:icon="@drawable/icon"> + - opencv_mobilenet + OCV Mobilenet Objdetect diff --git a/samples/android/mobilenet-objdetect/src/org/opencv/samples/opencv_mobilenet/MainActivity.java b/samples/android/mobilenet-objdetect/src/org/opencv/samples/opencv_mobilenet/MainActivity.java index f14cf1737c..e2add816c6 100644 --- a/samples/android/mobilenet-objdetect/src/org/opencv/samples/opencv_mobilenet/MainActivity.java +++ b/samples/android/mobilenet-objdetect/src/org/opencv/samples/opencv_mobilenet/MainActivity.java @@ -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 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(); + // load cascade file from application resources + InputStream is = getResources().openRawResource(id); - // 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"); + 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; }