Fixed some klockwork issues in C API samples (#4773)

This commit is contained in:
Ilya Lavrenov 2021-03-16 11:00:59 +03:00 committed by GitHub
parent b934851e9d
commit 7a533f7abf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 12 deletions

View File

@ -31,8 +31,13 @@ int image_read(const char *img_path, c_mat_t *img) {
img->mat_width = mat.size().width;
img->mat_height = mat.size().height;
img->mat_type = mat.type();
img->mat_data_size = img->mat_channels * img->mat_width * img->mat_height;
img->mat_data_size = mat.elemSize() * img->mat_width * img->mat_height;
img->mat_data = (unsigned char *)malloc(sizeof(unsigned char) * img->mat_data_size);
if (img->mat_data == NULL) {
return -1;
}
for (int i = 0; i < img->mat_data_size; ++i) {
img->mat_data[i] = mat.data[i];
}
@ -54,8 +59,13 @@ int image_resize(const c_mat_t *src_img, c_mat_t *dst_img, const int width, cons
dst_img->mat_width = mat_dst.size().width;
dst_img->mat_height = mat_dst.size().height;
dst_img->mat_type = mat_dst.type();
dst_img->mat_data_size = dst_img->mat_channels * dst_img->mat_width * dst_img->mat_height;
dst_img->mat_data_size = mat_dst.elemSize() * dst_img->mat_width * dst_img->mat_height;
dst_img->mat_data = (unsigned char *)malloc(sizeof(unsigned char) * dst_img->mat_data_size);
if (dst_img->mat_data == NULL) {
return -1;
}
for (int i = 0; i < dst_img->mat_data_size; ++i) {
dst_img->mat_data[i] = mat_dst.data[i];
}

View File

@ -39,6 +39,9 @@ struct classify_res *output_blob_to_classify_res(ie_blob_t *blob, size_t *n) {
*n = output_dim.dims[1];
struct classify_res *cls = (struct classify_res *)malloc(sizeof(struct classify_res) * (*n));
if (!cls) {
return NULL;
}
ie_blob_buffer_t blob_cbuffer;
status = ie_blob_get_cbuffer(blob, &blob_cbuffer);

View File

@ -38,6 +38,9 @@ struct classify_res *output_blob_to_classify_res(ie_blob_t *blob, size_t *n) {
*n = output_dim.dims[1];
struct classify_res *cls = (struct classify_res *)malloc(sizeof(struct classify_res) * (*n));
if (!cls) {
return NULL;
}
ie_blob_buffer_t blob_cbuffer;
status = ie_blob_get_cbuffer(blob, &blob_cbuffer);
@ -76,8 +79,8 @@ size_t read_image_from_file(const char *img_path, unsigned char *img_data, size_
fseek(fp, 0, SEEK_SET);
read_size = fread(img_data, 1, size, fp);
}
fclose(fp);
}
fclose(fp);
return read_size;
}

View File

@ -122,6 +122,7 @@ void readInputFilesArgument(const char *arg) {
for (i = 0; i < file_num; ++i) {
free(file_paths[i]);
}
free(file_path);
free(file_paths);
file_num = 0;
}
@ -279,6 +280,10 @@ int main(int argc, char **argv) {
ie_version_free(&version);
char **argv_temp =(char **)calloc(argc, sizeof(char *));
if (!argv_temp) {
return EXIT_FAILURE;
}
int i, j;
for (i = 0; i < argc; ++i) {
argv_temp[i] = argv[i];
@ -419,6 +424,10 @@ int main(int argc, char **argv) {
/** Collect images data **/
c_mat_t *originalImages = (c_mat_t *)calloc(file_num, sizeof(c_mat_t));
c_mat_t *images = (c_mat_t *)calloc(file_num, sizeof(c_mat_t));
if (!originalImages || !images)
goto err;
int image_num = 0;
for (i = 0; i < file_num; ++i) {
c_mat_t img = {NULL, 0, 0, 0, 0, 0};
@ -435,20 +444,27 @@ int main(int argc, char **argv) {
resized_img.mat_height = img.mat_height;
resized_img.mat_type = img.mat_type;
resized_img.mat_data = calloc(1, resized_img.mat_data_size);
if (resized_img.mat_data == NULL) {
image_free(&img);
continue;
}
for (j = 0; j < resized_img.mat_data_size; ++j)
resized_img.mat_data[j] = img.mat_data[j];
} else {
printf("%sImage is resized from (%d, %d) to (%zu, %zu)\n", \
warn, img.mat_width, img.mat_height, input_width, input_height);
warn, img.mat_width, img.mat_height, input_width, input_height);
image_resize(&img, &resized_img, (int)input_width, (int)input_height);
if (image_resize(&img, &resized_img, (int)input_width, (int)input_height) == -1) {
printf("%sImage %s cannot be resized!\n", warn, file_paths[i]);
image_free(&img);
continue;
}
}
if (resized_img.mat_data) {
originalImages[image_num] = img;
images[image_num] = resized_img;
++image_num;
}
originalImages[image_num] = img;
images[image_num] = resized_img;
++image_num;
}
if (!image_num) {
@ -523,8 +539,8 @@ int main(int argc, char **argv) {
if (config_msg) {
ie_config_t * config = parseConfig(config_msg, '#');
status = ie_core_load_network(core, network, device_name, config, &exe_network);
config_free(config);
if (status != OK) {
config_free(config);
goto err;
}
} else {

View File

@ -53,8 +53,8 @@ size_t read_image_from_file(const char* img_path, unsigned char *img_data, size_
fseek(fp, 0, SEEK_SET);
read_size = fread(img_data, 1, size, fp);
}
fclose(fp);
}
fclose(fp);
return read_size;
}