refactor(knowing apps): yolov2 json parser
This commit is contained in:
@@ -4,9 +4,9 @@ menuconfig SUPPORT_KNOWING_FRAMEWORK
|
||||
|
||||
if SUPPORT_KNOWING_FRAMEWORK
|
||||
source "$APP_DIR/Framework/knowing/tensorflow-lite/Kconfig"
|
||||
source "$APP_DIR/Framework/knowing/kpu-postprocessing/Kconfig"
|
||||
source "$APP_DIR/Framework/knowing/filter/Kconfig"
|
||||
source "$APP_DIR/Framework/knowing/ota/Kconfig"
|
||||
source "$APP_DIR/Framework/knowing/image_processing/Kconfig"
|
||||
source "$APP_DIR/Framework/knowing/cmsis_5/Kconfig"
|
||||
source "$APP_DIR/Framework/knowing/kpu/Kconfig"
|
||||
endif
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
menuconfig USING_KPU_POSTPROCESSING
|
||||
bool "kpu model postprocessing"
|
||||
default y
|
||||
if USING_KPU_POSTPROCESSING
|
||||
source "$APP_DIR/Framework/knowing/kpu-postprocessing/yolov2/Kconfig"
|
||||
endif
|
||||
@@ -0,0 +1,7 @@
|
||||
menuconfig USING_KPU_PROCESSING
|
||||
bool "kpu model processing"
|
||||
default y
|
||||
if USING_KPU_PROCESSING
|
||||
source "$APP_DIR/Framework/knowing/kpu/yolov2/Kconfig"
|
||||
source "$APP_DIR/Framework/knowing/kpu/yolov2_json/Kconfig"
|
||||
endif
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
menuconfig USING_YOLOV2
|
||||
bool "yolov2 region layer"
|
||||
depends on USING_KPU_POSTPROCESSING
|
||||
depends on USING_KPU_PROCESSING
|
||||
default n
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
menuconfig USING_YOLOV2_JSONPARSER
|
||||
bool "yolov2 model json parser"
|
||||
depends on USING_KPU_PROCESSING
|
||||
default n
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
from building import *
|
||||
import os
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
|
||||
src = Glob('*.c')
|
||||
|
||||
group = DefineGroup('yolov2_json', src, depend = ['USING_YOLOV2_JSONPARSER', 'LIB_USING_CJSON'], CPPPATH = [cwd])
|
||||
|
||||
Return('group')
|
||||
@@ -0,0 +1,138 @@
|
||||
#include "json_parser.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
|
||||
yolov2_params_t param_parse(char *json_file_path)
|
||||
{
|
||||
yolov2_params_t params_return;
|
||||
int fin;
|
||||
char buffer[JSON_BUFFER_SIZE] = "";
|
||||
// char *buffer;
|
||||
// if (NULL != (buffer = (char*)malloc(JSON_BUFFER_SIZE * sizeof(char)))) {
|
||||
// memset(buffer, 0, JSON_BUFFER_SIZE * sizeof(char));
|
||||
// } else {
|
||||
// printf("Json buffer malloc failed!");
|
||||
// exit(-1);
|
||||
// }
|
||||
int array_size;
|
||||
cJSON *json_obj;
|
||||
cJSON *json_item;
|
||||
cJSON *json_array_item;
|
||||
|
||||
fin = open(json_file_path, O_RDONLY);
|
||||
if (!fin) {
|
||||
printf("Error open file %s", json_file_path);
|
||||
exit(-1);
|
||||
}
|
||||
read(fin, buffer, sizeof(buffer));
|
||||
close(fin);
|
||||
|
||||
// read json string
|
||||
json_obj = cJSON_Parse(buffer);
|
||||
// free(buffer);
|
||||
char *json_print_str = cJSON_Print(json_obj);
|
||||
printf("Json file content: \n%s\n", json_print_str);
|
||||
cJSON_free(json_print_str);
|
||||
// get anchors
|
||||
json_item = cJSON_GetObjectItem(json_obj, "anchors");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (ANCHOR_NUM * 2 != array_size) {
|
||||
printf("Expect anchor size: %d, got %d in json file", ANCHOR_NUM * 2, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d anchors from json file\n", ANCHOR_NUM);
|
||||
}
|
||||
for (int i = 0; i < ANCHOR_NUM * 2; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
params_return.anchor[i] = json_array_item->valuedouble;
|
||||
printf("%d: %f\n", i, params_return.anchor[i]);
|
||||
}
|
||||
// net_input_size
|
||||
json_item = cJSON_GetObjectItem(json_obj, "net_input_size");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (2 != array_size) {
|
||||
printf("Expect net_input_size: %d, got %d in json file", 2, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d net_input_size from json file\n", 2);
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
params_return.net_input_size[i] = json_array_item->valueint;
|
||||
printf("%d: %d\n", i, params_return.net_input_size[i]);
|
||||
}
|
||||
// net_output_shape
|
||||
json_item = cJSON_GetObjectItem(json_obj, "net_output_shape");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (3 != array_size) {
|
||||
printf("Expect net_output_shape: %d, got %d in json file", 3, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d net_output_shape from json file\n", 3);
|
||||
}
|
||||
for (int i = 0; i < 3; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
params_return.net_output_shape[i] = json_array_item->valueint;
|
||||
printf("%d: %d\n", i, params_return.net_output_shape[i]);
|
||||
}
|
||||
// sensor_output_size
|
||||
json_item = cJSON_GetObjectItem(json_obj, "sensor_output_size");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (2 != array_size) {
|
||||
printf("Expect sensor_output_size: %d, got %d in json file", 2, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d sensor_output_size from json file\n", 2);
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
params_return.sensor_output_size[i] = json_array_item->valueint;
|
||||
printf("%d: %d\n", i, params_return.sensor_output_size[i]);
|
||||
}
|
||||
// kmodel_path
|
||||
json_item = cJSON_GetObjectItem(json_obj, "kmodel_path");
|
||||
memcpy(params_return.kmodel_path, json_item->valuestring, strlen(json_item->valuestring));
|
||||
printf("Got kmodel_path: %s\n", params_return.kmodel_path);
|
||||
// kmodel_size
|
||||
json_item = cJSON_GetObjectItem(json_obj, "kmodel_size");
|
||||
params_return.kmodel_size = json_item->valueint;
|
||||
printf("Got kmodel_size: %d\n", params_return.kmodel_size);
|
||||
// labels
|
||||
json_item = cJSON_GetObjectItem(json_obj, "labels");
|
||||
params_return.class_num = cJSON_GetArraySize(json_item);
|
||||
if (0 >= params_return.class_num) {
|
||||
printf("No labels!");
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d labels\n", params_return.class_num);
|
||||
}
|
||||
for (int i = 0; i < params_return.class_num; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
memcpy(params_return.labels[i], json_array_item->valuestring, strlen(json_array_item->valuestring));
|
||||
printf("%d: %s\n", i, params_return.labels[i]);
|
||||
}
|
||||
// obj_thresh
|
||||
json_item = cJSON_GetObjectItem(json_obj, "obj_thresh");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (params_return.class_num != array_size) {
|
||||
printf("label number and thresh number mismatch! label number : %d, obj thresh number %d", params_return.class_num,
|
||||
array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d obj_thresh\n", array_size);
|
||||
}
|
||||
for (int i = 0; i < array_size; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
params_return.obj_thresh[i] = json_array_item->valuedouble;
|
||||
printf("%d: %f\n", i, params_return.obj_thresh[i]);
|
||||
}
|
||||
// nms_thresh
|
||||
json_item = cJSON_GetObjectItem(json_obj, "nms_thresh");
|
||||
params_return.nms_thresh = json_item->valuedouble;
|
||||
printf("Got nms_thresh: %f\n", params_return.nms_thresh);
|
||||
|
||||
cJSON_Delete(json_obj);
|
||||
return params_return;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef _JSON_PARSER_H_
|
||||
#define _JSON_PARSER_H_
|
||||
|
||||
#define ANCHOR_NUM 5
|
||||
#define JSON_BUFFER_SIZE (4 * 1024)
|
||||
|
||||
// params from json
|
||||
typedef struct {
|
||||
float anchor[ANCHOR_NUM * 2];
|
||||
int net_output_shape[3];
|
||||
int net_input_size[2];
|
||||
int sensor_output_size[2];
|
||||
char kmodel_path[127];
|
||||
int kmodel_size;
|
||||
float obj_thresh[20];
|
||||
float nms_thresh;
|
||||
char labels[20][32];
|
||||
int class_num;
|
||||
} yolov2_params_t;
|
||||
|
||||
yolov2_params_t param_parse(char *json_file_path);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user