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) { ccv_enable_default_cache(); ccv_convnet_layer_param_t params[] = { { .type = CCV_CONVNET_CONVOLUTIONAL, .bias = 0, .sigma = 0.0001, .input = { .matrix = { .rows = 31, .cols = 31, .channels = 3, .partition = 1, }, }, .output = { .convolutional = { .rows = 5, .cols = 5, .channels = 3, .border = 2, .strides = 1, .count = 32, .partition = 1, }, }, },
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) { ccv_enable_default_cache(); assert(argc == 2); FILE *r = fopen(argv[1], "r"); char* file = (char*)malloc(1024); ccv_array_t* categorizeds = ccv_array_new(sizeof(ccv_categorized_t), 64, 0); size_t len = 1024; ssize_t read; while ((read = getline(&file, &len, r)) != -1) { while(read > 1 && isspace(file[read - 1])) read--; file[read] = 0; ccv_file_info_t input; input.filename = (char*)ccmalloc(1024); strncpy(input.filename, file, 1024); ccv_categorized_t categorized = ccv_categorized(0, 0, &input); ccv_array_push(categorizeds, &categorized); } fclose(r); free(file); /* MattNet parameters */ ccv_convnet_layer_param_t params[13] = { // first layer (convolutional => max pool => rnorm) { .type = CCV_CONVNET_CONVOLUTIONAL, .bias = 0, .glorot = sqrtf(2), .input = { .matrix = { .rows = 225, .cols = 225, .channels = 3, .partition = 1, }, }, .output = { .convolutional = { .count = 96, .strides = 2, .border = 1, .rows = 7, .cols = 7, .channels = 3, .partition = 2, }, }, }, { .type = CCV_CONVNET_LOCAL_RESPONSE_NORM,
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; }
TDetector::TDetector(std::vector<ccv_swt_param_t> params) { this->params = params; ccv_enable_default_cache(); }
TDetector::TDetector() { params.push_back(ccv_swt_default_params); ccv_enable_default_cache(); }
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; }
ofxCcv::ofxCcv() { ccv_enable_default_cache(); }
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); 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 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) { 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 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) { // 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; }