mirror of
https://github.com/opencv/opencv.git
synced 2025-06-08 01:53:19 +08:00
Fix build for Android sample mobilenet-ssd detector.
This commit is contained in:
parent
7833c63388
commit
9d454fed93
@ -12,6 +12,7 @@ add_subdirectory(qr-detection)
|
|||||||
add_subdirectory(image-manipulations)
|
add_subdirectory(image-manipulations)
|
||||||
add_subdirectory(camera-calibration)
|
add_subdirectory(camera-calibration)
|
||||||
add_subdirectory(color-blob-detection)
|
add_subdirectory(color-blob-detection)
|
||||||
|
add_subdirectory(mobilenet-objdetect)
|
||||||
add_subdirectory(tutorial-1-camerapreview)
|
add_subdirectory(tutorial-1-camerapreview)
|
||||||
add_subdirectory(tutorial-2-mixedprocessing)
|
add_subdirectory(tutorial-2-mixedprocessing)
|
||||||
add_subdirectory(tutorial-3-cameracontrol)
|
add_subdirectory(tutorial-3-cameracontrol)
|
||||||
|
@ -1,5 +1,25 @@
|
|||||||
set(sample example-mobilenet-objdetect)
|
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}")
|
add_android_project(${sample} "${CMAKE_CURRENT_SOURCE_DIR}" LIBRARY_DEPS "${OPENCV_ANDROID_LIB_DIR}" SDK_TARGET 11 "${ANDROID_SDK_TARGET}")
|
||||||
if(TARGET ${sample})
|
if(TARGET ${sample})
|
||||||
add_dependencies(opencv_android_examples ${sample})
|
add_dependencies(opencv_android_examples ${sample})
|
||||||
|
@ -1,15 +1,11 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="org.opencv.samples.opencv_mobilenet"
|
package="org.opencv.samples.opencv_mobilenet">
|
||||||
>
|
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
|
||||||
android:icon="@mipmap/ic_launcher"
|
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:icon="@drawable/icon">
|
||||||
android:supportsRtl="true"
|
|
||||||
android:theme="@style/Theme.AppCompat.NoActionBar"> <!--Full screen mode-->
|
|
||||||
<activity
|
<activity
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
|
BIN
samples/android/mobilenet-objdetect/res/drawable/icon.png
Normal file
BIN
samples/android/mobilenet-objdetect/res/drawable/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.0 KiB |
@ -1,3 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">opencv_mobilenet</string>
|
<string name="app_name">OCV Mobilenet Objdetect</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -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);
|
|
||||||
inputStream.close();
|
|
||||||
|
|
||||||
// Create copy file in storage.
|
int size = is.available();
|
||||||
File outFile = new File(context.getFilesDir(), file);
|
buffer = new byte[size];
|
||||||
FileOutputStream os = new FileOutputStream(outFile);
|
int bytesRead = is.read(buffer);
|
||||||
os.write(data);
|
is.close();
|
||||||
os.close();
|
} catch (IOException e) {
|
||||||
// Return a path to file which may be read in common way.
|
e.printStackTrace();
|
||||||
return outFile.getAbsolutePath();
|
Log.e(TAG, "Failed to ONNX model from resources! Exception thrown: " + e);
|
||||||
} catch (IOException ex) {
|
(Toast.makeText(this, "Failed to ONNX model from resources!", Toast.LENGTH_LONG)).show();
|
||||||
Log.i(TAG, "Failed to upload a file");
|
return null;
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String TAG = "OpenCV/Sample/MobileNet";
|
return new MatOfByte(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 MatOfByte mConfigBuffer;
|
||||||
|
private MatOfByte mModelBuffer;
|
||||||
private Net net;
|
private Net net;
|
||||||
private CameraBridgeViewBase mOpenCvCameraView;
|
private CameraBridgeViewBase mOpenCvCameraView;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user