| 404 | // ---------------------------------------------------------------------------------------- |
| 405 | |
| 406 | int resample_dataset(const command_line_parser& parser) |
| 407 | { |
| 408 | if (parser.number_of_arguments() != 1) |
| 409 | { |
| 410 | cerr << "The --resample option requires you to give one XML file on the command line." << endl; |
| 411 | return EXIT_FAILURE; |
| 412 | } |
| 413 | |
| 414 | const size_t obj_size = get_option(parser,"cropped-object-size",100*100); |
| 415 | const double margin_scale = get_option(parser,"crop-size",2.5); // cropped image will be this times wider than the object. |
| 416 | const unsigned long min_object_size = get_option(parser,"min-object-size",1); |
| 417 | const bool one_object_per_image = parser.option("one-object-per-image"); |
| 418 | |
| 419 | dlib::image_dataset_metadata::dataset data, resampled_data; |
| 420 | std::ostringstream sout; |
| 421 | sout << "\nThe --resample parameters which generated this dataset were:" << endl; |
| 422 | sout << " cropped-object-size: "<< obj_size << endl; |
| 423 | sout << " crop-size: "<< margin_scale << endl; |
| 424 | sout << " min-object-size: "<< min_object_size << endl; |
| 425 | if (one_object_per_image) |
| 426 | sout << " one_object_per_image: true" << endl; |
| 427 | resampled_data.comment = data.comment + sout.str(); |
| 428 | resampled_data.name = data.name + " RESAMPLED"; |
| 429 | |
| 430 | load_image_dataset_metadata(data, parser[0]); |
| 431 | locally_change_current_dir chdir(get_parent_directory(file(parser[0]))); |
| 432 | dlib::rand rnd; |
| 433 | |
| 434 | const size_t image_size = std::round(std::sqrt(obj_size*margin_scale*margin_scale)); |
| 435 | const chip_dims cdims(image_size, image_size); |
| 436 | |
| 437 | console_progress_indicator pbar(data.images.size()); |
| 438 | for (unsigned long i = 0; i < data.images.size(); ++i) |
| 439 | { |
| 440 | // don't even bother loading images that don't have objects. |
| 441 | if (data.images[i].boxes.size() == 0) |
| 442 | continue; |
| 443 | |
| 444 | pbar.print_status(i); |
| 445 | array2d<rgb_pixel> img, chip; |
| 446 | load_image(img, data.images[i].filename); |
| 447 | |
| 448 | |
| 449 | // figure out what chips we want to take from this image |
| 450 | for (unsigned long j = 0; j < data.images[i].boxes.size(); ++j) |
| 451 | { |
| 452 | const rectangle rect = data.images[i].boxes[j].rect; |
| 453 | if (data.images[i].boxes[j].ignore || rect.area() < min_object_size) |
| 454 | continue; |
| 455 | |
| 456 | const auto max_dim = std::max(rect.width(), rect.height()); |
| 457 | |
| 458 | const double rand_scale_perturb = 1 - 0.3*(rnd.get_random_double()-0.5); |
| 459 | const rectangle crop_rect = centered_rect(rect, max_dim*margin_scale*rand_scale_perturb, max_dim*margin_scale*rand_scale_perturb); |
| 460 | |
| 461 | const rectangle_transform tform = get_mapping_to_chip(chip_details(crop_rect, cdims)); |
| 462 | extract_image_chip(img, chip_details(crop_rect, cdims), chip); |
| 463 |
no test coverage detected