mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 15:53:03 +04:00
Merge pull request #28678 from omrope79:caffe-importer-cleanup
Caffe importer cleanup #28678 Merge with: https://github.com/opencv/opencv_extra/pull/1324 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable - [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
@@ -1,24 +1,20 @@
|
||||
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)
|
||||
# Explicitly ensure resource directories exist to guarantee smooth resource compiling
|
||||
file(MAKE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/res/raw")
|
||||
|
||||
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)
|
||||
# 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})
|
||||
|
||||
+18
-12
@@ -54,15 +54,19 @@ public class MainActivity extends CameraActivity implements CvCameraViewListener
|
||||
}
|
||||
|
||||
//! [init_model_from_memory]
|
||||
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");
|
||||
// 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);
|
||||
|
||||
net = Dnn.readNet("caffe", mModelBuffer, mConfigBuffer);
|
||||
Log.i(TAG, "Network loaded successfully");
|
||||
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);
|
||||
@@ -91,8 +95,8 @@ public class MainActivity extends CameraActivity implements CvCameraViewListener
|
||||
if (mOpenCvCameraView != null)
|
||||
mOpenCvCameraView.disableView();
|
||||
|
||||
mModelBuffer.release();
|
||||
mConfigBuffer.release();
|
||||
if (mModelBuffer != null)
|
||||
mModelBuffer.release();
|
||||
}
|
||||
|
||||
// Load a network.
|
||||
@@ -107,7 +111,10 @@ public class MainActivity extends CameraActivity implements CvCameraViewListener
|
||||
final double MEAN_VAL = 127.5;
|
||||
final double THRESHOLD = 0.2;
|
||||
|
||||
// Get a new frame
|
||||
if (net == null) {
|
||||
return inputFrame.rgba();
|
||||
}
|
||||
|
||||
Log.d(TAG, "handle new frame!");
|
||||
Mat frame = inputFrame.rgba();
|
||||
Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2RGB);
|
||||
@@ -188,7 +195,6 @@ public class MainActivity extends CameraActivity implements CvCameraViewListener
|
||||
"motorbike", "person", "pottedplant",
|
||||
"sheep", "sofa", "train", "tvmonitor"};
|
||||
|
||||
private MatOfByte mConfigBuffer;
|
||||
private MatOfByte mModelBuffer;
|
||||
private Net net;
|
||||
private CameraBridgeViewBase mOpenCvCameraView;
|
||||
|
||||
Reference in New Issue
Block a user