| 10 | #include <thread> |
| 11 | |
| 12 | int main(int argc, char** argv) { |
| 13 | if (argc != 5) { |
| 14 | std::cerr << "Usage: " << argv[0] << " <model_path> <image_path> <loop_count> <thread_num>" << std::endl; |
| 15 | return -1; |
| 16 | } |
| 17 | |
| 18 | std::string model_path = argv[1]; |
| 19 | std::string image_path = argv[2]; |
| 20 | int loop = std::stoi(argv[3]); |
| 21 | int thread_num = std::stoi(argv[4]); |
| 22 | |
| 23 | if (thread_num > 10) { |
| 24 | std::cerr << "Error: thread_num cannot be greater than 10" << std::endl; |
| 25 | return -1; |
| 26 | } |
| 27 | if (loop < 1000) { |
| 28 | std::cerr << "Error: loop count must be at least 1000" << std::endl; |
| 29 | return -1; |
| 30 | } |
| 31 | |
| 32 | INSPIREFACE_CONTEXT->Load(model_path); |
| 33 | inspirecv::Image image = inspirecv::Image::Create(image_path); |
| 34 | inspirecv::FrameProcess process = |
| 35 | inspirecv::FrameProcess::Create(image.Data(), image.Height(), image.Width(), inspirecv::BGR, inspirecv::ROTATION_0); |
| 36 | |
| 37 | inspire::parallel::ResourcePool<inspire::Session> sessionPool(thread_num, [](inspire::Session& session) { |
| 38 | |
| 39 | }); |
| 40 | |
| 41 | for (int i = 0; i < thread_num; ++i) { |
| 42 | inspire::CustomPipelineParameter param; |
| 43 | param.enable_recognition = true; |
| 44 | param.enable_liveness = true; |
| 45 | param.enable_mask_detect = true; |
| 46 | param.enable_face_attribute = true; |
| 47 | param.enable_face_quality = true; |
| 48 | inspire::Session session = inspire::Session::Create(inspire::DetectModuleMode::DETECT_MODE_ALWAYS_DETECT, 1, param); |
| 49 | sessionPool.AddResource(std::move(session)); |
| 50 | } |
| 51 | |
| 52 | std::vector<std::thread> threads; |
| 53 | int tasksPerThread = loop / thread_num; |
| 54 | int remainingTasks = loop % thread_num; |
| 55 | |
| 56 | // Run the task in parallel |
| 57 | for (int i = 0; i < thread_num; ++i) { |
| 58 | int taskCount = tasksPerThread + (i < remainingTasks ? 1 : 0); |
| 59 | threads.emplace_back([&, taskCount]() { |
| 60 | for (int j = 0; j < taskCount; ++j) { |
| 61 | auto sessionGuard = sessionPool.AcquireResource(); |
| 62 | std::vector<inspire::FaceTrackWrap> results; |
| 63 | int32_t ret; |
| 64 | ret = sessionGuard->FaceDetectAndTrack(process, results); |
| 65 | if (ret != 0) { |
| 66 | std::cerr << "FaceDetectAndTrack failed" << std::endl; |
| 67 | break; |
| 68 | } |
| 69 | if (results.size() == 0) { |
nothing calls this directly
no test coverage detected