From b4e47e6b1d21039d22454d577c3ff88fd362b7d8 Mon Sep 17 00:00:00 2001 From: Kazuki Kyakuno Date: Fri, 1 Nov 2024 22:17:35 +0900 Subject: [PATCH] Implement error handling --- face_detection/retinaface/retinaface.cpp | 86 +++++++++++++++++------- 1 file changed, 63 insertions(+), 23 deletions(-) diff --git a/face_detection/retinaface/retinaface.cpp b/face_detection/retinaface/retinaface.cpp index c1c528d..0eb8124 100644 --- a/face_detection/retinaface/retinaface.cpp +++ b/face_detection/retinaface/retinaface.cpp @@ -466,7 +466,7 @@ vector post_process(const vector& box_data, const vector return results; } -void set_input_shape(AILIANetwork *ailia, int tex_width, int tex_height){ +int set_input_shape(AILIANetwork *ailia, int tex_width, int tex_height){ AILIAShape shape; shape.x = tex_width; shape.y = tex_height; @@ -475,30 +475,55 @@ void set_input_shape(AILIANetwork *ailia, int tex_width, int tex_height){ shape.dim = 4; unsigned int input_idx = 0; - ailiaGetBlobIndexByInputIndex(ailia, &input_idx, 0); + int status = ailiaGetBlobIndexByInputIndex(ailia, &input_idx, 0); + if (status != AILIA_STATUS_SUCCESS){ + PRINT_ERR("ailiaGetBlobIndexByInputIndex failed %d\n", status); + return status; + } + + status = ailiaSetInputBlobShape(ailia, &shape, input_idx, AILIA_SHAPE_VERSION); + if (status != AILIA_STATUS_SUCCESS){ + PRINT_ERR("ailiaSetInputBlobShape failed %d\n", status); + return status; + } - ailiaSetInputBlobShape(ailia, &shape, 0, AILIA_SHAPE_VERSION); + return AILIA_STATUS_SUCCESS; } -vector Detection(AILIANetwork *ailia, const unsigned char* camera, int tex_width, int tex_height, int channels) { +vector detection(AILIANetwork *ailia, std::vector &work, const unsigned char* camera, int tex_width, int tex_height, int channels) { + vector detections; + // Prepare input data - std::vector data(tex_width * tex_height * 3 * 1); + work.resize(tex_width * tex_height * 3 * 1); for (int y = 0; y < tex_height; y++) { for (int x = 0; x < tex_width; x++) { int idx = (y * tex_width + x) * channels; - data[(y * tex_width + x) + 2 * tex_width * tex_height] = static_cast(camera[idx + 2]) - 123.0f; //R - data[(y * tex_width + x) + 1 * tex_width * tex_height] = static_cast(camera[idx + 1]) - 117.0f; //G - data[(y * tex_width + x) ] = static_cast(camera[idx + 0]) - 104.0f; //B + work[(y * tex_width + x) + 2 * tex_width * tex_height] = static_cast(camera[idx + 2]) - 123.0f; //R + work[(y * tex_width + x) + 1 * tex_width * tex_height] = static_cast(camera[idx + 1]) - 117.0f; //G + work[(y * tex_width + x) ] = static_cast(camera[idx + 0]) - 104.0f; //B } } + // Inference unsigned int input_idx = 0; - ailiaGetBlobIndexByInputIndex(ailia, &input_idx, 0); + int status = ailiaGetBlobIndexByInputIndex(ailia, &input_idx, 0); + if (status != AILIA_STATUS_SUCCESS){ + PRINT_ERR("ailiaGetBlobIndexByInputIndex failed %d\n", status); + return detections; + } - ailiaSetInputBlobData(ailia, &data[0], data.size() * sizeof(float), input_idx); + status = ailiaSetInputBlobData(ailia, &work[0], work.size() * sizeof(float), input_idx); + if (status != AILIA_STATUS_SUCCESS){ + PRINT_ERR("ailiaSetInputBlobData failed %d\n", status); + return detections; + } - ailiaUpdate(ailia); + status = ailiaUpdate(ailia); + if (status != AILIA_STATUS_SUCCESS){ + PRINT_ERR("ailiaUpdate failed %d\n", status); + return detections; + } AILIAShape box_shape; AILIAShape score_shape; @@ -528,7 +553,7 @@ vector Detection(AILIANetwork *ailia, const unsigned char* camera, in ailiaGetBlobData(ailia, &landmark_data[0], landmark_data.size() * sizeof(float), landmark_idx); // Post-processing - vector detections = post_process(box_data, score_data, landmark_data, tex_width, tex_height); + detections = post_process(box_data, score_data, landmark_data, tex_width, tex_height); return detections; } @@ -537,7 +562,7 @@ vector Detection(AILIANetwork *ailia, const unsigned char* camera, in int plot_result_retinaface(std::vector info, cv::Mat& img, bool logging) { if (logging) { - PRINT_OUT("object_count=%d\n", info.size()); + PRINT_OUT("object_count=%d\n", (int)info.size()); } for (int i = 0; i < info.size(); i++) { @@ -573,36 +598,41 @@ static int recognize_from_image(AILIANetwork* ailia) cv::Mat img; int status = load_image(img, image_path.c_str()); if (status != AILIA_STATUS_SUCCESS) { - return -1; + return status; } PRINT_OUT("input image shape: (%d, %d, %d)\n", img.cols, img.rows, img.channels()); - set_input_shape(ailia, img.cols, img.rows); + status = set_input_shape(ailia, img.cols, img.rows); + if (status != AILIA_STATUS_SUCCESS) { + PRINT_ERR("set_input_shape failed %d\n", status); + return status; + } // inference PRINT_OUT("Start inference...\n"); + std::vector work; vector results; if (benchmark) { PRINT_OUT("BENCHMARK mode\n"); for (int i = 0; i < BENCHMARK_ITERS; i++) { clock_t start = clock(); - results = Detection(ailia, img.data, img.cols, img.rows, img.channels()); + results = detection(ailia, work, img.data, img.cols, img.rows, img.channels()); clock_t end = clock(); if (status != AILIA_STATUS_SUCCESS) { PRINT_ERR("ailiaDetectorCompute failed %d\n", status); - return -1; + return status; } PRINT_OUT("\tailia processing time %ld ms\n", ((end-start)*1000)/CLOCKS_PER_SEC); } } else { - results = Detection(ailia, img.data, img.cols, img.rows, img.channels()); + results = detection(ailia, work, img.data, img.cols, img.rows, img.channels()); } status = plot_result_retinaface(results, img, true); if (status != AILIA_STATUS_SUCCESS) { - return -1; + return status; } cv::imwrite(save_image_path.c_str(), img); @@ -635,7 +665,13 @@ static int recognize_from_video(AILIANetwork* ailia) } } - set_input_shape(ailia, IMAGE_WIDTH, IMAGE_HEIGHT); + int status = set_input_shape(ailia, IMAGE_WIDTH, IMAGE_HEIGHT); + if (status != AILIA_STATUS_SUCCESS) { + PRINT_ERR("set_input_shape failed %d\n", status); + return status; + } + + std::vector work; while (1) { cv::Mat frame; @@ -648,11 +684,11 @@ static int recognize_from_video(AILIANetwork* ailia) cv::cvtColor(resized_img, img, cv::COLOR_BGR2BGRA); vector results; - results = Detection(ailia, img.data, img.cols, img.rows, 4); + results = detection(ailia, work, img.data, img.cols, img.rows, 4); - int status = plot_result_retinaface(results, resized_img, false); + status = plot_result_retinaface(results, resized_img, false); if (status != AILIA_STATUS_SUCCESS) { - return -1; + return status; } cv::imshow("frame", resized_img); } @@ -728,6 +764,10 @@ int main(int argc, char **argv) status = recognize_from_image(ailia); } + if (status != AILIA_STATUS_SUCCESS) { + PRINT_ERR("recognize failed %d\n", status); + } + ailiaDestroy(ailia); return status;