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; }
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) { ccv_dense_matrix_t* image = 0; ccv_read(argv[1], &image, CCV_IO_GRAY | CCV_IO_ANY_FILE); ccv_write(image, argv[2], 0, CCV_IO_PNG_FILE, 0); return 0; }
int main(int argc, char** argv) { int counter = 0; ccv_dense_matrix_t* image = 0; ccv_enable_default_cache(); ccv_read(argv[1], &image, CCV_IO_GRAY | CCV_IO_ANY_FILE); if (image != 0) { ccv_array_t* words = ccv_swt_detect_words(image, ccv_swt_default_params); if (words) { int i; for (i = 0; i < words->rnum; i++) { char filename[256]; ccv_matrix_t* box = 0; ccv_rect_t* rect = (ccv_rect_t*)ccv_array_get(words, i); ccv_slice(image, &box, 0, rect->y, rect->x, rect->height, rect->width); printf("%d %d %d %d\n", rect->x, rect->y, rect->width, rect->height); snprintf(filename, 256, "out-%d.png", ++counter); ccv_write(box, filename, NULL, CCV_IO_PNG_FILE, NULL); } } ccv_array_free(words); } ccv_drain_cache(); return 0; }
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) { ccv_dense_matrix_t* mat1 = 0; ccv_dense_matrix_t* mat2 = 0; ccv_dense_matrix_t* mat3 = 0; ccv_read(argv[1], &mat1, CCV_IO_GRAY | CCV_IO_ANY_FILE); ccv_read(argv[2], &mat2, CCV_IO_GRAY | CCV_IO_ANY_FILE); ccv_add((ccv_matrix_t *)mat1, (ccv_matrix_t *)mat2, (ccv_matrix_t **)&mat3, mat1->type); ccv_write(mat3, "test.jpeg", 0, CCV_IO_JPEG_FILE, 0); ccv_matrix_free(mat1); ccv_matrix_free(mat2); ccv_matrix_free(mat3); 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) { ccv_dense_matrix_t* image = 0; ccv_read(argv[1], &image, CCV_IO_RGB_COLOR | CCV_IO_ANY_FILE); ccv_scd_classifier_cascade_t* cascade = ccv_scd_classifier_cascade_read(argv[2]); ccv_array_t* faces = ccv_scd_detect_objects(image, &cascade, 1, ccv_scd_default_params); int i; for (i = 0; i < faces->rnum; i++) { ccv_comp_t* face = (ccv_comp_t*)ccv_array_get(faces, i); printf("%d %d %d %d\n", face->rect.x, face->rect.y, face->rect.width, face->rect.height); } ccv_array_free(faces); ccv_scd_classifier_cascade_free(cascade); ccv_matrix_free(image); 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) { ccv_nnc_init(); ccv_convnet_t* convnet = ccv_convnet_read(0, argv[2]); ccv_dense_matrix_t* image = 0; ccv_read(argv[1], &image, CCV_IO_ANY_FILE | CCV_IO_RGB_COLOR); if (image != 0) { ccv_dense_matrix_t* input = 0; ccv_convnet_input_formation(convnet->input, image, &input); ccv_matrix_free(image); ccv_dense_matrix_t* sliced = 0; ccv_slice(input, (ccv_matrix_t**)&sliced, 0, (input->rows - 225) / 2, (input->cols - 225) / 2, 225, 225); ccv_matrix_free(input); ccv_dense_matrix_t* b = 0; unsigned int elapsed_time = get_current_time(); ccv_convnet_encode(convnet, &sliced, &b, 1); printf("ccv_convnet_encode %u ms\n", get_current_time() - elapsed_time); ccv_nnc_tensor_t* c = ccv_nnc_tensor_new(0, ONE_CPU_TENSOR(1000), 0); ccv_nnc_graph_exec_t source, dest; ccv_array_t* tensors = ccv_array_new(sizeof(ccv_nnc_tensor_t*), 1, 0); ccv_nnc_graph_t* graph = ccv_nnc_simple_graph(convnet, (ccv_nnc_tensor_t*)sliced, c, &source, &dest, tensors); elapsed_time = get_current_time(); ccv_nnc_graph_run(graph, 0, &source, 1, &dest, 1); printf("ccv_nnc_graph_run %u ms\n", get_current_time() - elapsed_time); int i; for (i = 0; i < 1000; i++) if (fabsf(b->data.f32[i] - c->data.f32[i]) > 1e-4) printf("mis-match at %d: %f %f\n", i, b->data.f32[i], c->data.f32[i]); ccv_nnc_tensor_free(c); ccv_matrix_free(sliced); ccv_matrix_free(b); ccv_nnc_graph_free(graph); for (i = 0; i < tensors->rnum; i++) ccv_nnc_tensor_free(*(ccv_nnc_tensor_t**)ccv_array_get(tensors, i)); ccv_array_free(tensors); } ccv_convnet_free(convnet); return 0; }
int main(int argc, char** argv) { // process arguments char* image_file = ""; int scale_invariant = 0; int min_height = 0; int min_area = 0; char ch; while ((ch = getopt(argc, argv, "a:h:i")) != EOF) switch(ch) { case 'i': scale_invariant = 1; printf("Scale invariant\n"); break; case 'h': min_height = atoi(optarg); printf("Min height %d\n", min_height); continue; case 'a': min_area = atoi(optarg); printf("Min area %d\n", min_area); continue; } argc -= optind; argv += optind; image_file = argv[0]; ccv_enable_default_cache(); ccv_dense_matrix_t* image = 0; ccv_read(image_file, &image, CCV_IO_GRAY | CCV_IO_ANY_FILE); if (image==0) { fprintf(stderr, "ERROR: image could not be read\n"); return 1; } unsigned int elapsed_time = get_current_time(); ccv_swt_param_t params = ccv_swt_default_params; if (scale_invariant) params.scale_invariant = 1; if (min_height) params.min_height = min_height; if (min_area) params.min_area = min_area; ccv_array_t* letters = ccv_swt_detect_chars_contour(image, params); elapsed_time = get_current_time() - elapsed_time; if (letters) { printf("total : %d in time %dms\n", letters->rnum, elapsed_time); int i; for (i = 0; i < letters->rnum; i++) { // for each letter ccv_contour_t* cont = *(ccv_contour_t**)ccv_array_get(letters, i); printf("Contour %d (size=%d):\n", i, cont->size); for (int j = 0; j < cont->size; j++) { // for each point in contour ccv_point_t* point = (ccv_point_t*)ccv_array_get(cont->set, j); printf("%d %d\n", point->x, point->y); } printf("End contour %d\n", i); } ccv_array_free(letters); } ccv_matrix_free(image); ccv_drain_cache(); return 0; }
void TDetector::detect(const cv::Mat &rgb, std::vector<Text2D>& text2d) { ccv_dense_matrix_t* image = 0; ccv_read(rgb.data, &image, CCV_IO_GRAY_RAW | CCV_IO_ANY_RAW | CCV_IO_GRAY, rgb.rows, rgb.cols, rgb.step[0]); TDetector::pdetect(image, text2d); }
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; }
int main(int argc, char** argv) { assert(argc >= 3); int i, j; ccv_enable_default_cache(); ccv_dense_matrix_t* image = 0; ccv_read(argv[1], &image, CCV_IO_ANY_FILE); ccv_dpm_mixture_model_t* model = ccv_dpm_read_mixture_model(argv[2]); if (image != 0) { unsigned int elapsed_time = get_current_time(); ccv_array_t* seq = ccv_dpm_detect_objects(image, &model, 1, ccv_dpm_default_params); elapsed_time = get_current_time() - elapsed_time; if (seq) { for (i = 0; i < seq->rnum; i++) { ccv_root_comp_t* comp = (ccv_root_comp_t*)ccv_array_get(seq, i); printf("%d %d %d %d %f %d\n", comp->rect.x, comp->rect.y, comp->rect.width, comp->rect.height, comp->classification.confidence, comp->pnum); for (j = 0; j < comp->pnum; j++) printf("| %d %d %d %d %f\n", comp->part[j].rect.x, comp->part[j].rect.y, comp->part[j].rect.width, comp->part[j].rect.height, comp->part[j].classification.confidence); } printf("total : %d in time %dms\n", seq->rnum, elapsed_time); ccv_array_free(seq); } else { printf("elapsed time %dms\n", elapsed_time); } 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); ccv_array_t* seq = ccv_dpm_detect_objects(image, &model, 1, ccv_dpm_default_params); if (seq != 0) { for (i = 0; i < seq->rnum; i++) { ccv_root_comp_t* comp = (ccv_root_comp_t*)ccv_array_get(seq, i); printf("%s %d %d %d %d %f %d\n", file, comp->rect.x, comp->rect.y, comp->rect.width, comp->rect.height, comp->classification.confidence, comp->pnum); for (j = 0; j < comp->pnum; j++) printf("| %d %d %d %d %f\n", comp->part[j].rect.x, comp->part[j].rect.y, comp->part[j].rect.width, comp->part[j].rect.height, comp->part[j].classification.confidence); } ccv_array_free(seq); } ccv_matrix_free(image); } free(file); fclose(r); } } ccv_drain_cache(); ccv_dpm_mixture_model_free(model); return 0; }
int uri_dpm_detect_objects(const void* context, const void* parsed, ebb_buf* buf) { if (!parsed) return -1; dpm_param_parser_t* parser = (dpm_param_parser_t*)parsed; param_parser_terminate(&parser->param_parser); if (parser->source.data == 0) { free(parser); return -1; } if (parser->mixture_model == 0) { free(parser->source.data); free(parser); return -1; } ccv_dense_matrix_t* image = 0; ccv_read(parser->source.data, &image, CCV_IO_ANY_STREAM | CCV_IO_GRAY, parser->source.written); free(parser->source.data); if (image == 0) { free(parser); return -1; } ccv_array_t* seq = ccv_dpm_detect_objects(image, &parser->mixture_model, 1, parser->params); ccv_matrix_free(image); if (seq == 0) { free(parser); return -1; } if (seq->rnum > 0) { int i, j; buf->len = 192 + seq->rnum * 131 + 2; char* data = (char*)malloc(buf->len); data[0] = '['; buf->written = 1; for (i = 0; i < seq->rnum; i++) { char cell[128]; ccv_root_comp_t* comp = (ccv_root_comp_t*)ccv_array_get(seq, i); snprintf(cell, 128, "{\"x\":%d,\"y\":%d,\"width\":%d,\"height\":%d,\"confidence\":%f,\"parts\":[", comp->rect.x, comp->rect.y, comp->rect.width, comp->rect.height, comp->confidence); size_t len = strnlen(cell, 128); while (buf->written + len >= buf->len) { buf->len = (buf->len * 3 + 1) / 2; data = (char*)realloc(data, buf->len); } memcpy(data + buf->written, cell, len); buf->written += len; for (j = 0; j < comp->pnum; j++) { snprintf(cell, 128, "{\"x\":%d,\"y\":%d,\"width\":%d,\"height\":%d,\"confidence\":%f}", comp->part[j].rect.x, comp->part[j].rect.y, comp->part[j].rect.width, comp->part[j].rect.height, comp->part[j].confidence); len = strnlen(cell, 128); while (buf->written + len + 3 >= buf->len) { buf->len = (buf->len * 3 + 1) / 2; data = (char*)realloc(data, buf->len); } memcpy(data + buf->written, cell, len); buf->written += len + 1; data[buf->written - 1] = (j == comp->pnum - 1) ? ']' : ','; } buf->written += 2; data[buf->written - 2] = '}'; data[buf->written - 1] = (i == seq->rnum - 1) ? ']' : ','; } char http_header[192]; snprintf(http_header, 192, ebb_http_header, buf->written); size_t len = strnlen(http_header, 192); if (buf->written + len + 1 >= buf->len) { buf->len = buf->written + len + 1; data = (char*)realloc(data, buf->len); } memmove(data + len, data, buf->written); memcpy(data, http_header, len); buf->written += len + 1; data[buf->written - 1] = '\n'; buf->data = data; buf->len = buf->written; buf->on_release = uri_ebb_buf_free; } else { buf->data = (void*)ebb_http_empty_array; buf->len = sizeof(ebb_http_empty_array); buf->on_release = 0; } ccv_array_free(seq); free(parser); 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) { #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; }