File size: 3,453 Bytes
8be366a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | #include "json.h"
#include "adaboost.h"
char* readJson()
{
FILE* file = NULL;
file = fopen("training.json", "r");
if (file == NULL)
{
return NULL;
}
else
{
fseek(file, 0, SEEK_END);
long fsize = ftell(file);
fseek(file, 0, SEEK_SET);
char *string = malloc(fsize + 1);
fread(string, fsize, 1, file);
fclose(file);
string[fsize] = 0;
return string;
}
}
SCList* dbExtract()
{
char* str = readJson();
if (str == NULL)
{
printf("error: can not read training database\n");
// HERE IS A EXIT(0) JUST HERE !!! IN CASE OF BUG, REMOVE IT !!!
exit(0);
}
else
{
json_object *jobj = json_tokener_parse(str);
return jsonToStrongClassifierList(jobj);
}
}
SCList* jsonToStrongClassifierList(json_object* jStrongList)
{
int len = json_object_array_length(jStrongList);
SCList *old = NULL;
for (int i = len - 1; i >= 0; i--)
{
json_object *jStrongElt;
jStrongElt = json_object_array_get_idx(jStrongList, i);
SCList *strongList = malloc(sizeof(SCList));;
strongList->sc = jsonToStrongClassifier(jStrongElt);
strongList->next = old;
old = strongList;
}
return old;
}
StrongClassifier jsonToStrongClassifier(json_object *jStrongClass)
{
StrongClassifier strongClass;
json_object *jWeakL, *jTotalT, *jFalseNegative, *jWeakNumber;
json_object_object_get_ex(jStrongClass, "weak_classifier_list", &jWeakL);
json_object_object_get_ex(jStrongClass, "total_threshold", &jTotalT);
json_object_object_get_ex(jStrongClass, "false_negative", &jFalseNegative);
json_object_object_get_ex(jStrongClass, "weak_number", &jWeakNumber);
strongClass.weak = jsonToWeakClassifierList(jWeakL);
strongClass.totalThreshold = json_object_get_double(jTotalT);
strongClass.falseNegative = json_object_get_double(jFalseNegative);
strongClass.weakNumber = json_object_get_int(jWeakNumber);
return strongClass;
}
ClassifierList* jsonToWeakClassifierList(json_object *jWeakList)
{
int len = json_object_array_length(jWeakList);
ClassifierList *old = NULL;
for (int i = len - 1; i >= 0; i--)
{
json_object *jWeakElt, *jWeight, *jWeakClass;
jWeakElt = json_object_array_get_idx(jWeakList, i);
json_object_object_get_ex(jWeakElt, "weight", &jWeight);
json_object_object_get_ex(jWeakElt, "weak_classifier", &jWeakClass);
ClassifierList *weakList = malloc(sizeof(ClassifierList));;
weakList->weight = json_object_get_double(jWeight);
weakList->class = jsonToWeakClassifier(jWeakClass);
weakList->next = old;
old = weakList;
}
return old;
}
WeakClassifier jsonToWeakClassifier(json_object *jWeakClass)
{
WeakClassifier weakClass;
json_object *jXmin, *jYmin, *jXmax, *jYmax, *jType, *jParity, *jThreshold;
json_object_object_get_ex(jWeakClass, "xmin", &jXmin);
json_object_object_get_ex(jWeakClass, "ymin", &jYmin);
json_object_object_get_ex(jWeakClass, "xmax", &jXmax);
json_object_object_get_ex(jWeakClass, "ymax", &jYmax);
json_object_object_get_ex(jWeakClass, "type", &jType);
json_object_object_get_ex(jWeakClass, "parity", &jParity);
json_object_object_get_ex(jWeakClass, "threshold", &jThreshold);
weakClass.xmin = json_object_get_int(jXmin);
weakClass.ymin = json_object_get_int(jYmin);
weakClass.xmax = json_object_get_int(jXmax);
weakClass.ymax = json_object_get_int(jYmax);
weakClass.type = json_object_get_int(jType);
weakClass.parity = json_object_get_int(jParity);
weakClass.threshold = json_object_get_double(jThreshold);
return weakClass;
}
|