| 39 | // ---------------------------------------------------------------------------------------- |
| 40 | |
| 41 | void parse_annotation_file( |
| 42 | const std::string& file, |
| 43 | dlib::image_dataset_metadata::image& img, |
| 44 | std::string& dataset_name |
| 45 | ) |
| 46 | { |
| 47 | ifstream fin(file.c_str()); |
| 48 | if (!fin) |
| 49 | throw dlib::error("Unable to open file " + file); |
| 50 | |
| 51 | img = dlib::image_dataset_metadata::image(); |
| 52 | |
| 53 | string str, line; |
| 54 | std::vector<string> words; |
| 55 | while (fin.peek() != EOF) |
| 56 | { |
| 57 | getline(fin, line); |
| 58 | words = split(line, " \r\n\t:(,-)\""); |
| 59 | if (words.size() > 2) |
| 60 | { |
| 61 | if (words[0] == "#") |
| 62 | continue; |
| 63 | |
| 64 | if (words[0] == "Image" && words[1] == "filename") |
| 65 | { |
| 66 | img.filename = pick_out_quoted_string(line); |
| 67 | } |
| 68 | else if (words[0] == "Database") |
| 69 | { |
| 70 | dataset_name = pick_out_quoted_string(line); |
| 71 | } |
| 72 | else if (words[0] == "Objects" && words[1] == "with" && words.size() >= 5) |
| 73 | { |
| 74 | const int num = sa = words[4]; |
| 75 | img.boxes.resize(num); |
| 76 | } |
| 77 | else if (words.size() > 4 && (words[2] == "for" || words[2] == "on") && words[3] == "object") |
| 78 | { |
| 79 | long idx = sa = words[4]; |
| 80 | --idx; |
| 81 | if (idx >= (long)img.boxes.size()) |
| 82 | throw dlib::error("Invalid object id number of " + words[4]); |
| 83 | |
| 84 | if (words[0] == "Center" && words[1] == "point" && words.size() > 9) |
| 85 | { |
| 86 | const long x = sa = words[8]; |
| 87 | const long y = sa = words[9]; |
| 88 | img.boxes[idx].parts["head"] = point(x,y); |
| 89 | } |
| 90 | else if (words[0] == "Bounding" && words[1] == "box" && words.size() > 13) |
| 91 | { |
| 92 | rectangle rect; |
| 93 | img.boxes[idx].rect.left() = sa = words[10]; |
| 94 | img.boxes[idx].rect.top() = sa = words[11]; |
| 95 | img.boxes[idx].rect.right() = sa = words[12]; |
| 96 | img.boxes[idx].rect.bottom() = sa = words[13]; |
| 97 | } |
| 98 | else if (words[0] == "Original" && words[1] == "label" && words.size() > 6) |