|
|
//
|
|
|
// Created by alg002 on 8/22/24.
|
|
|
//
|
|
|
#include "demo_utils.h"
|
|
|
#include "gd_alg_type.h"
|
|
|
|
|
|
std::string replace_str(std::string str, const std::string& from, const std::string& to)
|
|
|
{
|
|
|
std::stringstream ss;
|
|
|
size_t start_pos = 0;
|
|
|
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
|
|
|
ss << str.substr(0, start_pos) << to;
|
|
|
start_pos += from.length();
|
|
|
str = str.substr(start_pos);
|
|
|
}
|
|
|
ss << str;
|
|
|
return ss.str();
|
|
|
}
|
|
|
|
|
|
std::vector <std::string> ImgsTxtReader(std::string imgTxtPath)
|
|
|
{
|
|
|
ifstream infile;
|
|
|
infile.open(imgTxtPath.data());
|
|
|
if (infile.is_open() == false) {
|
|
|
printf("Txt file: %s Load fail!!!\n", imgTxtPath.c_str());
|
|
|
}
|
|
|
vector <string> out;
|
|
|
string s;
|
|
|
|
|
|
while (getline(infile, s)) {
|
|
|
out.push_back(s);
|
|
|
//std::cout<< s << std::endl;
|
|
|
}
|
|
|
infile.close();
|
|
|
return out;
|
|
|
}
|
|
|
|
|
|
cv::Rect ExpandRect(cv::Rect rectin, float ratio, int imW,int imH)
|
|
|
{
|
|
|
int ox = rectin.x;
|
|
|
int oy = rectin.y;
|
|
|
int ow = rectin.width;
|
|
|
int oh = rectin.height;
|
|
|
cv::Rect rect = rectin;
|
|
|
|
|
|
rect.width = static_cast<int>(ow * ratio);
|
|
|
rect.height = static_cast<int>(oh * ratio);
|
|
|
rect.x = static_cast<int>(ox - (ratio-1.0)*ow/2);
|
|
|
rect.y = static_cast<int>(oy - (ratio-1.0)*oh/2);
|
|
|
|
|
|
if (0 <= rect.x && 0 <= rect.width && rect.x + rect.width <= imW && 0 <= rect.y && 0 <= rect.height && rect.y + rect.height <= imH)
|
|
|
{
|
|
|
return rect;
|
|
|
} else{
|
|
|
return rectin;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
bool UpdateTrackScoreDeque(std::deque<float> &sts, float score, int size, float sthd, float *avgScore)
|
|
|
{
|
|
|
int n = sts.size();
|
|
|
float sum_score = 0;
|
|
|
|
|
|
if (n == 0)
|
|
|
{
|
|
|
sts.push_back(score);
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
if (n >= size)
|
|
|
{
|
|
|
sts.pop_front();
|
|
|
sts.push_back(score);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
sts.push_back(score);
|
|
|
}
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
{
|
|
|
sum_score += sts.at(i);
|
|
|
}
|
|
|
float avg_score = (sum_score * 1.0) / sts.size();
|
|
|
*avgScore = avg_score;
|
|
|
//printf ("AvgTrackScore:%f sum_score:%f\n",avg_score, sum_score);
|
|
|
|
|
|
if (avg_score <= sthd)
|
|
|
return 0;
|
|
|
else
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
void resolve_labeled_json(const char*js_file, gud_rect_t1 *bbox)
|
|
|
{
|
|
|
/******************* OT_Json File Read *******************/
|
|
|
printf("***** jsonReader is started : {%s}*****\n",js_file);
|
|
|
FILE *fp;
|
|
|
cJSON *json;
|
|
|
fp = fopen(js_file, "rb");
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
long len = ftell(fp);
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
char *content = (char *) malloc(sizeof(char) * (len + 1));
|
|
|
printf("***** resolve_labeled_json okokokok*****\n");
|
|
|
fread(content, 1, len, fp);
|
|
|
fclose(fp);
|
|
|
|
|
|
/******************* OCR_Json File Read *******************/
|
|
|
json = cJSON_Parse(content);
|
|
|
|
|
|
if (json == NULL) {
|
|
|
std::cout << "cJSON is read failed! -->" << js_file << std::endl;
|
|
|
exit(1);
|
|
|
}
|
|
|
cJSON *json_x1,*json_y1,*json_x2,*json_y2;
|
|
|
json_x1 = cJSON_GetObjectItem(json, "x1");
|
|
|
json_y1 = cJSON_GetObjectItem(json, "y1");
|
|
|
json_x2 = cJSON_GetObjectItem(json, "x2");
|
|
|
json_y2 = cJSON_GetObjectItem(json, "y2");
|
|
|
|
|
|
bbox->ul.x = static_cast<int>(json_x1->valueint);
|
|
|
bbox->ul.y = static_cast<int>(json_y1->valueint);
|
|
|
bbox->lr.x = static_cast<int>(json_x2->valueint);
|
|
|
bbox->lr.y = static_cast<int>(json_y2->valueint);
|
|
|
printf("[%d %d %d %d]\n",bbox->ul.x,bbox->ul.y,bbox->lr.x - bbox->ul.x ,bbox->lr.y - bbox->ul.y);
|
|
|
//exit(-1);
|
|
|
}
|
|
|
|
|
|
void ot_v2_jsonReader(const char *js_path, gud_nano_config_t *ot_config)
|
|
|
{
|
|
|
/******************* OT_Json File Read *******************/
|
|
|
FILE *fp;
|
|
|
cJSON *json;
|
|
|
fp = fopen(js_path, "rb");
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
long len = ftell(fp);
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
char *content = (char *) malloc(sizeof(char) * (len + 1));
|
|
|
fread(content, 1, len, fp);
|
|
|
fclose(fp);
|
|
|
|
|
|
/******************* OCR_Json File Read *******************/
|
|
|
json = cJSON_Parse(content);
|
|
|
|
|
|
if (json == NULL) {
|
|
|
std::cout << "cJSON is read failed! -->" << js_path << std::endl;
|
|
|
exit(1);
|
|
|
}
|
|
|
|
|
|
cJSON *json_lr, *json_ac,*json_use_reid, *json_reid_frq,*json_use_update,*json_update_frq;
|
|
|
cJSON *json_use_trace,*json_trace_len,*json_device_type;
|
|
|
cJSON *json_model0, *json_model1, *json_model2, *json_reid;
|
|
|
//printf("***** 2jsonRead okokokok*****\n");
|
|
|
json_lr = cJSON_GetObjectItem(json, "lr_rate");
|
|
|
json_ac = cJSON_GetObjectItem(json, "ac_score");
|
|
|
json_device_type = cJSON_GetObjectItem(json, "device_type");
|
|
|
|
|
|
json_use_reid = cJSON_GetObjectItem(json, "use_reid");
|
|
|
json_reid_frq = cJSON_GetObjectItem(json, "reid_frq");
|
|
|
json_use_trace = cJSON_GetObjectItem(json, "use_trace");
|
|
|
json_trace_len = cJSON_GetObjectItem(json, "trace_len");
|
|
|
json_use_update = cJSON_GetObjectItem(json, "use_update");
|
|
|
json_update_frq = cJSON_GetObjectItem(json, "update_frq");
|
|
|
|
|
|
json_model0 = cJSON_GetObjectItem(json, "Model_temp");
|
|
|
json_model1 = cJSON_GetObjectItem(json, "Model_search");
|
|
|
json_model2 = cJSON_GetObjectItem(json, "Model_head");
|
|
|
json_reid = cJSON_GetObjectItem(json, "reid_json");
|
|
|
|
|
|
ot_config->lr_rate = json_lr->valuedouble;
|
|
|
ot_config->ac_score = json_ac->valuedouble;
|
|
|
ot_config->device_type = json_device_type->valueint;
|
|
|
ot_config->use_reid = json_use_reid->valueint;
|
|
|
ot_config->reid_frq = json_reid_frq->valueint;
|
|
|
ot_config->use_trace = json_use_trace->valueint;
|
|
|
ot_config->trace_len = json_trace_len->valueint;
|
|
|
ot_config->use_update = json_use_update->valueint;
|
|
|
ot_config->update_frq = json_update_frq->valueint;
|
|
|
|
|
|
ot_config->Model_temp = json_model0->valuestring;
|
|
|
ot_config->Model_search = json_model1->valuestring;
|
|
|
ot_config->Model_head = json_model2->valuestring;
|
|
|
ot_config->reid_json = json_reid->valuestring;
|
|
|
//ot_config->update_json = json_updatenet->valuestring;
|
|
|
#if 1
|
|
|
printf("***** GUD_AI_OT_V2_jsonReader start *****\n");
|
|
|
printf ("ac_score:(%.2f) lr:(%.2f) device_type:(%d)\n)",ot_config->ac_score,ot_config->lr_rate,ot_config->device_type);
|
|
|
printf ("use_reid:(%d) reid_frq:(%d) use_trace:(%d) traceLen:(%d)\n",ot_config->use_reid,ot_config->reid_frq,ot_config->use_trace,ot_config->trace_len);
|
|
|
printf ("use_update:(%d) update_frq:(%d) \n",ot_config->use_update,ot_config->update_frq);
|
|
|
printf("model0: %s\n",ot_config->Model_temp);
|
|
|
printf("model1: %s\n",ot_config->Model_search);
|
|
|
printf("model2: %s\n",ot_config->Model_head);
|
|
|
printf("***** GUD_AI_OT_V2_jsonReader End *****\n");
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
void cxy_wh_2_rect(const cv::Point2f& pos, const cv::Point2f& sz, cv::Rect &rect)
|
|
|
{
|
|
|
rect.x = max(0, (int)pos.x - int(sz.x / 2));
|
|
|
rect.y = max(0, (int)pos.y - int(sz.y / 2));
|
|
|
rect.width = int(sz.x);
|
|
|
rect.height = int(sz.y);
|
|
|
}
|
|
|
|
|
|
|
|
|
int resolve_mulit_labeled_json(const char*js_file, gud_rect_t1 *bbox)
|
|
|
{
|
|
|
/******************* OT_Json File Read *******************/
|
|
|
FILE *fp;
|
|
|
//cJSON *json;
|
|
|
fp = fopen(js_file, "rb");
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
long len = ftell(fp);
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
char *content = (char *) malloc(sizeof(char) * (len + 1));
|
|
|
fread(content, 1, len, fp);
|
|
|
fclose(fp);
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>JSON<4F>ַ<EFBFBD><D6B7><EFBFBD>
|
|
|
cJSON *json = cJSON_Parse(content);
|
|
|
if (json == NULL) {
|
|
|
const char *error_ptr = cJSON_GetErrorPtr();
|
|
|
if (error_ptr != NULL) {
|
|
|
fprintf(stderr, "Error before: %s\n", error_ptr);
|
|
|
}
|
|
|
cJSON_Delete(json);
|
|
|
|
|
|
}
|
|
|
|
|
|
// <20><>ȡ"imagePath"
|
|
|
cJSON *image_path = cJSON_GetObjectItemCaseSensitive(json, "imagePath");
|
|
|
if (cJSON_IsString(image_path) && (image_path->valuestring != NULL)) {
|
|
|
printf("Image Path: %s\n", image_path->valuestring);
|
|
|
}
|
|
|
int tagNum = 0;
|
|
|
float tagPonits[4];
|
|
|
// <20><>ȡ"shapes"<22><><EFBFBD><EFBFBD>
|
|
|
|
|
|
cJSON *shapes = cJSON_GetObjectItemCaseSensitive(json, "shapes");
|
|
|
if (cJSON_IsArray(shapes)) {
|
|
|
int shapes_count = cJSON_GetArraySize(shapes);
|
|
|
tagNum = shapes_count;
|
|
|
for (int i = 0; i < shapes_count; ++i) {
|
|
|
cJSON *shape = cJSON_GetArrayItem(shapes, i);
|
|
|
if (cJSON_IsObject(shape)) {
|
|
|
cJSON *rects = cJSON_GetObjectItemCaseSensitive(shape, "points");
|
|
|
int idx = 0;
|
|
|
int val_count = cJSON_GetArraySize(rects);
|
|
|
for (int j = 0; j < val_count; ++j) {
|
|
|
cJSON *point = cJSON_GetArrayItem(rects, j);
|
|
|
int pnum = cJSON_GetArraySize(point);
|
|
|
for (int k = 0; k < pnum; ++k) {
|
|
|
cJSON *pointVal = cJSON_GetArrayItem(point, k);
|
|
|
//printf ("%f\n",pointVal->valuedouble);
|
|
|
tagPonits[idx] = pointVal->valuedouble;
|
|
|
idx++;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
bbox[i].ul.x = (int)(round(tagPonits[0]));
|
|
|
bbox[i].ul.y = (int)(round(tagPonits[1]));
|
|
|
bbox[i].lr.x = (int)(round(tagPonits[2]));
|
|
|
bbox[i].lr.y = (int)(round(tagPonits[3]));
|
|
|
printf ("Tag:[%d] = {%d %d %d %d}\n",i, bbox[i].ul.x, bbox[i].ul.y, bbox[i].lr.x, bbox[i].lr.y );
|
|
|
memset(tagPonits,0, sizeof(float)*4);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// <20>ͷ<EFBFBD>cJSON<4F>ṹ
|
|
|
cJSON_Delete(json);
|
|
|
|
|
|
return tagNum;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
int DirAndMkdir(const char* pathname)
|
|
|
{
|
|
|
int ret=0;
|
|
|
DIR * mydir =NULL;
|
|
|
mydir=opendir(pathname); //<2F><><EFBFBD><EFBFBD>Ŀ¼
|
|
|
if(mydir==NULL)
|
|
|
{
|
|
|
ret = mkdir(pathname,0777); //<2F><><EFBFBD><EFBFBD>Ŀ¼
|
|
|
printf("mkdir pathname:{%s}\n",pathname);
|
|
|
if(ret!=0)
|
|
|
{
|
|
|
return -1;
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
printf("--dir exist\n");
|
|
|
ret = 1;
|
|
|
return ret;
|
|
|
}
|
|
|
closedir(mydir);//<2F>ر<EFBFBD>Ŀ¼
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
|
|
|
void Stringsplit(string str, const char split,vector<string>& res)
|
|
|
{
|
|
|
istringstream iss(str);
|
|
|
string token;
|
|
|
while (getline(iss, token, split))
|
|
|
{
|
|
|
res.push_back(token);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void stripWhiteSpace (string &str)
|
|
|
{
|
|
|
if (str == "")
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
string::iterator cur_it;
|
|
|
cur_it = str.begin();
|
|
|
|
|
|
while (cur_it != str.end())
|
|
|
{
|
|
|
if (((*cur_it) != '\t') && ((*cur_it) != ' '))
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
cur_it = str.erase (cur_it);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
cur_it = str.begin();
|
|
|
|
|
|
while (cur_it != str.end())
|
|
|
{
|
|
|
if ((*cur_it) == '\n')
|
|
|
{
|
|
|
cur_it = str.erase (cur_it);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
cur_it++;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
int CV_RectIOU(cv::Rect re1, cv::Rect re2)
|
|
|
{
|
|
|
int iou = 0;
|
|
|
int re1_ul_x = re1.x;
|
|
|
int re1_ul_y = re1.y;
|
|
|
int re1_lr_x = re1.x + re1.width;
|
|
|
int re1_lr_y = re1.y + re1.height;
|
|
|
|
|
|
int re2_ul_x = re2.x;
|
|
|
int re2_ul_y = re2.y;
|
|
|
int re2_lr_x = re2.x + re2.width;
|
|
|
int re2_lr_y = re2.y + re2.height;
|
|
|
|
|
|
|
|
|
int max_left = MAX(re1_ul_x, re2_ul_x);
|
|
|
int min_right = MIN(re1_lr_x, re2_lr_x);
|
|
|
int max_up = MAX(re1_ul_y, re2_ul_y);
|
|
|
int min_down = MIN(re1_lr_y, re2_lr_y);
|
|
|
|
|
|
if (min_right - max_left < 0 || min_down - max_up < 0)
|
|
|
{
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
else
|
|
|
{
|
|
|
int overflow_area = (min_right - max_left) * (min_down - max_up);
|
|
|
int re1_area = (re1_lr_x - re1_ul_x) * (re1_lr_y - re1_ul_y);
|
|
|
int re2_area = (re2_lr_x - re2_ul_x) * (re2_lr_y - re2_ul_y);
|
|
|
if (overflow_area!=0 and re1_area!=0){
|
|
|
iou = static_cast<int>(overflow_area * 100 / (re1_area+re2_area));
|
|
|
return iou;
|
|
|
}
|
|
|
else
|
|
|
return 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
void swapYUV_I420toNV12(unsigned char* i420bytes, unsigned char* nv12bytes, int width, int height)
|
|
|
{
|
|
|
int nLenY = width * height;
|
|
|
int nLenU = nLenY / 4;
|
|
|
|
|
|
memcpy(nv12bytes, i420bytes, width * height);
|
|
|
|
|
|
for (int i = 0; i < nLenU; i++)
|
|
|
{
|
|
|
nv12bytes[nLenY + 2 * i] = i420bytes[nLenY + i]; // U
|
|
|
nv12bytes[nLenY + 2 * i + 1] = i420bytes[nLenY + nLenU + i]; // V
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void BGR2YUV_nv12(cv::Mat src, cv::Mat &dst)
|
|
|
{
|
|
|
int w_img = src.cols;
|
|
|
int h_img = src.rows;
|
|
|
dst = cv::Mat(h_img*1.5, w_img, CV_8UC1, cv::Scalar(0));
|
|
|
cv::Mat src_YUV_I420(h_img*1.5, w_img, CV_8UC1, cv::Scalar(0)); //YUV_I420
|
|
|
cvtColor(src, src_YUV_I420, COLOR_BGR2YUV_I420);
|
|
|
swapYUV_I420toNV12(src_YUV_I420.data, dst.data, w_img, h_img);
|
|
|
}
|
|
|
|
|
|
std::vector<int> get_color(int cls){
|
|
|
cls += 1;
|
|
|
int stride = 200;
|
|
|
std::vector<int> clr = {0,0,0};
|
|
|
for(int k=0; k<cls; k++){
|
|
|
int i = k % 3;
|
|
|
clr[i] += stride;
|
|
|
}
|
|
|
for(int k = 0; k< 3; k++){
|
|
|
clr[k] = clr[k] %255;
|
|
|
}
|
|
|
return clr;
|
|
|
}
|