@@ -1023,51 +1023,51 @@ if __name__ == '__main__':
1023
1023
Here is a basic example of SDK C++ API:
1024
1024
1025
1025
``` C++
1026
- #include " mmdeploy/detector .hpp"
1027
-
1028
- #include " opencv2/imgcodecs/imgcodecs .hpp"
1029
- #include " utils/argparse.h"
1030
- # include " utils/visualize.h "
1031
-
1032
- DEFINE_ARG_string (model , "Model path");
1033
- DEFINE_ARG_string(image , "Input image path ");
1034
- DEFINE_string(device, "cpu" , R"(Device name, e.g. "cpu", "cuda" )");
1035
- DEFINE_string(output, "detector_output.jpg", "Output image path ");
1036
-
1037
- DEFINE_double(det_thr, .5, "Detection score threshold ");
1026
+ #include " mmdeploy/pose_detector .hpp"
1027
+ # include " opencv2/imgcodecs.hpp "
1028
+ #include " opencv2/imgproc .hpp"
1029
+ #include " utils/argparse.h" // See: https://github.com/open-mmlab/mmdeploy/blob/main/demo/csrc/cpp/utils/argparse.h
1030
+
1031
+ DEFINE_ARG_string (model_path, "Model path");
1032
+ DEFINE_ARG_string(image_path , "Input image path");
1033
+ DEFINE_string(device_name , "cpu", R"(Device name, e.g. "cpu", "cuda") ");
1034
+ DEFINE_int32(bbox_x, -1 , R"(x position of the bounding box )");
1035
+ DEFINE_int32(bbox_y, -1, R"(y position of the bounding box) ");
1036
+ DEFINE_int32(bbox_w, -1, R"(width of the bounding box)");
1037
+ DEFINE_int32(bbox_h, -1, R"(height of the bounding box) ");
1038
1038
1039
1039
int main(int argc, char* argv[ ] ) {
1040
1040
if (!utils::ParseArguments(argc, argv)) {
1041
1041
return -1;
1042
1042
}
1043
1043
1044
- cv::Mat img = cv::imread(ARGS_image);
1045
- if (img.empty()) {
1046
- fprintf(stderr, "failed to load image: %s\n", ARGS_image.c_str());
1047
- return -1;
1048
- }
1044
+ cv::Mat img = cv::imread(ARGS_image_path);
1045
+
1046
+ mmdeploy::PoseDetector detector(mmdeploy::Model{ARGS_model_path}, mmdeploy::Device{FLAGS_device_name, 0});
1049
1047
1050
- // construct a detector instance
1051
- mmdeploy::Detector detector(mmdeploy::Model{ARGS_model}, mmdeploy::Device{FLAGS_device});
1052
-
1053
- // apply the detector, the result is an array-like class holding references to
1054
- // ` mmdeploy_detection_t ` , will be released automatically on destruction
1055
- mmdeploy::Detector::Result dets = detector.Apply(img);
1056
-
1057
- // visualize
1058
- utils::Visualize v;
1059
- auto sess = v.get_session(img);
1060
- int count = 0;
1061
- for (const mmdeploy_detection_t& det : dets) {
1062
- if (det.score > FLAGS_det_thr) { // filter bboxes
1063
- sess.add_det(det.bbox, det.label_id, det.score, det.mask, count++);
1064
- }
1048
+ mmdeploy::PoseDetector::Result result{0, 0, nullptr};
1049
+
1050
+ if (FLAGS_bbox_x == -1 || FLAGS_bbox_y == -1 || FLAGS_bbox_w == -1 || FLAGS_bbox_h == -1) {
1051
+ result = detector.Apply(img);
1052
+ } else {
1053
+ // convert (x, y, w, h) -> (left, top, right, bottom)
1054
+ mmdeploy::cxx::Rect rect;
1055
+ rect.left = FLAGS_bbox_x;
1056
+ rect.top = FLAGS_bbox_y;
1057
+ rect.right = FLAGS_bbox_x + FLAGS_bbox_w;
1058
+ rect.bottom = FLAGS_bbox_y + FLAGS_bbox_h;
1059
+ result = detector.Apply(img, {rect});
1065
1060
}
1066
1061
1067
- if (!FLAGS_output.empty()) {
1068
- cv::imwrite(FLAGS_output, sess.get());
1062
+ // Draw circles at detected keypoints
1063
+ for (size_t i = 0; i < result[ 0] .length; ++i) {
1064
+ cv::Point keypoint(result[ 0] .point[ i] .x, result[ 0] .point[ i] .y);
1065
+ cv::circle(img, keypoint, 1, cv::Scalar(0, 255, 0), 2); // Draw filled circle
1069
1066
}
1070
1067
1068
+ // Save the output image
1069
+ cv::imwrite("output_pose.png", img);
1070
+
1071
1071
return 0;
1072
1072
}
1073
1073
```
0 commit comments