| 105 | // ---------------------------------------------------------------------------------------- |
| 106 | |
| 107 | int main(int argc, char** argv) try |
| 108 | { |
| 109 | if (argc != 2) |
| 110 | { |
| 111 | cout << "You call this program like this: " << endl; |
| 112 | cout << "./dnn_semantic_segmentation_train_ex /path/to/images" << endl; |
| 113 | cout << endl; |
| 114 | cout << "You will also need a trained '" << semantic_segmentation_net_filename << "' file." << endl; |
| 115 | cout << "You can either train it yourself (see example program" << endl; |
| 116 | cout << "dnn_semantic_segmentation_train_ex), or download a" << endl; |
| 117 | cout << "copy from here: http://dlib.net/files/" << semantic_segmentation_net_filename << endl; |
| 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | // Read the file containing the trained network from the working directory. |
| 122 | anet_type net; |
| 123 | deserialize(semantic_segmentation_net_filename) >> net; |
| 124 | |
| 125 | // Show inference results in a window. |
| 126 | image_window win; |
| 127 | |
| 128 | matrix<rgb_pixel> input_image; |
| 129 | matrix<uint16_t> index_label_image; |
| 130 | matrix<rgb_pixel> rgb_label_image; |
| 131 | |
| 132 | // Find supported image files. |
| 133 | const std::vector<file> files = dlib::get_files_in_directory_tree(argv[1], |
| 134 | dlib::match_endings(".jpeg .jpg .png")); |
| 135 | |
| 136 | cout << "Found " << files.size() << " images, processing..." << endl; |
| 137 | |
| 138 | for (const file& file : files) |
| 139 | { |
| 140 | // Load the input image. |
| 141 | load_image(input_image, file.full_name()); |
| 142 | |
| 143 | // Create predictions for each pixel. At this point, the type of each prediction |
| 144 | // is an index (a value between 0 and 20). Note that the net may return an image |
| 145 | // that is not exactly the same size as the input. |
| 146 | const matrix<uint16_t> temp = net(input_image); |
| 147 | |
| 148 | // Crop the returned image to be exactly the same size as the input. |
| 149 | const chip_details chip_details( |
| 150 | centered_rect(temp.nc() / 2, temp.nr() / 2, input_image.nc(), input_image.nr()), |
| 151 | chip_dims(input_image.nr(), input_image.nc()) |
| 152 | ); |
| 153 | extract_image_chip(temp, chip_details, index_label_image, interpolate_nearest_neighbor()); |
| 154 | |
| 155 | // Convert the indexes to RGB values. |
| 156 | index_label_image_to_rgb_label_image(index_label_image, rgb_label_image); |
| 157 | |
| 158 | // Show the input image on the left, and the predicted RGB labels on the right. |
| 159 | win.set_image(join_rows(input_image, rgb_label_image)); |
| 160 | |
| 161 | // Find the most prominent class label from amongst the per-pixel predictions. |
| 162 | const std::string classlabel = get_most_prominent_non_background_classlabel(index_label_image); |
| 163 | |
| 164 | cout << file.name() << " : " << classlabel << " - hit enter to process the next image"; |
nothing calls this directly
no test coverage detected