#include #include #include #include #include #include "shmparser/shmparser.h" static char* timestamp = "2023-1-31 10:05:56.998"; static char* cid = "cameraid"; static char* cname = "cameraname"; static int printlog = 1; static int savepic = 0; static struct stimg* pack_image(const int w, const int h, const unsigned char* data, const uint64_t size) { int width = w, height = h; uint64_t id = 1223997600787778; return make_image_ref(id, data, size, width, height, timestamp, strlen(timestamp), cid, strlen(cid), cname, strlen(cname)); } static void print_image(void* shm, const char* file){ struct stimg* img = shm2image(shm); if (printlog){ char fmt[128] = {0}; sprintf(fmt, "imag2 id [%%lu] data size [%%lu] width [%%d] height [%%d] " "time [%%%d.%ds] cid [%%%d.%ds] cname [%%%d.%ds]", img->timestamp_size, img->timestamp_size, img->camera_id_size, img->camera_id_size, img->camera_name_size, img->camera_name_size); printf(fmt, img->id, img->data_size, img->width, img->height, img->timestamp, img->camera_id, img->camera_name); printf("\n"); } if (savepic){ FILE* fp = fopen(file, "wb"); long pos = 0; while (pos != img->data_size) { pos += fwrite(img->data+pos, 1, img->data_size-pos, fp); } fclose(fp); } free_stimg(img); } static void readfile(const char* file, unsigned char** data, size_t* size){ FILE* fp = fopen(file, "rb"); if (!fp) { printf("file %s error %s\n", file, strerror(errno)); return; } fseek(fp, 0, SEEK_END); *size = ftell(fp); fseek(fp, 0, SEEK_SET); unsigned char* d = (unsigned char*)malloc(*size); long s = *size; long pos = 0; while (pos != *size) { pos += fread(d+pos, 1, s-pos, fp); } *data = d; fclose(fp); } static void test_image(void* shm, const int order){ const char* pics[] = { "../opic/720x576.jpg", "../opic/720x576.bgr", "../opic/1920x1080.jpg", "../opic/1920x1080.bgr", }; unsigned char* d = NULL; size_t s = 0; if (order == 0) for(int i = 0; i < sizeof(pics)/sizeof(pics[0]); i++){ readfile(pics[i], &d, &s); int w = 0, h = 0; if (i < 2){ w = 720, h = 576; }else{ w = 1920, h = 1080; } struct stimg* img = pack_image(w, h, d, s); image2shm(shm, img); free_stimg(img); free(d); char file[128] = {0}; sprintf(file, "../npic/%dx%d.%d", w, h, i); print_image(shm, file); } else for(int i = sizeof(pics)/sizeof(pics[0]) - 1; i >= 0; i--){ readfile(pics[i], &d, &s); int w = 0, h = 0; if (i < 2){ w = 720, h = 576; }else{ w = 1920, h = 1080; } struct stimg* img = pack_image(w, h, d, s); image2shm(shm, img); free_stimg(img); free(d); char file[128] = {0}; sprintf(file, "../npic/%dx%d.%d", w, h, i); print_image(shm, file); } } static void test_rule(void* shm); static int test_once(void* shm, const int count){ struct timeval s, e; gettimeofday(&s, NULL); for(int i = 0; i < count; i++) test_rule(shm); for(int i = 0; i < count; i++){ test_image(shm, 0); // test_image(shm, 1); } for(int i = 0; i < count; i++){ // test_image(shm, 0); test_image(shm, 1); } for(int i = 0; i < count; i++){ test_image(shm, 0); test_image(shm, 1); } for(int i = 0; i < count; i++){ test_image(shm, 1); test_image(shm, 0); } for(int i = 0; i < count; i++) test_rule(shm); gettimeofday(&e, NULL); return (e.tv_sec- s.tv_sec)*1000 + (e.tv_usec - s.tv_usec)/1000; } int main(int argc, char const *argv[]) { char* shm = (char*)malloc(10*1024*1024); int cntPerTest = 1000; int cntRun = 1; int avg = 0; for(int i = 1; i <= cntRun; i++){ int tm = test_once(shm, cntPerTest); if (avg == 0) avg = tm; else avg = (double)avg / (double) i * (double)(i-1) + (double)tm / (double)i; } printf("run %d time test, each %d use %d ms\n", cntPerTest*cntRun, cntPerTest, avg); return 0; } static Target* pack_target(const int size){ Target* tgts = (Target*)calloc(size, sizeof(Target)); for(int i = 0; i < size; i++){ tgts[i].id = i + 1002; tgts[i].confidence = 99-i; TRect rc = {0,0,1920,1080}; memcpy(&tgts[i].rect, &rc, sizeof(TRect)); memcpy(tgts[i].type, "pack_target", 11); tgts[i].feature_size = 123; tgts[i].feature = (char*)calloc(tgts[i].feature_size, 1); memcpy(tgts[i].feature, "feature and feature_size", 24); tgts[i].attribute_size = 235; tgts[i].attribute = (char*)calloc(tgts[i].attribute_size, 1); memcpy(tgts[i].attribute, "attribute and attribute_size", 28); } return tgts; } static void print_targets(struct sttgt* tgts, const int count){ for(int i = 0; i < count; i++){ struct sttgt* tgt = &tgts[i]; printf("\t\t [%d] id [%lu] confidence [%d] rect [%dx%dx%dx%d] type [%s] \ feature_size [%d] feature [%s] attribute_size [%d] attribute [%s]\n", i, tgt->id, tgt->confidence, tgt->rect.left, tgt->rect.top, tgt->rect.right, tgt->rect.bottom, tgt->type, tgt->feature_size, tgt->feature, tgt->attribute_size, tgt->attribute); } } const char* sdktype[] = { "face", "yolo", "bike", "fire", "car" }; static void pack_result(void* shm, const int s, const int e){ int sdkcnt = e - s; TResult* resarray = (TResult*)calloc(sdkcnt, sizeof(TResult)); for(size_t i = 0; i < sdkcnt; i++){ resarray[i].count = 2+i; resarray[i].targets = pack_target(resarray[i].count); } for(size_t i = 0; i < sdkcnt; i++){ add_result2rule_in_shm(shm, &resarray[i], sdktype[s+i], strlen(sdktype[i+s]), timestamp, strlen(timestamp)); } for(size_t i = 0; i < sdkcnt; i++){ for(int j = 0; j < resarray[i].count; j++){ free(resarray[i].targets[j].feature); free(resarray[i].targets[j].attribute); } free(resarray[i].targets); } free(resarray); } static char* handletrack = "camera1"; static void pack_rule(void* shm){ rule2shm(shm, handletrack, strlen(handletrack)); pack_result(shm, 0, 3); } static void print_rule(void* shm){ struct strule* rule = shm2rule(shm); if (printlog) printf(" handletrack [%s] datatype [%s]\n", rule->handletrack, rule->datatype); for(int i = 0; i < rule->sdk_count; i++){ struct stsdk* sdk = &rule->sdk[i]; if (printlog){ printf("\t [%d] type [%s] tgts [%d] id [%s] name [%s] timestamp [%s]\n", i, sdk->sdktype, sdk->tgt_count, sdk->sdkid, sdk->sdkname, sdk->timestamp); print_targets(sdk->tgt, sdk->tgt_count); } } free_strule(rule); } static void test_rule(void* shm){ pack_rule(shm); pack_result(shm, 2, 5); pack_result(shm, 0, 5); // 测试时发现如果取 6 造成 rule 中的 handletrack 字符串的长度溢出 // 取 5 时大约有 35 个 sdk 运行,目前一张图片不会运行 35 个 sdk // 暂时保留 handletrack 的 256 个字符限制 // for(int i = 0; i < 6; i++){ for(int i = 0; i < 5; i++){ pack_result(shm, 0, 5); } print_rule(shm); }