|
| 1 | +// ------------------------- OpenPose C++ API Tutorial - Example 13 - Custom Pre-processing ------------------------- |
| 2 | +// Synchronous mode: ideal for production integration. It provides the fastest results with respect to runtime |
| 3 | +// performance. |
| 4 | +// In this function, the user can implement its own pre-processing, i.e., his function will be called after the image |
| 5 | +// has been read by OpenPose but before OpenPose processes the frames. |
| 6 | + |
| 7 | +// Command-line user intraface |
| 8 | +#include <openpose/flags.hpp> |
| 9 | +// OpenPose dependencies |
| 10 | +#include <openpose/headers.hpp> |
| 11 | + |
| 12 | +// This worker will just invert the image |
| 13 | +class WUserPreProcessing : public op::Worker<std::shared_ptr<std::vector<std::shared_ptr<op::Datum>>>> |
| 14 | +{ |
| 15 | +public: |
| 16 | + WUserPreProcessing() |
| 17 | + { |
| 18 | + // User's constructor here |
| 19 | + } |
| 20 | + |
| 21 | + void initializationOnThread() {} |
| 22 | + |
| 23 | + void work(std::shared_ptr<std::vector<std::shared_ptr<op::Datum>>>& datumsPtr) |
| 24 | + { |
| 25 | + // User's pre-processing (after OpenPose read the input image & before OpenPose processing) here |
| 26 | + // datumPtr->cvInputData: input frame |
| 27 | + try |
| 28 | + { |
| 29 | + if (datumsPtr != nullptr && !datumsPtr->empty()) |
| 30 | + for (auto& datumPtr : *datumsPtr) |
| 31 | + cv::bitwise_not(datumPtr->cvOutputData, datumPtr->cvOutputData); |
| 32 | + } |
| 33 | + catch (const std::exception& e) |
| 34 | + { |
| 35 | + this->stop(); |
| 36 | + op::error(e.what(), __LINE__, __FUNCTION__, __FILE__); |
| 37 | + } |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +void configureWrapper(op::Wrapper& opWrapper) |
| 42 | +{ |
| 43 | + try |
| 44 | + { |
| 45 | + // Configuring OpenPose |
| 46 | + |
| 47 | + // logging_level |
| 48 | + op::check(0 <= FLAGS_logging_level && FLAGS_logging_level <= 255, "Wrong logging_level value.", |
| 49 | + __LINE__, __FUNCTION__, __FILE__); |
| 50 | + op::ConfigureLog::setPriorityThreshold((op::Priority)FLAGS_logging_level); |
| 51 | + op::Profiler::setDefaultX(FLAGS_profile_speed); |
| 52 | + |
| 53 | + // Applying user defined configuration - GFlags to program variables |
| 54 | + // producerType |
| 55 | + op::ProducerType producerType; |
| 56 | + std::string producerString; |
| 57 | + std::tie(producerType, producerString) = op::flagsToProducer( |
| 58 | + FLAGS_image_dir, FLAGS_video, FLAGS_ip_camera, FLAGS_camera, FLAGS_flir_camera, FLAGS_flir_camera_index); |
| 59 | + // cameraSize |
| 60 | + const auto cameraSize = op::flagsToPoint(FLAGS_camera_resolution, "-1x-1"); |
| 61 | + // outputSize |
| 62 | + const auto outputSize = op::flagsToPoint(FLAGS_output_resolution, "-1x-1"); |
| 63 | + // netInputSize |
| 64 | + const auto netInputSize = op::flagsToPoint(FLAGS_net_resolution, "-1x368"); |
| 65 | + // faceNetInputSize |
| 66 | + const auto faceNetInputSize = op::flagsToPoint(FLAGS_face_net_resolution, "368x368 (multiples of 16)"); |
| 67 | + // handNetInputSize |
| 68 | + const auto handNetInputSize = op::flagsToPoint(FLAGS_hand_net_resolution, "368x368 (multiples of 16)"); |
| 69 | + // poseModel |
| 70 | + const auto poseModel = op::flagsToPoseModel(FLAGS_model_pose); |
| 71 | + // JSON saving |
| 72 | + if (!FLAGS_write_keypoint.empty()) |
| 73 | + op::log("Flag `write_keypoint` is deprecated and will eventually be removed." |
| 74 | + " Please, use `write_json` instead.", op::Priority::Max); |
| 75 | + // keypointScaleMode |
| 76 | + const auto keypointScaleMode = op::flagsToScaleMode(FLAGS_keypoint_scale); |
| 77 | + // heatmaps to add |
| 78 | + const auto heatMapTypes = op::flagsToHeatMaps(FLAGS_heatmaps_add_parts, FLAGS_heatmaps_add_bkg, |
| 79 | + FLAGS_heatmaps_add_PAFs); |
| 80 | + const auto heatMapScaleMode = op::flagsToHeatMapScaleMode(FLAGS_heatmaps_scale); |
| 81 | + // >1 camera view? |
| 82 | + const auto multipleView = (FLAGS_3d || FLAGS_3d_views > 1 || FLAGS_flir_camera); |
| 83 | + // Face and hand detectors |
| 84 | + const auto faceDetector = op::flagsToDetector(FLAGS_face_detector); |
| 85 | + const auto handDetector = op::flagsToDetector(FLAGS_hand_detector); |
| 86 | + // Enabling Google Logging |
| 87 | + const bool enableGoogleLogging = true; |
| 88 | + |
| 89 | + // Initializing the user custom classes |
| 90 | + // Processing |
| 91 | + auto wUserPreProcessing = std::make_shared<WUserPreProcessing>(); |
| 92 | + // Add custom processing |
| 93 | + const auto workerProcessingOnNewThread = true; |
| 94 | + opWrapper.setWorker(op::WorkerType::PreProcessing, wUserPreProcessing, workerProcessingOnNewThread); |
| 95 | + |
| 96 | + // Pose configuration (use WrapperStructPose{} for default and recommended configuration) |
| 97 | + const op::WrapperStructPose wrapperStructPose{ |
| 98 | + !FLAGS_body_disable, netInputSize, outputSize, keypointScaleMode, FLAGS_num_gpu, FLAGS_num_gpu_start, |
| 99 | + FLAGS_scale_number, (float)FLAGS_scale_gap, op::flagsToRenderMode(FLAGS_render_pose, multipleView), |
| 100 | + poseModel, !FLAGS_disable_blending, (float)FLAGS_alpha_pose, (float)FLAGS_alpha_heatmap, |
| 101 | + FLAGS_part_to_show, FLAGS_model_folder, heatMapTypes, heatMapScaleMode, FLAGS_part_candidates, |
| 102 | + (float)FLAGS_render_threshold, FLAGS_number_people_max, FLAGS_maximize_positives, FLAGS_fps_max, |
| 103 | + FLAGS_prototxt_path, FLAGS_caffemodel_path, enableGoogleLogging}; |
| 104 | + opWrapper.configure(wrapperStructPose); |
| 105 | + // Face configuration (use op::WrapperStructFace{} to disable it) |
| 106 | + const op::WrapperStructFace wrapperStructFace{ |
| 107 | + FLAGS_face, faceDetector, faceNetInputSize, |
| 108 | + op::flagsToRenderMode(FLAGS_face_render, multipleView, FLAGS_render_pose), |
| 109 | + (float)FLAGS_face_alpha_pose, (float)FLAGS_face_alpha_heatmap, (float)FLAGS_face_render_threshold}; |
| 110 | + opWrapper.configure(wrapperStructFace); |
| 111 | + // Hand configuration (use op::WrapperStructHand{} to disable it) |
| 112 | + const op::WrapperStructHand wrapperStructHand{ |
| 113 | + FLAGS_hand, handDetector, handNetInputSize, FLAGS_hand_scale_number, (float)FLAGS_hand_scale_range, |
| 114 | + op::flagsToRenderMode(FLAGS_hand_render, multipleView, FLAGS_render_pose), (float)FLAGS_hand_alpha_pose, |
| 115 | + (float)FLAGS_hand_alpha_heatmap, (float)FLAGS_hand_render_threshold}; |
| 116 | + opWrapper.configure(wrapperStructHand); |
| 117 | + // Extra functionality configuration (use op::WrapperStructExtra{} to disable it) |
| 118 | + const op::WrapperStructExtra wrapperStructExtra{ |
| 119 | + FLAGS_3d, FLAGS_3d_min_views, FLAGS_identification, FLAGS_tracking, FLAGS_ik_threads}; |
| 120 | + opWrapper.configure(wrapperStructExtra); |
| 121 | + // Producer (use default to disable any input) |
| 122 | + const op::WrapperStructInput wrapperStructInput{ |
| 123 | + producerType, producerString, FLAGS_frame_first, FLAGS_frame_step, FLAGS_frame_last, |
| 124 | + FLAGS_process_real_time, FLAGS_frame_flip, FLAGS_frame_rotate, FLAGS_frames_repeat, |
| 125 | + cameraSize, FLAGS_camera_parameter_path, FLAGS_frame_undistort, FLAGS_3d_views}; |
| 126 | + opWrapper.configure(wrapperStructInput); |
| 127 | + // Output (comment or use default argument to disable any output) |
| 128 | + const op::WrapperStructOutput wrapperStructOutput{ |
| 129 | + FLAGS_cli_verbose, FLAGS_write_keypoint, op::stringToDataFormat(FLAGS_write_keypoint_format), |
| 130 | + FLAGS_write_json, FLAGS_write_coco_json, FLAGS_write_coco_foot_json, FLAGS_write_coco_json_variant, |
| 131 | + FLAGS_write_images, FLAGS_write_images_format, FLAGS_write_video, FLAGS_write_video_fps, |
| 132 | + FLAGS_write_video_with_audio, FLAGS_write_heatmaps, FLAGS_write_heatmaps_format, FLAGS_write_video_3d, |
| 133 | + FLAGS_write_video_adam, FLAGS_write_bvh, FLAGS_udp_host, FLAGS_udp_port}; |
| 134 | + opWrapper.configure(wrapperStructOutput); |
| 135 | + // GUI (comment or use default argument to disable any visual output) |
| 136 | + const op::WrapperStructGui wrapperStructGui{ |
| 137 | + op::flagsToDisplayMode(FLAGS_display, FLAGS_3d), !FLAGS_no_gui_verbose, FLAGS_fullscreen}; |
| 138 | + opWrapper.configure(wrapperStructGui); |
| 139 | + // Set to single-thread (for sequential processing and/or debugging and/or reducing latency) |
| 140 | + if (FLAGS_disable_multi_thread) |
| 141 | + opWrapper.disableMultiThreading(); |
| 142 | + } |
| 143 | + catch (const std::exception& e) |
| 144 | + { |
| 145 | + op::error(e.what(), __LINE__, __FUNCTION__, __FILE__); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +int tutorialApiCpp() |
| 150 | +{ |
| 151 | + try |
| 152 | + { |
| 153 | + op::log("Starting OpenPose demo...", op::Priority::High); |
| 154 | + const auto opTimer = op::getTimerInit(); |
| 155 | + |
| 156 | + // OpenPose wrapper |
| 157 | + op::log("Configuring OpenPose...", op::Priority::High); |
| 158 | + op::Wrapper opWrapper; |
| 159 | + configureWrapper(opWrapper); |
| 160 | + |
| 161 | + // Start, run, and stop processing - exec() blocks this thread until OpenPose wrapper has finished |
| 162 | + op::log("Starting thread(s)...", op::Priority::High); |
| 163 | + opWrapper.exec(); |
| 164 | + |
| 165 | + // Measuring total time |
| 166 | + op::printTime(opTimer, "OpenPose demo successfully finished. Total time: ", " seconds.", op::Priority::High); |
| 167 | + |
| 168 | + // Return |
| 169 | + return 0; |
| 170 | + } |
| 171 | + catch (const std::exception& e) |
| 172 | + { |
| 173 | + return -1; |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +int main(int argc, char *argv[]) |
| 178 | +{ |
| 179 | + // Parsing command line flags |
| 180 | + gflags::ParseCommandLineFlags(&argc, &argv, true); |
| 181 | + |
| 182 | + // Running tutorialApiCpp |
| 183 | + return tutorialApiCpp(); |
| 184 | +} |
0 commit comments