void cos_init(void) { ccv_disable_cache(); cbufp_t cbid; char *buf; int mat_size; long evt_tsplit; td_t td; ccv_dense_matrix_t *ccv_mat_input = NULL; ccv_dense_matrix_t *ccv_mat_output = NULL; ccv_read("photo.bmp", &ccv_mat_input, CCV_IO_ANY_FILE | CCV_IO_GRAY); assert(ccv_mat_input); evt_tsplit = evt_split(cos_spd_id(), 0, 0); td = ccv_fd_tsplit(cos_spd_id(), td_root, "face", strlen("face"), TOR_ALL, evt_tsplit); cbuf_matrix_t *cbuf_mat = ccv2cbufmat(ccv_mat_input); cbufp_send_deref(cbuf_mat->cbid); ccv_fd_twritep(cos_spd_id(), td, cbuf_mat->cbid, cbuf_mat->size); /* test of treapd, should same with the input*/ int off, len; cbid = ccv_fd_treadp(cos_spd_id(), td, &off, &len); buf = cbufp2buf(cbid, len); cbuf_mat = (cbuf_matrix_t *)buf; ccv_mat_output = cbuf2ccvmat(cbuf_mat); return; }
int main(int argc, char** argv) { printf("Edge Detection Benchmark ...\n"); ccv_enable_default_cache(); unsigned int elapsed_time; ccv_dense_matrix_t* yuv = 0; ccv_read(argv[1], &yuv, CCV_IO_GRAY | CCV_IO_ANY_FILE); /* ORIGIN */ ccv_dense_matrix_t* canny = 0; elapsed_time = get_current_time(); ccv_canny(yuv, &canny, 0, 3, 175, 320); elapsed_time = get_current_time() - elapsed_time; printf("origin: %ums\n", elapsed_time); ccv_matrix_free(canny); /* SLICE & DETECT */ int X_SLICE = atoi(argv[2]), Y_SLICE = atoi(argv[3]); int i, count = X_SLICE * Y_SLICE; int slice_rows = yuv->rows / Y_SLICE; int slice_cols = yuv->cols / X_SLICE; ccv_dense_matrix_t* canny_arr[count]; elapsed_time = get_current_time(); #pragma omp parallel for for (i = 0; i < count; i++) { int y = i / X_SLICE; int x = i - X_SLICE * y; ccv_dense_matrix_t* slice = 0; ccv_slice(yuv, (ccv_matrix_t**)&slice, 0, slice_rows * y, slice_cols * x, slice_rows, slice_cols); #ifdef DEBUG cos_ccv_slice_output(slice, y, x); #endif canny_arr[i] = 0; ccv_canny(slice, &canny_arr[i], 0, 3, 175, 320); } elapsed_time = get_current_time() - elapsed_time; printf("slice & detect: %ums\n", elapsed_time); unsigned int slice_time = elapsed_time; /* save to compute total time */ /* MERGE */ ccv_dense_matrix_t* final_output = 0; elapsed_time = get_current_time(); cos_ccv_merge(canny_arr, &final_output, yuv->rows, yuv->cols, X_SLICE, Y_SLICE); elapsed_time = get_current_time() - elapsed_time; printf("merge: %ums\n", elapsed_time); ccv_matrix_free(final_output); printf("parallel total: %ums\n", slice_time + elapsed_time); ccv_matrix_free(yuv); ccv_disable_cache(); return 0; }
int main(int argc, char** argv) { assert(argc >= 3); int i; ccv_enable_default_cache(); ccv_dense_matrix_t* image = 0; ccv_icf_classifier_cascade_t* cascade = ccv_icf_read_classifier_cascade(argv[2]); ccv_read(argv[1], &image, CCV_IO_ANY_FILE | CCV_IO_RGB_COLOR); if (image != 0) { unsigned int elapsed_time = get_current_time(); ccv_array_t* seq = ccv_icf_detect_objects(image, &cascade, 1, ccv_icf_default_params); elapsed_time = get_current_time() - elapsed_time; for (i = 0; i < seq->rnum; i++) { ccv_comp_t* comp = (ccv_comp_t*)ccv_array_get(seq, i); printf("%d %d %d %d %f\n", comp->rect.x, comp->rect.y, comp->rect.width, comp->rect.height, comp->confidence); } printf("total : %d in time %dms\n", seq->rnum, elapsed_time); ccv_array_free(seq); ccv_matrix_free(image); } else { FILE* r = fopen(argv[1], "rt"); if (argc == 4) chdir(argv[3]); if(r) { size_t len = 1024; char* file = (char*)malloc(len); ssize_t read; while((read = getline(&file, &len, r)) != -1) { while(read > 1 && isspace(file[read - 1])) read--; file[read] = 0; image = 0; ccv_read(file, &image, CCV_IO_ANY_FILE | CCV_IO_RGB_COLOR); assert(image != 0); ccv_array_t* seq = ccv_icf_detect_objects(image, &cascade, 1, ccv_icf_default_params); for (i = 0; i < seq->rnum; i++) { ccv_comp_t* comp = (ccv_comp_t*)ccv_array_get(seq, i); printf("%s %d %d %d %d %f\n", file, comp->rect.x, comp->rect.y, comp->rect.width, comp->rect.height, comp->confidence); } ccv_array_free(seq); ccv_matrix_free(image); } free(file); fclose(r); } } ccv_icf_classifier_cascade_free(cascade); ccv_disable_cache(); return 0; }
int main(int argc, char** argv) { assert(argc == 3); ccv_enable_default_cache(); ccv_dense_matrix_t* image = 0; ccv_read(argv[1], &image, CCV_IO_RGB_COLOR | CCV_IO_ANY_FILE); ccv_mser_param_t params = { .min_area = 60, .max_area = (int)(image->rows * image->cols * 0.3 + 0.5), .min_diversity = 0.2, .area_threshold = 1.01, .min_margin = 0.003, .max_evolution = 200, .edge_blur_sigma = sqrt(3.0), .delta = 5, .max_variance = 0.25, .direction = CCV_DARK_TO_BRIGHT, }; if (image) { ccv_dense_matrix_t* yuv = 0; // ccv_color_transform(image, &yuv, 0, CCV_RGB_TO_YUV); ccv_read(argv[1], &yuv, CCV_IO_GRAY | CCV_IO_ANY_FILE); unsigned int elapsed_time = get_current_time(); ccv_dense_matrix_t* canny = 0; ccv_canny(yuv, &canny, 0, 3, 175, 320); ccv_dense_matrix_t* outline = 0; ccv_close_outline(canny, &outline, 0); ccv_matrix_free(canny); ccv_dense_matrix_t* mser = 0; ccv_array_t* mser_keypoint = ccv_mser(yuv, outline, &mser, 0, params); elapsed_time = get_current_time() - elapsed_time; ccv_matrix_free(outline); printf("total : %d in time %dms\n", mser_keypoint->rnum, elapsed_time); ccv_array_free(mser_keypoint); ccv_make_matrix_mutable(image); int i, j; for (i = 0; i < image->rows; i++) for (j = 0; j < image->cols; j++) { if (mser->data.i32[i * mser->cols + j]) { image->data.u8[i * image->step + j * 3] = colors[mser->data.i32[i * mser->cols + j] % 6][0]; image->data.u8[i * image->step + j * 3 + 1] = colors[mser->data.i32[i * mser->cols + j] % 6][1]; image->data.u8[i * image->step + j * 3 + 2] = colors[mser->data.i32[i * mser->cols + j] % 6][2]; } } ccv_write(image, argv[2], 0, CCV_IO_PNG_FILE, 0); ccv_matrix_free(yuv); ccv_matrix_free(image); } ccv_disable_cache(); return 0; }
int main(int argc, char** argv) { if (argc != 5) { print_help(); return -1; } ccv_enable_default_cache(); int i, rt; int posnum = atoi(argv[2]); FILE* pf = fopen(argv[1], "r"); ccv_dense_matrix_t** posimg = (ccv_dense_matrix_t**)malloc(sizeof(posimg[0]) * posnum); for (i = 0; i < posnum; i++) { char buf[1024]; rt = fscanf(pf, "%s", buf); ccv_read(buf, &posimg[i], CCV_IO_GRAY | CCV_IO_ANY_FILE); } fclose(pf); int negnum = atoi(argv[4]); FILE* bgf = fopen(argv[3], "r"); int bgnum; rt = fscanf(bgf, "%d", &bgnum); char** bgfiles = (char**)malloc(sizeof(bgfiles[0]) * bgnum); for (i = 0; i < bgnum; i++) { bgfiles[i] = (char*)malloc(1024); rt = fscanf(bgf, "%s", bgfiles[i]); } fclose(bgf); ccv_bbf_new_param_t params = { .pos_crit = 0.9975, .neg_crit = 0.50, .balance_k = 1.0, .layer = 24, .feature_number = 100, .optimizer = CCV_BBF_GENETIC_OPT | CCV_BBF_FLOAT_OPT, }; ccv_bbf_classifier_cascade_new(posimg, posnum, bgfiles, bgnum, negnum, ccv_size(24, 24), "data", params); for (i = 0; i < bgnum; i++) free(bgfiles[i]); for (i = 0; i < posnum; i++) ccv_matrix_free(&posimg[i]); free(posimg); free(bgfiles); ccv_disable_cache(); return 0; }
int main(int argc, char** argv) { printf("Face Detection Benchmark ...\n"); ccv_enable_default_cache(); ccv_dense_matrix_t* image = 0; ccv_bbf_classifier_cascade_t* cascade = ccv_bbf_read_classifier_cascade(argv[4]); ccv_read(argv[1], &image, CCV_IO_GRAY | CCV_IO_ANY_FILE); unsigned int elapsed_time; ccv_array_t* seq; elapsed_time = get_current_time(); seq = ccv_bbf_detect_objects(image, &cascade, 1, ccv_bbf_default_params); elapsed_time = get_current_time() - elapsed_time; printf("origin: %d in %dms\n", seq->rnum, elapsed_time); ccv_array_free(seq); int X_SLICE = atoi(argv[2]), Y_SLICE = atoi(argv[3]); int sliced_total = 0; int slice_rows = image->rows / Y_SLICE; int slice_cols = image->cols / X_SLICE; int i, count = X_SLICE * Y_SLICE; elapsed_time = get_current_time(); #pragma omp parallel for shared(sliced_total) for (i = 0; i < count; i++) { int y = i / X_SLICE; int x = i - X_SLICE * y; ccv_dense_matrix_t* slice = 0; ccv_slice(image, (ccv_matrix_t**)&slice, 0, slice_rows * y, slice_cols * x, slice_rows, slice_cols); ccv_array_t* sseq = ccv_bbf_detect_objects(slice, &cascade, 1, ccv_bbf_default_params); sliced_total += sseq->rnum; #ifdef DEBUG cos_ccv_slice_output(slice, y, x); #endif } elapsed_time = get_current_time() - elapsed_time; printf("slice & detect: %d in %dms\n", sliced_total, elapsed_time); ccv_matrix_free(image); ccv_bbf_classifier_cascade_free(cascade); ccv_disable_cache(); return 0; }
int main(int argc, char** argv) { static struct option image_net_options[] = { /* help */ {"help", 0, 0, 0}, /* required parameters */ {"train-list", 1, 0, 0}, {"test-list", 1, 0, 0}, {"working-dir", 1, 0, 0}, /* optional parameters */ {"base-dir", 1, 0, 0}, {"max-epoch", 1, 0, 0}, {"iterations", 1, 0, 0}, {0, 0, 0, 0} }; char* train_list = 0; char* test_list = 0; char* working_dir = 0; char* base_dir = 0; ccv_convnet_train_param_t train_params = { .max_epoch = 100, .mini_batch = 64, .sgd_frequency = 1, // do sgd every sgd_frequency batches (mini_batch * device_count * sgd_frequency) .iterations = 50000, .device_count = 4, .peer_access = 1, .symmetric = 1, .image_manipulation = 0.2, .color_gain = 0.001, .input = { .min_dim = 257, .max_dim = 257, }, }; int i, c; while (getopt_long_only(argc, argv, "", image_net_options, &c) != -1) { switch (c) { case 0: exit_with_help(); case 1: train_list = optarg; break; case 2: test_list = optarg; break; case 3: working_dir = optarg; break; case 4: base_dir = optarg; break; case 5: train_params.max_epoch = atoi(optarg); break; case 6: train_params.iterations = atoi(optarg); break; } } if (!train_list || !test_list || !working_dir) exit_with_help(); ccv_enable_default_cache(); FILE *r0 = fopen(train_list, "r"); assert(r0 && "train-list doesn't exists"); FILE* r1 = fopen(test_list, "r"); assert(r1 && "test-list doesn't exists"); char* file = (char*)malloc(1024); int dirlen = (base_dir != 0) ? strlen(base_dir) + 1 : 0; ccv_array_t* categorizeds = ccv_array_new(sizeof(ccv_categorized_t), 64, 0); while (fscanf(r0, "%d %s", &c, file) != EOF) { char* filename = (char*)ccmalloc(1024); if (base_dir != 0) { strncpy(filename, base_dir, 1024); filename[dirlen - 1] = '/'; } strncpy(filename + dirlen, file, 1024 - dirlen); ccv_file_info_t file_info = { .filename = filename, }; // imageNet's category class starts from 1, thus, minus 1 to get 0-index ccv_categorized_t categorized = ccv_categorized(c - 1, 0, &file_info); ccv_array_push(categorizeds, &categorized); } fclose(r0); ccv_array_t* tests = ccv_array_new(sizeof(ccv_categorized_t), 64, 0); while (fscanf(r1, "%d %s", &c, file) != EOF) { char* filename = (char*)ccmalloc(1024); if (base_dir != 0) { strncpy(filename, base_dir, 1024); filename[dirlen - 1] = '/'; } strncpy(filename + dirlen, file, 1024 - dirlen); ccv_file_info_t file_info = { .filename = filename, }; // imageNet's category class starts from 1, thus, minus 1 to get 0-index ccv_categorized_t categorized = ccv_categorized(c - 1, 0, &file_info); ccv_array_push(tests, &categorized); } fclose(r1); free(file); // #define model_params vgg_d_params #define model_params matt_c_params int depth = sizeof(model_params) / sizeof(ccv_convnet_layer_param_t); ccv_convnet_t* convnet = ccv_convnet_new(1, ccv_size(257, 257), model_params, depth); if (ccv_convnet_verify(convnet, 1000) == 0) { ccv_convnet_layer_train_param_t layer_params[depth]; memset(layer_params, 0, sizeof(layer_params)); for (i = 0; i < depth; i++) { layer_params[i].w.decay = 0.0005; layer_params[i].w.learn_rate = 0.01; layer_params[i].w.momentum = 0.9; layer_params[i].bias.decay = 0; layer_params[i].bias.learn_rate = 0.01; layer_params[i].bias.momentum = 0.9; } // set the two full connect layers to last with dropout rate at 0.5 for (i = depth - 3; i < depth - 1; i++) layer_params[i].dor = 0.5; train_params.layer_params = layer_params; ccv_set_cli_output_levels(ccv_cli_output_level_and_above(CCV_CLI_INFO)); ccv_convnet_supervised_train(convnet, categorizeds, tests, working_dir, train_params); } else { PRINT(CCV_CLI_ERROR, "Invalid convnet configuration\n"); } ccv_convnet_free(convnet); ccv_disable_cache(); return 0; }
int main(int argc, char** argv) { static struct option bbf_options[] = { /* help */ {"help", 0, 0, 0}, /* required parameters */ {"positive-list", 1, 0, 0}, {"background-list", 1, 0, 0}, {"working-dir", 1, 0, 0}, {"negative-count", 1, 0, 0}, {"width", 1, 0, 0}, {"height", 1, 0, 0}, /* optional parameters */ {"base-dir", 1, 0, 0}, {"layer", 1, 0, 0}, {"positive-criteria", 1, 0, 0}, {"negative-criteria", 1, 0, 0}, {"balance", 1, 0, 0}, {"feature-number", 1, 0, 0}, {0, 0, 0, 0} }; char* positive_list = 0; char* background_list = 0; char* working_dir = 0; char* base_dir = 0; int negnum = 0; int width = 0, height = 0; ccv_bbf_new_param_t params = { .pos_crit = 0.9975, .neg_crit = 0.50, .balance_k = 1.0, .layer = 24, .feature_number = 100, .optimizer = CCV_BBF_GENETIC_OPT | CCV_BBF_FLOAT_OPT, }; int i, k; while (getopt_long_only(argc, argv, "", bbf_options, &k) != -1) { switch (k) { case 0: exit_with_help(); case 1: positive_list = optarg; break; case 2: background_list = optarg; break; case 3: working_dir = optarg; break; case 4: negnum = atoi(optarg); break; case 5: width = atoi(optarg); break; case 6: height = atoi(optarg); break; case 7: base_dir = optarg; break; case 8: params.layer = atoi(optarg); break; case 9: params.pos_crit = atof(optarg); break; case 10: params.neg_crit = atof(optarg); break; case 11: params.balance_k = atof(optarg); break; case 12: params.feature_number = atoi(optarg); break; } } assert(positive_list != 0); assert(background_list != 0); assert(working_dir != 0); assert(negnum > 0); assert(width > 0 && height > 0); ccv_enable_default_cache(); FILE* r0 = fopen(positive_list, "r"); assert(r0 && "positive-list doesn't exists"); FILE* r1 = fopen(background_list, "r"); assert(r1 && "background-list doesn't exists"); char* file = (char*)malloc(1024); int dirlen = (base_dir != 0) ? strlen(base_dir) + 1 : 0; size_t len = 1024; ssize_t read; int capacity = 32, size = 0; ccv_dense_matrix_t** posimg = (ccv_dense_matrix_t**)ccmalloc(sizeof(ccv_dense_matrix_t*) * capacity); while ((read = getline(&file, &len, r0)) != -1) { while(read > 1 && isspace(file[read - 1])) read--; file[read] = 0; char* posfile = (char*)ccmalloc(1024); if (base_dir != 0) { strncpy(posfile, base_dir, 1024); posfile[dirlen - 1] = '/'; } strncpy(posfile + dirlen, file, 1024 - dirlen); posimg[size] = 0; ccv_read(posfile, &posimg[size], CCV_IO_GRAY | CCV_IO_ANY_FILE); if (posimg != 0) { ++size; if (size >= capacity) { capacity *= 2; posimg = (ccv_dense_matrix_t**)ccrealloc(posimg, sizeof(ccv_dense_matrix_t*) * capacity); } } } fclose(r0); int posnum = size; capacity = 32; size = 0; char** bgfiles = (char**)ccmalloc(sizeof(char*) * capacity); while ((read = getline(&file, &len, r1)) != -1) { while(read > 1 && isspace(file[read - 1])) read--; file[read] = 0; bgfiles[size] = (char*)ccmalloc(1024); if (base_dir != 0) { strncpy(bgfiles[size], base_dir, 1024); bgfiles[size][dirlen - 1] = '/'; } strncpy(bgfiles[size] + dirlen, file, 1024 - dirlen); ++size; if (size >= capacity) { capacity *= 2; bgfiles = (char**)ccrealloc(bgfiles, sizeof(char*) * capacity); } } fclose(r1); int bgnum = size; free(file); ccv_bbf_classifier_cascade_new(posimg, posnum, bgfiles, bgnum, negnum, ccv_size(width, height), working_dir, params); for (i = 0; i < bgnum; i++) free(bgfiles[i]); for (i = 0; i < posnum; i++) ccv_matrix_free(&posimg[i]); free(posimg); free(bgfiles); ccv_disable_cache(); return 0; }
int main(int argc, char** argv) { FILE* file; char *output_file = "my_output.txt"; int i, ret_val; ccv_dense_matrix_t* image = 0; ccv_array_t* seq; accept_roi_begin(); assert(argc >= 3); ccv_enable_default_cache(); ccv_bbf_classifier_cascade_t* cascade = ccv_bbf_read_classifier_cascade(argv[2]); ccv_read(argv[1], &image, CCV_IO_GRAY | CCV_IO_ANY_FILE); if (image != 0) { unsigned int elapsed_time = get_current_time(); seq = ccv_bbf_detect_objects(image, &cascade, 1, ccv_bbf_default_params); elapsed_time = get_current_time() - elapsed_time; for (i = 0; ENDORSE(i < seq->rnum); i++) { ccv_comp_t* comp = (ccv_comp_t*)ENDORSE(ccv_array_get(seq, i)); printf("%d %d %d %d %f\n", comp->rect.x, comp->rect.y, comp->rect.width, comp->rect.height, comp->classification.confidence); } printf("total : %d in time %dms\n", seq->rnum, elapsed_time); ccv_bbf_classifier_cascade_free(cascade); ccv_disable_cache(); accept_roi_end(); file = fopen(output_file, "w"); if (file == NULL) { perror("fopen for write failed"); return EXIT_FAILURE; } // latest changes struct coordinates { APPROX int x; APPROX int y; }; for (i = 0; ENDORSE(i < seq->rnum); i++) { ccv_comp_t* comp = (ccv_comp_t*) ENDORSE(ccv_array_get(seq, i)); struct coordinates upperleft, upperright, lowerleft, lowerright; upperleft.x = comp->rect.x; upperleft.y = comp->rect.y; upperright.x = comp->rect.x + comp->rect.width; upperright.y = upperleft.y; lowerleft.x = upperleft.x; lowerleft.y = upperleft.y + comp->rect.height; lowerright.x = upperright.x; lowerright.y = lowerleft.y; ret_val = fprintf(file, "%d %d\n%d %d\n%d %d\n%d %d\n", upperleft.x, upperleft.y, upperright.x, upperright.y, lowerright.x, lowerright.y, lowerleft.x, lowerleft.y); // latest changes if (ret_val < 0) { perror("fprintf of coordinates failed"); fclose(file); return EXIT_FAILURE; } } ret_val = fclose(file); if (ret_val != 0) { perror("fclose failed"); return EXIT_FAILURE; } ccv_array_free(seq); ccv_matrix_free(image); } else { FILE* r = fopen(argv[1], "rt"); if (argc == 4) chdir(argv[3]); if(r) { size_t len = 1024; char* file = (char*)malloc(len); ssize_t read; while((read = getline(&file, &len, r)) != -1) { while(read > 1 && isspace(file[read - 1])) read--; file[read] = 0; image = 0; ccv_read(file, &image, CCV_IO_GRAY | CCV_IO_ANY_FILE); assert(image != 0); seq = ccv_bbf_detect_objects(image, &cascade, 1, ccv_bbf_default_params); // seq already declared above for (i = 0; ENDORSE(i < seq->rnum); i++) { ccv_comp_t* comp = (ccv_comp_t*) ENDORSE(ccv_array_get(seq, i)); printf("%s %d %d %d %d %f\n", file, comp->rect.x, comp->rect.y, comp->rect.width, comp->rect.height, comp->classification.confidence); } } free(file); fclose(r); } ccv_bbf_classifier_cascade_free(cascade); ccv_disable_cache(); accept_roi_end(); file = fopen(output_file, "w"); if (file == NULL) { perror("fopen for write failed"); return EXIT_FAILURE; } for (i = 0; ENDORSE(i < seq->rnum); i++) { ccv_comp_t* comp = (ccv_comp_t*) ENDORSE(ccv_array_get(seq, i)); ret_val = fprintf(file, "%d\n%d\n%d\n%d\n%f\n", comp->rect.x, comp->rect.y, comp->rect.width, comp->rect.height, comp->classification.confidence); if (ret_val < 0) { perror("fprintf of coordinates and confidence failed"); fclose(file); return EXIT_FAILURE; } } ret_val = fclose(file); if (ret_val != 0) { perror("fclose failed"); return EXIT_FAILURE; } ccv_array_free(seq); ccv_matrix_free(image); } return 0; }
int main(int argc, char** argv) { static struct option icf_options[] = { /* help */ {"help", 0, 0, 0}, /* required parameters */ {"positive-list", 1, 0, 0}, {"background-list", 1, 0, 0}, {"validate-list", 1, 0, 0}, {"working-dir", 1, 0, 0}, {"negative-count", 1, 0, 0}, {"positive-count", 1, 0, 0}, {"acceptance", 1, 0, 0}, {"size", 1, 0, 0}, {"feature-size", 1, 0, 0}, {"weak-classifier-count", 1, 0, 0}, /* optional parameters */ {"base-dir", 1, 0, 0}, {"grayscale", 1, 0, 0}, {"margin", 1, 0, 0}, {"deform-shift", 1, 0, 0}, {"deform-angle", 1, 0, 0}, {"deform-scale", 1, 0, 0}, {"min-dimension", 1, 0, 0}, {"bootstrap", 1, 0, 0}, {0, 0, 0, 0} }; char* positive_list = 0; char* background_list = 0; char* validate_list = 0; char* working_dir = 0; char* base_dir = 0; int negative_count = 0; int positive_count = 0; ccv_icf_new_param_t params = { .grayscale = 0, .margin = ccv_margin(0, 0, 0, 0), .size = ccv_size(0, 0), .deform_shift = 1, .deform_angle = 0, .deform_scale = 0.075, .feature_size = 0, .weak_classifier = 0, .min_dimension = 2, .bootstrap = 3, .detector = ccv_icf_default_params, }; params.detector.step_through = 4; // for faster negatives bootstrap time int i, k; char* token; char* saveptr; while (getopt_long_only(argc, argv, "", icf_options, &k) != -1) { switch (k) { case 0: exit_with_help(); case 1: positive_list = optarg; break; case 2: background_list = optarg; break; case 3: validate_list = optarg; break; case 4: working_dir = optarg; break; case 5: negative_count = atoi(optarg); break; case 6: positive_count = atoi(optarg); break; case 7: params.acceptance = atof(optarg); break; case 8: token = strtok_r(optarg, "x", &saveptr); params.size.width = atoi(token); token = strtok_r(0, "x", &saveptr); params.size.height = atoi(token); break; case 9: params.feature_size = atoi(optarg); break; case 10: params.weak_classifier = atoi(optarg); break; case 11: base_dir = optarg; break; case 12: params.grayscale = !!atoi(optarg); break; case 13: token = strtok_r(optarg, ",", &saveptr); params.margin.left = atoi(token); token = strtok_r(0, ",", &saveptr); params.margin.top = atoi(token); token = strtok_r(0, ",", &saveptr); params.margin.right = atoi(token); token = strtok_r(0, ",", &saveptr); params.margin.bottom = atoi(token); break; case 14: params.deform_shift = atof(optarg); break; case 15: params.deform_angle = atof(optarg); break; case 16: params.deform_scale = atof(optarg); break; case 17: params.min_dimension = atoi(optarg); break; case 18: params.bootstrap = atoi(optarg); break; } } assert(positive_list != 0); assert(background_list != 0); assert(validate_list != 0); assert(working_dir != 0); assert(positive_count > 0); assert(negative_count > 0); assert(params.size.width > 0); assert(params.size.height > 0); ccv_enable_cache(512 * 1024 * 1024); FILE* r0 = fopen(positive_list, "r"); assert(r0 && "positive-list doesn't exists"); FILE* r1 = fopen(background_list, "r"); assert(r1 && "background-list doesn't exists"); FILE* r2 = fopen(validate_list, "r"); assert(r2 && "validate-list doesn't exists"); char* file = (char*)malloc(1024); ccv_decimal_pose_t pose; int dirlen = (base_dir != 0) ? strlen(base_dir) + 1 : 0; ccv_array_t* posfiles = ccv_array_new(sizeof(ccv_file_info_t), 32, 0); // roll pitch yaw while (fscanf(r0, "%s %f %f %f %f %f %f %f", file, &pose.x, &pose.y, &pose.a, &pose.b, &pose.roll, &pose.pitch, &pose.yaw) != EOF) { ccv_file_info_t file_info; file_info.filename = (char*)ccmalloc(1024); if (base_dir != 0) { strncpy(file_info.filename, base_dir, 1024); file_info.filename[dirlen - 1] = '/'; } strncpy(file_info.filename + dirlen, file, 1024 - dirlen); file_info.pose = pose; ccv_array_push(posfiles, &file_info); } fclose(r0); size_t len = 1024; ssize_t read; ccv_array_t* bgfiles = (ccv_array_t*)ccv_array_new(sizeof(ccv_file_info_t), 32, 0); while ((read = getline(&file, &len, r1)) != -1) { while(read > 1 && isspace(file[read - 1])) read--; file[read] = 0; ccv_file_info_t file_info; file_info.filename = (char*)ccmalloc(1024); if (base_dir != 0) { strncpy(file_info.filename, base_dir, 1024); file_info.filename[dirlen - 1] = '/'; } strncpy(file_info.filename + dirlen, file, 1024 - dirlen); ccv_array_push(bgfiles, &file_info); } fclose(r1); ccv_array_t* validatefiles = ccv_array_new(sizeof(ccv_file_info_t), 32, 0); // roll pitch yaw while (fscanf(r2, "%s %f %f %f %f %f %f %f", file, &pose.x, &pose.y, &pose.a, &pose.b, &pose.roll, &pose.pitch, &pose.yaw) != EOF) { ccv_file_info_t file_info; file_info.filename = (char*)ccmalloc(1024); if (base_dir != 0) { strncpy(file_info.filename, base_dir, 1024); file_info.filename[dirlen - 1] = '/'; } strncpy(file_info.filename + dirlen, file, 1024 - dirlen); file_info.pose = pose; ccv_array_push(validatefiles, &file_info); } fclose(r2); free(file); ccv_icf_classifier_cascade_t* classifier = ccv_icf_classifier_cascade_new(posfiles, positive_count, bgfiles, negative_count, validatefiles, working_dir, params); char filename[1024]; snprintf(filename, 1024, "%s/final-cascade", working_dir); ccv_icf_write_classifier_cascade(classifier, filename); for (i = 0; i < posfiles->rnum; i++) { ccv_file_info_t* file_info = (ccv_file_info_t*)ccv_array_get(posfiles, i); free(file_info->filename); } ccv_array_free(posfiles); for (i = 0; i < bgfiles->rnum; i++) { ccv_file_info_t* file_info = (ccv_file_info_t*)ccv_array_get(bgfiles, i); free(file_info->filename); } ccv_array_free(bgfiles); for (i = 0; i < validatefiles->rnum; i++) { ccv_file_info_t* file_info = (ccv_file_info_t*)ccv_array_get(validatefiles, i); free(file_info->filename); } ccv_array_free(validatefiles); ccv_disable_cache(); return 0; }
int main(int argc, char** argv) { #ifdef HAVE_AVCODEC #ifdef HAVE_AVFORMAT #ifdef HAVE_SWSCALE assert(argc == 6); ccv_rect_t box = ccv_rect(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), atoi(argv[5])); box.width = box.width - box.x + 1; box.height = box.height - box.y + 1; printf("%d,%d,%d,%d,%f\n", box.x, box.y, box.width + box.x - 1, box.height + box.y - 1, 1.0f); // init av-related structs AVFormatContext* ic = 0; int video_stream = -1; AVStream* video_st = 0; AVFrame* picture = 0; AVFrame rgb_picture; memset(&rgb_picture, 0, sizeof(AVPicture)); AVPacket packet; memset(&packet, 0, sizeof(AVPacket)); av_init_packet(&packet); av_register_all(); avformat_network_init(); // load video and codec avformat_open_input(&ic, argv[1], 0, 0); avformat_find_stream_info(ic, 0); int i; for (i = 0; i < ic->nb_streams; i++) { AVCodecContext* enc = ic->streams[i]->codec; enc->thread_count = 2; if (AVMEDIA_TYPE_VIDEO == enc->codec_type && video_stream < 0) { AVCodec* codec = avcodec_find_decoder(enc->codec_id); if (!codec || avcodec_open2(enc, codec, 0) < 0) continue; video_stream = i; video_st = ic->streams[i]; picture = avcodec_alloc_frame(); rgb_picture.data[0] = (uint8_t*)ccmalloc(avpicture_get_size(PIX_FMT_RGB24, enc->width, enc->height)); avpicture_fill((AVPicture*)&rgb_picture, rgb_picture.data[0], PIX_FMT_RGB24, enc->width, enc->height); break; } } int got_picture = 0; while (!got_picture) { int result = av_read_frame(ic, &packet); if (result == AVERROR(EAGAIN)) continue; avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet); } ccv_enable_default_cache(); struct SwsContext* picture_ctx = sws_getCachedContext(0, video_st->codec->width, video_st->codec->height, video_st->codec->pix_fmt, video_st->codec->width, video_st->codec->height, PIX_FMT_RGB24, SWS_BICUBIC, 0, 0, 0); sws_scale(picture_ctx, (const uint8_t* const*)picture->data, picture->linesize, 0, video_st->codec->height, rgb_picture.data, rgb_picture.linesize); ccv_dense_matrix_t* x = 0; ccv_read(rgb_picture.data[0], &x, CCV_IO_RGB_RAW | CCV_IO_GRAY, video_st->codec->height, video_st->codec->width, rgb_picture.linesize[0]); ccv_tld_t* tld = ccv_tld_new(x, box, ccv_tld_default_params); ccv_dense_matrix_t* y = 0; for (;;) { got_picture = 0; int result = av_read_frame(ic, &packet); if (result == AVERROR(EAGAIN)) continue; avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet); if (!got_picture) break; sws_scale(picture_ctx, (const uint8_t* const*)picture->data, picture->linesize, 0, video_st->codec->height, rgb_picture.data, rgb_picture.linesize); ccv_read(rgb_picture.data[0], &y, CCV_IO_RGB_RAW | CCV_IO_GRAY, video_st->codec->height, video_st->codec->width, rgb_picture.linesize[0]); ccv_tld_info_t info; ccv_comp_t newbox = ccv_tld_track_object(tld, x, y, &info); printf("%04d: performed learn: %d, performed track: %d, successfully track: %d; %d passed fern detector, %d passed nnc detector, %d merged, %d confident matches, %d close matches\n", tld->count, info.perform_learn, info.perform_track, info.track_success, info.ferns_detects, info.nnc_detects, info.clustered_detects, info.confident_matches, info.close_matches); ccv_dense_matrix_t* image = 0; ccv_read(rgb_picture.data[0], &image, CCV_IO_RGB_RAW | CCV_IO_RGB_COLOR, video_st->codec->height, video_st->codec->width, rgb_picture.linesize[0]); // draw out // for (i = 0; i < tld->top->rnum; i++) if (tld->found) { ccv_comp_t* comp = &newbox; // (ccv_comp_t*)ccv_array_get(tld->top, i); if (comp->rect.x >= 0 && comp->rect.x + comp->rect.width < image->cols && comp->rect.y >= 0 && comp->rect.y + comp->rect.height < image->rows) { int x, y; for (x = comp->rect.x; x < comp->rect.x + comp->rect.width; x++) { image->data.u8[image->step * comp->rect.y + x * 3] = image->data.u8[image->step * (comp->rect.y + comp->rect.height - 1) + x * 3] = 255; image->data.u8[image->step * comp->rect.y + x * 3 + 1] = image->data.u8[image->step * (comp->rect.y + comp->rect.height - 1) + x * 3 + 1] = image->data.u8[image->step * comp->rect.y + x * 3 + 2] = image->data.u8[image->step * (comp->rect.y + comp->rect.height - 1) + x * 3 + 2] = 0; } for (y = comp->rect.y; y < comp->rect.y + comp->rect.height; y++) { image->data.u8[image->step * y + comp->rect.x * 3] = image->data.u8[image->step * y + (comp->rect.x + comp->rect.width - 1) * 3] = 255; image->data.u8[image->step * y + comp->rect.x * 3 + 1] = image->data.u8[image->step * y + (comp->rect.x + comp->rect.width - 1) * 3 + 1] = image->data.u8[image->step * y + comp->rect.x * 3 + 2] = image->data.u8[image->step * y + (comp->rect.x + comp->rect.width - 1) * 3 + 2] = 0; } } } char filename[1024]; sprintf(filename, "tld-out/output-%04d.png", tld->count); ccv_write(image, filename, 0, CCV_IO_PNG_FILE, 0); ccv_matrix_free(image); if (tld->found) printf("%d,%d,%d,%d,%f\n", newbox.rect.x, newbox.rect.y, newbox.rect.width + newbox.rect.x - 1, newbox.rect.height + newbox.rect.y - 1, newbox.confidence); else printf("NaN,NaN,NaN,NaN,NaN\n"); x = y; y = 0; } ccv_matrix_free(x); ccv_tld_free(tld); ccfree(rgb_picture.data[0]); ccv_disable_cache(); #endif #endif #endif return 0; }
int main(int argc, char** argv) { assert(argc == 3); ccv_enable_default_cache(); ccv_dense_matrix_t* object = 0; ccv_dense_matrix_t* image = 0; ccv_read(argv[1], &object, CCV_IO_GRAY | CCV_IO_ANY_FILE); ccv_read(argv[2], &image, CCV_IO_GRAY | CCV_IO_ANY_FILE); unsigned int elapsed_time = get_current_time(); ccv_sift_param_t params = { .noctaves = 3, .nlevels = 6, .up2x = 1, .edge_threshold = 10, .norm_threshold = 0, .peak_threshold = 0, }; ccv_array_t* obj_keypoints = 0; ccv_dense_matrix_t* obj_desc = 0; ccv_sift(object, &obj_keypoints, &obj_desc, 0, params); ccv_array_t* image_keypoints = 0; ccv_dense_matrix_t* image_desc = 0; ccv_sift(image, &image_keypoints, &image_desc, 0, params); elapsed_time = get_current_time() - elapsed_time; int i, j, k; int match = 0; for (i = 0; i < obj_keypoints->rnum; i++) { float* odesc = obj_desc->data.f32 + i * 128; int minj = -1; double mind = 1e6, mind2 = 1e6; for (j = 0; j < image_keypoints->rnum; j++) { float* idesc = image_desc->data.f32 + j * 128; double d = 0; for (k = 0; k < 128; k++) { d += (odesc[k] - idesc[k]) * (odesc[k] - idesc[k]); if (d > mind2) break; } if (d < mind) { mind2 = mind; mind = d; minj = j; } else if (d < mind2) { mind2 = d; } } if (mind < mind2 * 0.36) { ccv_keypoint_t* op = (ccv_keypoint_t*)ccv_array_get(obj_keypoints, i); ccv_keypoint_t* kp = (ccv_keypoint_t*)ccv_array_get(image_keypoints, minj); printf("%f %f => %f %f\n", op->x, op->y, kp->x, kp->y); match++; } } printf("%dx%d on %dx%d\n", object->cols, object->rows, image->cols, image->rows); printf("%d keypoints out of %d are matched\n", match, obj_keypoints->rnum); printf("elpased time : %d\n", elapsed_time); ccv_array_free(obj_keypoints); ccv_array_free(image_keypoints); ccv_matrix_free(obj_desc); ccv_matrix_free(image_desc); ccv_matrix_free(object); ccv_matrix_free(image); ccv_disable_cache(); return 0; }