-
Notifications
You must be signed in to change notification settings - Fork 237
add mobilenet c++ sample #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aaadbd2
add mobilenet c++ sample
LaurentBerger efbc2de
review 1
LaurentBerger 643e473
use blobFromImageWithParams
LaurentBerger b19d4db
change int in dnn::Backend, dnn::Target
LaurentBerger a928056
review 2
LaurentBerger 83c905a
review 3
LaurentBerger a210c4b
add include for labels imagenet1k
LaurentBerger dd57787
samples::findfile
LaurentBerger b864275
Add -v option. 100 inferences max with a video when -v=false
LaurentBerger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
cmake_minimum_required(VERSION 3.2) | ||
set(project_name "opencv_zoo_image_classification_mobilenet") | ||
|
||
PROJECT (${project_name}) | ||
|
||
set(OPENCV_VERSION "4.7.0") | ||
set(OPENCV_INSTALLATION_PATH "" CACHE PATH "Where to look for OpenCV installation") | ||
find_package(OpenCV ${OPENCV_VERSION} REQUIRED HINTS ${OPENCV_INSTALLATION_PATH}) | ||
# Find OpenCV, you may need to set OpenCV_DIR variable | ||
# to the absolute path to the directory containing OpenCVConfig.cmake file | ||
# via the command line or GUI | ||
|
||
file(GLOB SourceFile | ||
"demo.cpp") | ||
# If the package has been found, several variables will | ||
# be set, you can find the full list with descriptions | ||
# in the OpenCVConfig.cmake file. | ||
# Print some message showing some of them | ||
message(STATUS "OpenCV library status:") | ||
message(STATUS " config: ${OpenCV_DIR}") | ||
message(STATUS " version: ${OpenCV_VERSION}") | ||
message(STATUS " libraries: ${OpenCV_LIBS}") | ||
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}") | ||
|
||
# Declare the executable target built from your sources | ||
add_executable(${project_name} ${SourceFile}) | ||
|
||
# Link your application with OpenCV libraries | ||
target_link_libraries(${project_name} PRIVATE ${OpenCV_LIBS}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#include <vector> | ||
#include <string> | ||
#include <utility> | ||
|
||
#include <opencv2/opencv.hpp> | ||
#include "labelsimagenet1k.h" | ||
|
||
using namespace std; | ||
using namespace cv; | ||
using namespace dnn; | ||
|
||
vector< pair<dnn::Backend, dnn::Target> > backendTargetPairs = { | ||
std::make_pair<dnn::Backend, dnn::Target>(dnn::DNN_BACKEND_OPENCV, dnn::DNN_TARGET_CPU), | ||
std::make_pair<dnn::Backend, dnn::Target>(dnn::DNN_BACKEND_CUDA, dnn::DNN_TARGET_CUDA), | ||
std::make_pair<dnn::Backend, dnn::Target>(dnn::DNN_BACKEND_CUDA, dnn::DNN_TARGET_CUDA_FP16), | ||
std::make_pair<dnn::Backend, dnn::Target>(dnn::DNN_BACKEND_TIMVX, dnn::DNN_TARGET_NPU), | ||
std::make_pair<dnn::Backend, dnn::Target>(dnn::DNN_BACKEND_CANN, dnn::DNN_TARGET_NPU) }; | ||
|
||
|
||
std::string keys = | ||
"{ help h | | Print help message. }" | ||
"{ model m | image_classification_mobilenetv1_2022apr.onnx | Usage: Set model type, defaults to image_classification_mobilenetv1_2022apr.onnx (v1) }" | ||
"{ input i | | Path to input image or video file. Skip this argument to capture frames from a camera.}" | ||
"{ initial_width | 0 | Preprocess input image by initial resizing to a specific width.}" | ||
"{ initial_height | 0 | Preprocess input image by initial resizing to a specific height.}" | ||
"{ rgb | true | swap R and B plane.}" | ||
"{ crop | false | Preprocess input image by center cropping.}" | ||
"{ vis v | true | Usage: Specify to open a new window to show results.}" | ||
"{ backend bt | 0 | Choose one of computation backends: " | ||
"0: (default) OpenCV implementation + CPU, " | ||
"1: CUDA + GPU (CUDA), " | ||
"2: CUDA + GPU (CUDA FP16), " | ||
"3: TIM-VX + NPU, " | ||
"4: CANN + NPU}"; | ||
|
||
|
||
int main(int argc, char** argv) | ||
{ | ||
CommandLineParser parser(argc, argv, keys); | ||
|
||
parser.about("Use this script to run classification deep learning networks in opencv Zoo using OpenCV."); | ||
if (parser.has("help")) | ||
{ | ||
parser.printMessage(); | ||
return 0; | ||
} | ||
|
||
int rszWidth = parser.get<int>("initial_width"); | ||
int rszHeight = parser.get<int>("initial_height"); | ||
bool swapRB = parser.get<bool>("rgb"); | ||
bool crop = parser.get<bool>("crop"); | ||
bool vis = parser.get<bool>("vis"); | ||
String model = parser.get<String>("model"); | ||
int backendTargetid = parser.get<int>("backend"); | ||
|
||
if (model.empty()) | ||
{ | ||
CV_Error(Error::StsError, "Model file " + model + " not found"); | ||
} | ||
vector<string> labels = getLabelsImagenet1k(); | ||
|
||
Net net = readNet(samples::findFile(model)); | ||
net.setPreferableBackend(backendTargetPairs[backendTargetid].first); | ||
net.setPreferableTarget(backendTargetPairs[backendTargetid].second); | ||
//! [Open a video file or an image file or a camera stream] | ||
VideoCapture cap; | ||
if (parser.has("input")) | ||
cap.open(samples::findFile(parser.get<String>("input"))); | ||
else | ||
cap.open(0); | ||
if (!cap.isOpened()) | ||
CV_Error(Error::StsError, "Cannot opend video or file"); | ||
Mat frame, blob; | ||
static const std::string kWinName = model; | ||
int nbInference = 0; | ||
while (waitKey(1) < 0) | ||
{ | ||
cap >> frame; | ||
if (frame.empty()) | ||
{ | ||
cout << "Frame is empty" << endl; | ||
waitKey(); | ||
break; | ||
} | ||
|
||
if (rszWidth != 0 && rszHeight != 0) | ||
{ | ||
resize(frame, frame, Size(rszWidth, rszHeight)); | ||
} | ||
Image2BlobParams paramMobilenet; | ||
paramMobilenet.datalayout = DNN_LAYOUT_NCHW; | ||
paramMobilenet.ddepth = CV_32F; | ||
paramMobilenet.mean = Scalar(123.675, 116.28, 103.53); | ||
paramMobilenet.scalefactor = Scalar(1 / (255. * 0.229), 1 / (255. * 0.224), 1 / (255. * 0.225)); | ||
paramMobilenet.size = Size(224, 224); | ||
paramMobilenet.swapRB = swapRB; | ||
if (crop) | ||
paramMobilenet.paddingmode = DNN_PMODE_CROP_CENTER; | ||
else | ||
paramMobilenet.paddingmode = DNN_PMODE_NULL; | ||
//! [Create a 4D blob from a frame] | ||
blobFromImageWithParams(frame, blob, paramMobilenet); | ||
|
||
//! [Set input blob] | ||
net.setInput(blob); | ||
Mat prob = net.forward(); | ||
|
||
//! [Get a class with a highest score] | ||
Point classIdPoint; | ||
double confidence; | ||
minMaxLoc(prob.reshape(1, 1), 0, &confidence, 0, &classIdPoint); | ||
int classId = classIdPoint.x; | ||
std::string label = format("%s: %.4f", (labels.empty() ? format("Class #%d", classId).c_str() : | ||
labels[classId].c_str()), | ||
confidence); | ||
if (vis) | ||
{ | ||
putText(frame, label, Point(0, 55), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0)); | ||
imshow(kWinName, frame); | ||
} | ||
else | ||
{ | ||
cout << label << endl; | ||
nbInference++; | ||
if (nbInference > 100) | ||
{ | ||
cout << nbInference << " inference made. Demo existing" << endl; | ||
break; | ||
} | ||
} | ||
} | ||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.