Skip to content
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

Implement error handling #75

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 109 additions & 32 deletions face_detection/retinaface/retinaface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ vector<FaceInfo> post_process(const vector<float>& box_data, const vector<float>
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;
Expand All @@ -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<FaceInfo> Detection(AILIANetwork *ailia, const unsigned char* camera, int tex_width, int tex_height, int channels) {
vector<FaceInfo> detection(AILIANetwork *ailia, std::vector<float> &work, const unsigned char* camera, int tex_width, int tex_height, int channels) {
vector<FaceInfo> detections;

// Prepare input data
std::vector<float> 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<float>(camera[idx + 2]) - 123.0f; //R
data[(y * tex_width + x) + 1 * tex_width * tex_height] = static_cast<float>(camera[idx + 1]) - 117.0f; //G
data[(y * tex_width + x) ] = static_cast<float>(camera[idx + 0]) - 104.0f; //B
work[(y * tex_width + x) + 2 * tex_width * tex_height] = static_cast<float>(camera[idx + 2]) - 123.0f; //R
work[(y * tex_width + x) + 1 * tex_width * tex_height] = static_cast<float>(camera[idx + 1]) - 117.0f; //G
work[(y * tex_width + x) ] = static_cast<float>(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;
Expand All @@ -508,12 +533,37 @@ vector<FaceInfo> Detection(AILIANetwork *ailia, const unsigned char* camera, in
unsigned int score_idx = 0;
unsigned int landmark_idx = 0;

ailiaGetBlobIndexByOutputIndex(ailia, &box_idx, 0);
ailiaGetBlobShape(ailia, &box_shape, box_idx, AILIA_SHAPE_VERSION);
ailiaGetBlobIndexByOutputIndex(ailia, &score_idx, 1);
ailiaGetBlobShape(ailia, &score_shape, score_idx, AILIA_SHAPE_VERSION);
ailiaGetBlobIndexByOutputIndex(ailia, &landmark_idx, 2);
ailiaGetBlobShape(ailia, &landmark_shape, landmark_idx, AILIA_SHAPE_VERSION);
status = ailiaGetBlobIndexByOutputIndex(ailia, &box_idx, 0);
if (status != AILIA_STATUS_SUCCESS){
PRINT_ERR("ailiaGetBlobIndexByOutputIndex failed %d\n", status);
return detections;
}
status = ailiaGetBlobIndexByOutputIndex(ailia, &score_idx, 1);
if (status != AILIA_STATUS_SUCCESS){
PRINT_ERR("ailiaGetBlobIndexByOutputIndex failed %d\n", status);
return detections;
}
status = ailiaGetBlobIndexByOutputIndex(ailia, &landmark_idx, 2);
if (status != AILIA_STATUS_SUCCESS){
PRINT_ERR("ailiaGetBlobIndexByOutputIndex failed %d\n", status);
return detections;
}

status = ailiaGetBlobShape(ailia, &box_shape, box_idx, AILIA_SHAPE_VERSION);
if (status != AILIA_STATUS_SUCCESS){
PRINT_ERR("ailiaGetBlobShape failed %d\n", status);
return detections;
}
status = ailiaGetBlobShape(ailia, &score_shape, score_idx, AILIA_SHAPE_VERSION);
if (status != AILIA_STATUS_SUCCESS){
PRINT_ERR("ailiaGetBlobShape failed %d\n", status);
return detections;
}
status = ailiaGetBlobShape(ailia, &landmark_shape, landmark_idx, AILIA_SHAPE_VERSION);
if (status != AILIA_STATUS_SUCCESS){
PRINT_ERR("ailiaGetBlobShape failed %d\n", status);
return detections;
}

vector<float> box_data(box_shape.x * box_shape.y * box_shape.z * box_shape.w);
vector<float> score_data(score_shape.x * score_shape.y * score_shape.z * score_shape.w);
Expand All @@ -523,12 +573,24 @@ vector<FaceInfo> Detection(AILIANetwork *ailia, const unsigned char* camera, in
//PRINT_OUT("score %d %d %d %d\n", score_shape.x, score_shape.y, score_shape.z, score_shape.w);
//PRINT_OUT("landmark %d %d %d %d\n", landmark_shape.x, landmark_shape.y, landmark_shape.z, landmark_shape.w);

ailiaGetBlobData(ailia, &box_data[0], box_data.size() * sizeof(float), box_idx);
ailiaGetBlobData(ailia, &score_data[0], score_data.size() * sizeof(float), score_idx);
ailiaGetBlobData(ailia, &landmark_data[0], landmark_data.size() * sizeof(float), landmark_idx);
status = ailiaGetBlobData(ailia, &box_data[0], box_data.size() * sizeof(float), box_idx);
if (status != AILIA_STATUS_SUCCESS){
PRINT_ERR("ailiaGetBlobData failed %d\n", status);
return detections;
}
status = ailiaGetBlobData(ailia, &score_data[0], score_data.size() * sizeof(float), score_idx);
if (status != AILIA_STATUS_SUCCESS){
PRINT_ERR("ailiaGetBlobData failed %d\n", status);
return detections;
}
status = ailiaGetBlobData(ailia, &landmark_data[0], landmark_data.size() * sizeof(float), landmark_idx);
if (status != AILIA_STATUS_SUCCESS){
PRINT_ERR("ailiaGetBlobData failed %d\n", status);
return detections;
}

// Post-processing
vector<FaceInfo> 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;
}
Expand All @@ -537,7 +599,7 @@ vector<FaceInfo> Detection(AILIANetwork *ailia, const unsigned char* camera, in
int plot_result_retinaface(std::vector<FaceInfo> 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++) {
Expand Down Expand Up @@ -573,36 +635,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<float> work;
vector<FaceInfo> 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);
Expand Down Expand Up @@ -635,7 +702,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<float> work;

while (1) {
cv::Mat frame;
Expand All @@ -648,11 +721,11 @@ static int recognize_from_video(AILIANetwork* ailia)
cv::cvtColor(resized_img, img, cv::COLOR_BGR2BGRA);

vector<FaceInfo> 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);
}
Expand Down Expand Up @@ -728,6 +801,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;
Expand Down
Loading