vendor: OpenCV 5.0.0 snapshot at 40738fb16ceddb5fb3fea747585f7ce6abb0605b
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
set(sample example-mobilenet-objdetect)
|
||||
|
||||
# Explicitly ensure resource directories exist to guarantee smooth resource compiling
|
||||
file(MAKE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/res/raw")
|
||||
|
||||
# The Caffe MobileNet-SSD (mobilenet_iter_73000.caffemodel + deploy.prototxt) has been
|
||||
# replaced by a single-file ONNX model. The sample now loads "res/raw/mobilenet.onnx".
|
||||
if((DEFINED ENV{OPENCV_MOBILENET_SSD_ONNX_URL} OR DEFINED OPENCV_MOBILENET_SSD_ONNX_URL) AND OPENCV_MOBILENET_SSD_ONNX_HASH)
|
||||
ocv_download(FILENAME "mobilenet.onnx"
|
||||
HASH "${OPENCV_MOBILENET_SSD_ONNX_HASH}"
|
||||
URL
|
||||
"${OPENCV_MOBILENET_SSD_ONNX_URL}"
|
||||
"$ENV{OPENCV_MOBILENET_SSD_ONNX_URL}"
|
||||
DESTINATION_DIR "${CMAKE_CURRENT_SOURCE_DIR}/res/raw"
|
||||
ID OPENCV_MOBILENET_SSD_ONNX
|
||||
STATUS res)
|
||||
endif()
|
||||
|
||||
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})
|
||||
endif()
|
||||
@@ -0,0 +1,37 @@
|
||||
apply plugin: 'com.android.application'
|
||||
|
||||
android {
|
||||
namespace 'org.opencv.samples.opencv_mobilenet'
|
||||
compileSdkVersion @ANDROID_COMPILE_SDK_VERSION@
|
||||
defaultConfig {
|
||||
applicationId "org.opencv.samples.opencv_mobilenet"
|
||||
minSdkVersion @ANDROID_MIN_SDK_VERSION@
|
||||
targetSdkVersion @ANDROID_TARGET_SDK_VERSION@
|
||||
versionCode 341
|
||||
versionName "3.41"
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
sourceSets {
|
||||
main {
|
||||
java.srcDirs = @ANDROID_SAMPLE_JAVA_PATH@
|
||||
res.srcDirs = @ANDROID_SAMPLE_RES_PATH@
|
||||
manifest.srcFile '@ANDROID_SAMPLE_MANIFEST_PATH@'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
//implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
if (gradle.opencv_source == "sdk_path") {
|
||||
println 'Using OpenCV from SDK'
|
||||
implementation project(':opencv')
|
||||
} else if (gradle.opencv_source == "maven_local" || gradle.opencv_source == "maven_central") {
|
||||
println 'Using OpenCV from Maven repo'
|
||||
implementation 'org.opencv:opencv:@OPENCV_VERSION_PLAIN@'
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.opencv.samples.opencv_mobilenet">
|
||||
|
||||
<application
|
||||
android:label="@string/app_name"
|
||||
android:icon="@drawable/icon">
|
||||
<!-- //! [mobilenet_tutorial] -->
|
||||
<activity
|
||||
android:exported="true"
|
||||
android:name=".MainActivity"
|
||||
android:screenOrientation="landscape"> <!--Screen orientation-->
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
<!--Allow to use a camera-->
|
||||
<uses-permission android:name="android.permission.CAMERA"/>
|
||||
<uses-feature android:name="android.hardware.camera" android:required="false"/>
|
||||
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
|
||||
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
|
||||
<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>
|
||||
|
||||
</manifest>
|
||||
<!-- //! [mobilenet_tutorial] -->
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 2.0 KiB |
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context="org.opencv.samples.opencv_mobilenet.MainActivity">
|
||||
|
||||
<org.opencv.android.JavaCameraView
|
||||
android:id="@+id/CameraView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:visibility="visible" />
|
||||
</FrameLayout>
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">OCV Mobilenet Objdetect</string>
|
||||
</resources>
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
package org.opencv.samples.opencv_mobilenet;
|
||||
/*
|
||||
// snippet was added for Android tutorial
|
||||
//! [mobilenet_tutorial_package]
|
||||
package com.example.myapplication;
|
||||
//! [mobilenet_tutorial_package]
|
||||
*/
|
||||
//! [mobilenet_tutorial]
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
import android.os.Bundle;
|
||||
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;
|
||||
import org.opencv.dnn.Net;
|
||||
import org.opencv.dnn.Dnn;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class MainActivity extends CameraActivity implements CvCameraViewListener2 {
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
if (mOpenCvCameraView != null)
|
||||
mOpenCvCameraView.enableView();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
if (OpenCVLoader.initLocal()) {
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
} else {
|
||||
Log.e(TAG, "OpenCV initialization failed!");
|
||||
(Toast.makeText(this, "OpenCV initialization failed!", Toast.LENGTH_LONG)).show();
|
||||
return;
|
||||
}
|
||||
|
||||
//! [init_model_from_memory]
|
||||
// Load a single-file MobileNet-SSD ONNX model from res/raw/mobilenet.onnx.
|
||||
// The resource is resolved by name at runtime so the sample still builds when the
|
||||
// (optionally downloaded) model is absent; object detection is simply disabled then.
|
||||
int modelResId = getResources().getIdentifier("mobilenet", "raw", getPackageName());
|
||||
if (modelResId != 0)
|
||||
mModelBuffer = loadFileFromResource(modelResId);
|
||||
|
||||
if (mModelBuffer == null) {
|
||||
Log.e(TAG, "MobileNet ONNX model (res/raw/mobilenet.onnx) not found - object detection disabled");
|
||||
} else {
|
||||
net = Dnn.readNetFromONNX(mModelBuffer);
|
||||
Log.i(TAG, "Network loaded successfully");
|
||||
}
|
||||
//! [init_model_from_memory]
|
||||
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
// Set up camera listener.
|
||||
mOpenCvCameraView = (CameraBridgeViewBase)findViewById(R.id.CameraView);
|
||||
mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
|
||||
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();
|
||||
|
||||
if (mModelBuffer != null)
|
||||
mModelBuffer.release();
|
||||
}
|
||||
|
||||
// Load a network.
|
||||
public void onCameraViewStarted(int width, int height) {
|
||||
}
|
||||
|
||||
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
|
||||
final int IN_WIDTH = 300;
|
||||
final int IN_HEIGHT = 300;
|
||||
final float WH_RATIO = (float)IN_WIDTH / IN_HEIGHT;
|
||||
final double IN_SCALE_FACTOR = 0.007843;
|
||||
final double MEAN_VAL = 127.5;
|
||||
final double THRESHOLD = 0.2;
|
||||
|
||||
if (net == null) {
|
||||
return inputFrame.rgba();
|
||||
}
|
||||
|
||||
Log.d(TAG, "handle new frame!");
|
||||
Mat frame = inputFrame.rgba();
|
||||
Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2RGB);
|
||||
|
||||
// Forward image through network.
|
||||
//! [mobilenet_handle_frame]
|
||||
Mat blob = Dnn.blobFromImage(frame, IN_SCALE_FACTOR,
|
||||
new Size(IN_WIDTH, IN_HEIGHT),
|
||||
new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL), /*swapRB*/false, /*crop*/false);
|
||||
net.setInput(blob);
|
||||
Mat detections = net.forward();
|
||||
|
||||
int cols = frame.cols();
|
||||
int rows = frame.rows();
|
||||
|
||||
detections = detections.reshape(1, (int)detections.total() / 7);
|
||||
|
||||
for (int i = 0; i < detections.rows(); ++i) {
|
||||
double confidence = detections.get(i, 2)[0];
|
||||
if (confidence > THRESHOLD) {
|
||||
int classId = (int)detections.get(i, 1)[0];
|
||||
|
||||
int left = (int)(detections.get(i, 3)[0] * cols);
|
||||
int top = (int)(detections.get(i, 4)[0] * rows);
|
||||
int right = (int)(detections.get(i, 5)[0] * cols);
|
||||
int bottom = (int)(detections.get(i, 6)[0] * rows);
|
||||
|
||||
// Draw rectangle around detected object.
|
||||
Imgproc.rectangle(frame, new Point(left, top), new Point(right, bottom),
|
||||
new Scalar(0, 255, 0));
|
||||
String label = classNames[classId] + ": " + confidence;
|
||||
int[] baseLine = new int[1];
|
||||
Size labelSize = Imgproc.getTextSize(label, Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, 1, baseLine);
|
||||
|
||||
// Draw background for label.
|
||||
Imgproc.rectangle(frame, new Point(left, top - labelSize.height),
|
||||
new Point(left + labelSize.width, top + baseLine[0]),
|
||||
new Scalar(255, 255, 255), Imgproc.FILLED);
|
||||
// Write class name and confidence.
|
||||
Imgproc.putText(frame, label, new Point(left, top),
|
||||
Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(0, 0, 0));
|
||||
}
|
||||
}
|
||||
//! [mobilenet_handle_frame]
|
||||
|
||||
return frame;
|
||||
}
|
||||
|
||||
public void onCameraViewStopped() {}
|
||||
|
||||
//! [mobilenet_tutorial_resource]
|
||||
private MatOfByte loadFileFromResource(int id) {
|
||||
byte[] buffer;
|
||||
try {
|
||||
// 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 new MatOfByte(buffer);
|
||||
}
|
||||
//! [mobilenet_tutorial_resource]
|
||||
|
||||
private static final String TAG = "OpenCV-MobileNet";
|
||||
private static final String[] classNames = {"background",
|
||||
"aeroplane", "bicycle", "bird", "boat",
|
||||
"bottle", "bus", "car", "cat", "chair",
|
||||
"cow", "diningtable", "dog", "horse",
|
||||
"motorbike", "person", "pottedplant",
|
||||
"sheep", "sofa", "train", "tvmonitor"};
|
||||
|
||||
private MatOfByte mModelBuffer;
|
||||
private Net net;
|
||||
private CameraBridgeViewBase mOpenCvCameraView;
|
||||
}
|
||||
//! [mobilenet_tutorial]
|
||||
Reference in New Issue
Block a user