Merge pull request #24616 from asmorkalov:as/android_mobilenet_refresh

Fix Android sample mobilenet-ssd detector
pull/24655/head
Alexander Smorkalov 1 year ago committed by GitHub
commit ac649b2ffd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      samples/android/CMakeLists.txt
  2. 20
      samples/android/mobilenet-objdetect/CMakeLists.txt
  3. 10
      samples/android/mobilenet-objdetect/gradle/AndroidManifest.xml
  4. BIN
      samples/android/mobilenet-objdetect/res/drawable/icon.png
  5. 3
      samples/android/mobilenet-objdetect/res/values/strings.xml
  6. 94
      samples/android/mobilenet-objdetect/src/org/opencv/samples/opencv_mobilenet/MainActivity.java

@ -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)

@ -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})

@ -1,15 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.opencv.samples.opencv_mobilenet"
>
package="org.opencv.samples.opencv_mobilenet">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.NoActionBar"> <!--Full screen mode-->
android:icon="@drawable/icon">
<activity
android:exported="true"
android:name=".MainActivity"

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">opencv_mobilenet</string>
<string name="app_name">OCV Mobilenet Objdetect</string>
</resources>

@ -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;
}

Loading…
Cancel
Save