Fork me on GitHub

全景分割panopticapi使用

文件解析

参考github:https://github.com/cocodataset/panopticapi

输入图像:

标注图像png:

标注json文件(里面有两张图):

{"info": {"description": "COCO 2018 Panoptic Dataset", "url": "http://cocodataset.org", "version": "1.0", "year": 2018, "contributor": "https://arxiv.org/abs/1801.00868", "date_created": "2018-06-01 00:00:00.0"}, "licenses": [{"url": "http://creativecommons.org/licenses/by-nc-sa/2.0/", "id": 1, "name": "Attribution-NonCommercial-ShareAlike License"}, {"url": "http://creativecommons.org/licenses/by-nc/2.0/", "id": 2, "name": "Attribution-NonCommercial License"}, {"url": "http://creativecommons.org/licenses/by-nc-nd/2.0/", "id": 3, "name": "Attribution-NonCommercial-NoDerivs License"}, {"url": "http://creativecommons.org/licenses/by/2.0/", "id": 4, "name": "Attribution License"}, {"url": "http://creativecommons.org/licenses/by-sa/2.0/", "id": 5, "name": "Attribution-ShareAlike License"}, {"url": "http://creativecommons.org/licenses/by-nd/2.0/", "id": 6, "name": "Attribution-NoDerivs License"}, {"url": "http://flickr.com/commons/usage/", "id": 7, "name": "No known copyright restrictions"}, {"url": "http://www.usa.gov/copyright.shtml", "id": 8, "name": "United States Government Work"}], "images": [{"license": 2, "file_name": "000000142238.jpg", "coco_url": "http://images.cocodataset.org/val2017/000000142238.jpg", "height": 427, "width": 640, "date_captured": "2013-11-20 16:47:35", "flickr_url": "http://farm5.staticflickr.com/4028/5079131149_dde584ed79_z.jpg", "id": 142238}, {"license": 1, "file_name": "000000439180.jpg", "coco_url": "http://images.cocodataset.org/val2017/000000439180.jpg", "height": 360, "width": 640, "date_captured": "2013-11-19 01:25:39", "flickr_url": "http://farm3.staticflickr.com/2831/9275116980_1d9b986e3b_z.jpg", "id": 439180}], "annotations": [{"segments_info": [{"area": 3528, "category_id": 1, "iscrowd": 0, "id": 3937500, "bbox": [282, 207, 48, 149]}, {"area": 3751, "category_id": 1, "iscrowd": 0, "id": 4260062, "bbox": [420, 183, 49, 173]}, {"area": 2301, "category_id": 1, "iscrowd": 0, "id": 2035955, "bbox": [271, 118, 53, 141]}, {"area": 4076, "category_id": 1, "iscrowd": 0, "id": 4325578, "bbox": [337, 208, 54, 149]}, {"area": 2931, "category_id": 1, "iscrowd": 0, "id": 2628072, "bbox": [47, 221, 36, 138]}, {"area": 2969, "category_id": 1, "iscrowd": 0, "id": 2822390, "bbox": [436, 125, 70, 151]}, {"area": 3557, "category_id": 1, "iscrowd": 0, "id": 5186532, "bbox": [241, 183, 45, 171]}, {"area": 477, "category_id": 1, "iscrowd": 0, "id": 4917453, "bbox": [618, 248, 22, 68]}, {"area": 3968, "category_id": 1, "iscrowd": 0, "id": 3476419, "bbox": [470, 211, 63, 142]}, {"area": 153, "category_id": 1, "iscrowd": 0, "id": 3997935, "bbox": [0, 242, 9, 44]}, {"area": 3330, "category_id": 1, "iscrowd": 0, "id": 2098642, "bbox": [153, 217, 58, 140]}, {"area": 355, "category_id": 1, "iscrowd": 0, "id": 5314498, "bbox": [586, 229, 13, 39]}, {"area": 636, "category_id": 1, "iscrowd": 0, "id": 4721614, "bbox": [270, 124, 26, 61]}, {"area": 24295, "category_id": 1, "iscrowd": 1, "id": 3153362, "bbox": [75, 111, 517, 262]}, {"area": 175, "category_id": 37, "iscrowd": 0, "id": 16757838, "bbox": [360, 116, 16, 17]}, {"area": 130762, "category_id": 184, "iscrowd": 0, "id": 2330219, "bbox": [0, 0, 640, 263]}, {"area": 8204, "category_id": 187, "iscrowd": 0, "id": 11829830, "bbox": [440, 0, 200, 103]}, {"area": 75100, "category_id": 193, "iscrowd": 0, "id": 10025880, "bbox": [0, 241, 640, 186]}], "file_name": "000000142238.png", "image_id": 142238}, {"segments_info": [{"area": 3512, "category_id": 1, "iscrowd": 0, "id": 3937500, "bbox": [200, 160, 53, 140]}, {"area": 797, "category_id": 1, "iscrowd": 0, "id": 2823390, "bbox": [520, 160, 43, 77]}, {"area": 538, "category_id": 1, "iscrowd": 0, "id": 5768384, "bbox": [0, 205, 13, 63]}, {"area": 2282, "category_id": 1, "iscrowd": 0, "id": 2626802, "bbox": [339, 151, 52, 70]}, {"area": 1005, "category_id": 1, "iscrowd": 0, "id": 3870421, "bbox": [164, 160, 36, 44]}, {"area": 2493, "category_id": 1, "iscrowd": 0, "id": 5119213, "bbox": [270, 153, 71, 113]}, {"area": 1250, "category_id": 1, "iscrowd": 0, "id": 3604684, "bbox": [577, 141, 38, 79]}, {"area": 2955, "category_id": 1, "iscrowd": 0, "id": 4070338, "bbox": [388, 147, 84, 124]}, {"area": 496, "category_id": 1, "iscrowd": 0, "id": 5777861, "bbox": [255, 172, 24, 48]}, {"area": 1521, "category_id": 1, "iscrowd": 0, "id": 3608258, "bbox": [470, 150, 48, 92]}, {"area": 2202, "category_id": 1, "iscrowd": 0, "id": 4725734, "bbox": [87, 163, 71, 116]}, {"area": 1222, "category_id": 1, "iscrowd": 0, "id": 4858845, "bbox": [233, 173, 35, 90]}, {"area": 672, "category_id": 1, "iscrowd": 0, "id": 4596217, "bbox": [16, 205, 19, 63]}, {"area": 7839, "category_id": 1, "iscrowd": 1, "id": 5120499, "bbox": [272, 142, 368, 178]}, {"area": 1680, "category_id": 8, "iscrowd": 0, "id": 4587520, "bbox": [187, 145, 86, 49]}, {"area": 5791, "category_id": 8, "iscrowd": 0, "id": 6094848, "bbox": [36, 159, 143, 77]}, {"area": 4363, "category_id": 19, "iscrowd": 0, "id": 16758454, "bbox": [175, 203, 57, 152]}, {"area": 573, "category_id": 19, "iscrowd": 0, "id": 16762012, "bbox": [555, 182, 22, 54]}, {"area": 96, "category_id": 19, "iscrowd": 0, "id": 15714220, "bbox": [264, 206, 11, 21]}, {"area": 2505, "category_id": 19, "iscrowd": 0, "id": 16752587, "bbox": [156, 192, 33, 146]}, {"area": 4160, "category_id": 19, "iscrowd": 0, "id": 16306338, "bbox": [273, 184, 52, 164]}, {"area": 1904, "category_id": 19, "iscrowd": 0, "id": 16756904, "bbox": [513, 194, 40, 84]}, {"area": 2742, "category_id": 19, "iscrowd": 0, "id": 16762031, "bbox": [462, 187, 45, 103]}, {"area": 5333, "category_id": 19, "iscrowd": 0, "id": 15444403, "bbox": [398, 181, 64, 157]}, {"area": 3612, "category_id": 19, "iscrowd": 0, "id": 14995098, "bbox": [339, 213, 42, 124]}, {"area": 4508, "category_id": 19, "iscrowd": 0, "id": 16761532, "bbox": [92, 204, 54, 144]}, {"area": 1511, "category_id": 19, "iscrowd": 0, "id": 16762578, "bbox": [578, 179, 30, 95]}, {"area": 421, "category_id": 19, "iscrowd": 1, "id": 16175517, "bbox": [605, 184, 16, 37]}, {"area": 11074, "category_id": 125, "iscrowd": 0, "id": 11881084, "bbox": [58, 192, 345, 168]}, {"area": 91045, "category_id": 184, "iscrowd": 0, "id": 2330219, "bbox": [0, 0, 640, 243]}, {"area": 12912, "category_id": 187, "iscrowd": 0, "id": 11829830, "bbox": [121, 0, 519, 79]}, {"area": 40197, "category_id": 193, "iscrowd": 0, "id": 10025880, "bbox": [0, 219, 640, 141]}], "file_name": "000000439180.png", "image_id": 439180}], "categories": [{"supercategory": "person", "color": [220, 20, 60], "isthing": 1, "id": 1, "name": "person"}, {"supercategory": "vehicle", "color": [119, 11, 32], "isthing": 1, "id": 2, "name": "bicycle"}, {"supercategory": "vehicle", "color": [0, 0, 142], "isthing": 1, "id": 3, "name": "car"}, {"supercategory": "vehicle", "color": [0, 0, 230], "isthing": 1, "id": 4, "name": "motorcycle"}, {"supercategory": "vehicle", "color": [106, 0, 228], "isthing": 1, "id": 5, "name": "airplane"}, {"supercategory": "vehicle", "color": [0, 60, 100], "isthing": 1, "id": 6, "name": "bus"}, {"supercategory": "vehicle", "color": [0, 80, 100], "isthing": 1, "id": 7, "name": "train"}, {"supercategory": "vehicle", "color": [0, 0, 70], "isthing": 1, "id": 8, "name": "truck"}, {"supercategory": "vehicle", "color": [0, 0, 192], "isthing": 1, "id": 9, "name": "boat"}, {"supercategory": "outdoor", "color": [250, 170, 30], "isthing": 1, "id": 10, "name": "traffic light"}, {"supercategory": "outdoor", "color": [100, 170, 30], "isthing": 1, "id": 11, "name": "fire hydrant"}, {"supercategory": "outdoor", "color": [220, 220, 0], "isthing": 1, "id": 13, "name": "stop sign"}, {"supercategory": "outdoor", "color": [175, 116, 175], "isthing": 1, "id": 14, "name": "parking meter"}, {"supercategory": "outdoor", "color": [250, 0, 30], "isthing": 1, "id": 15, "name": "bench"}, {"supercategory": "animal", "color": [165, 42, 42], "isthing": 1, "id": 16, "name": "bird"}, {"supercategory": "animal", "color": [255, 77, 255], "isthing": 1, "id": 17, "name": "cat"}, {"supercategory": "animal", "color": [0, 226, 252], "isthing": 1, "id": 18, "name": "dog"}, {"supercategory": "animal", "color": [182, 182, 255], "isthing": 1, "id": 19, "name": "horse"}, {"supercategory": "animal", "color": [0, 82, 0], "isthing": 1, "id": 20, "name": "sheep"}, {"supercategory": "animal", "color": [120, 166, 157], "isthing": 1, "id": 21, "name": "cow"}, {"supercategory": "animal", "color": [110, 76, 0], "isthing": 1, "id": 22, "name": "elephant"}, {"supercategory": "animal", "color": [174, 57, 255], "isthing": 1, "id": 23, "name": "bear"}, {"supercategory": "animal", "color": [199, 100, 0], "isthing": 1, "id": 24, "name": "zebra"}, {"supercategory": "animal", "color": [72, 0, 118], "isthing": 1, "id": 25, "name": "giraffe"}, {"supercategory": "accessory", "color": [255, 179, 240], "isthing": 1, "id": 27, "name": "backpack"}, {"supercategory": "accessory", "color": [0, 125, 92], "isthing": 1, "id": 28, "name": "umbrella"}, {"supercategory": "accessory", "color": [209, 0, 151], "isthing": 1, "id": 31, "name": "handbag"}, {"supercategory": "accessory", "color": [188, 208, 182], "isthing": 1, "id": 32, "name": "tie"}, {"supercategory": "accessory", "color": [0, 220, 176], "isthing": 1, "id": 33, "name": "suitcase"}, {"supercategory": "sports", "color": [255, 99, 164], "isthing": 1, "id": 34, "name": "frisbee"}, {"supercategory": "sports", "color": [92, 0, 73], "isthing": 1, "id": 35, "name": "skis"}, {"supercategory": "sports", "color": [133, 129, 255], "isthing": 1, "id": 36, "name": "snowboard"}, {"supercategory": "sports", "color": [78, 180, 255], "isthing": 1, "id": 37, "name": "sports ball"}, {"supercategory": "sports", "color": [0, 228, 0], "isthing": 1, "id": 38, "name": "kite"}, {"supercategory": "sports", "color": [174, 255, 243], "isthing": 1, "id": 39, "name": "baseball bat"}, {"supercategory": "sports", "color": [45, 89, 255], "isthing": 1, "id": 40, "name": "baseball glove"}, {"supercategory": "sports", "color": [134, 134, 103], "isthing": 1, "id": 41, "name": "skateboard"}, {"supercategory": "sports", "color": [145, 148, 174], "isthing": 1, "id": 42, "name": "surfboard"}, {"supercategory": "sports", "color": [255, 208, 186], "isthing": 1, "id": 43, "name": "tennis racket"}, {"supercategory": "kitchen", "color": [197, 226, 255], "isthing": 1, "id": 44, "name": "bottle"}, {"supercategory": "kitchen", "color": [171, 134, 1], "isthing": 1, "id": 46, "name": "wine glass"}, {"supercategory": "kitchen", "color": [109, 63, 54], "isthing": 1, "id": 47, "name": "cup"}, {"supercategory": "kitchen", "color": [207, 138, 255], "isthing": 1, "id": 48, "name": "fork"}, {"supercategory": "kitchen", "color": [151, 0, 95], "isthing": 1, "id": 49, "name": "knife"}, {"supercategory": "kitchen", "color": [9, 80, 61], "isthing": 1, "id": 50, "name": "spoon"}, {"supercategory": "kitchen", "color": [84, 105, 51], "isthing": 1, "id": 51, "name": "bowl"}, {"supercategory": "food", "color": [74, 65, 105], "isthing": 1, "id": 52, "name": "banana"}, {"supercategory": "food", "color": [166, 196, 102], "isthing": 1, "id": 53, "name": "apple"}, {"supercategory": "food", "color": [208, 195, 210], "isthing": 1, "id": 54, "name": "sandwich"}, {"supercategory": "food", "color": [255, 109, 65], "isthing": 1, "id": 55, "name": "orange"}, {"supercategory": "food", "color": [0, 143, 149], "isthing": 1, "id": 56, "name": "broccoli"}, {"supercategory": "food", "color": [179, 0, 194], "isthing": 1, "id": 57, "name": "carrot"}, {"supercategory": "food", "color": [209, 99, 106], "isthing": 1, "id": 58, "name": "hot dog"}, {"supercategory": "food", "color": [5, 121, 0], "isthing": 1, "id": 59, "name": "pizza"}, {"supercategory": "food", "color": [227, 255, 205], "isthing": 1, "id": 60, "name": "donut"}, {"supercategory": "food", "color": [147, 186, 208], "isthing": 1, "id": 61, "name": "cake"}, {"supercategory": "furniture", "color": [153, 69, 1], "isthing": 1, "id": 62, "name": "chair"}, {"supercategory": "furniture", "color": [3, 95, 161], "isthing": 1, "id": 63, "name": "couch"}, {"supercategory": "furniture", "color": [163, 255, 0], "isthing": 1, "id": 64, "name": "potted plant"}, {"supercategory": "furniture", "color": [119, 0, 170], "isthing": 1, "id": 65, "name": "bed"}, {"supercategory": "furniture", "color": [0, 182, 199], "isthing": 1, "id": 67, "name": "dining table"}, {"supercategory": "furniture", "color": [0, 165, 120], "isthing": 1, "id": 70, "name": "toilet"}, {"supercategory": "electronic", "color": [183, 130, 88], "isthing": 1, "id": 72, "name": "tv"}, {"supercategory": "electronic", "color": [95, 32, 0], "isthing": 1, "id": 73, "name": "laptop"}, {"supercategory": "electronic", "color": [130, 114, 135], "isthing": 1, "id": 74, "name": "mouse"}, {"supercategory": "electronic", "color": [110, 129, 133], "isthing": 1, "id": 75, "name": "remote"}, {"supercategory": "electronic", "color": [166, 74, 118], "isthing": 1, "id": 76, "name": "keyboard"}, {"supercategory": "electronic", "color": [219, 142, 185], "isthing": 1, "id": 77, "name": "cell phone"}, {"supercategory": "appliance", "color": [79, 210, 114], "isthing": 1, "id": 78, "name": "microwave"}, {"supercategory": "appliance", "color": [178, 90, 62], "isthing": 1, "id": 79, "name": "oven"}, {"supercategory": "appliance", "color": [65, 70, 15], "isthing": 1, "id": 80, "name": "toaster"}, {"supercategory": "appliance", "color": [127, 167, 115], "isthing": 1, "id": 81, "name": "sink"}, {"supercategory": "appliance", "color": [59, 105, 106], "isthing": 1, "id": 82, "name": "refrigerator"}, {"supercategory": "indoor", "color": [142, 108, 45], "isthing": 1, "id": 84, "name": "book"}, {"supercategory": "indoor", "color": [196, 172, 0], "isthing": 1, "id": 85, "name": "clock"}, {"supercategory": "indoor", "color": [95, 54, 80], "isthing": 1, "id": 86, "name": "vase"}, {"supercategory": "indoor", "color": [128, 76, 255], "isthing": 1, "id": 87, "name": "scissors"}, {"supercategory": "indoor", "color": [201, 57, 1], "isthing": 1, "id": 88, "name": "teddy bear"}, {"supercategory": "indoor", "color": [246, 0, 122], "isthing": 1, "id": 89, "name": "hair drier"}, {"supercategory": "indoor", "color": [191, 162, 208], "isthing": 1, "id": 90, "name": "toothbrush"}, {"supercategory": "textile", "color": [255, 255, 128], "isthing": 0, "id": 92, "name": "banner"}, {"supercategory": "textile", "color": [147, 211, 203], "isthing": 0, "id": 93, "name": "blanket"}, {"supercategory": "building", "color": [150, 100, 100], "isthing": 0, "id": 95, "name": "bridge"}, {"supercategory": "raw-material", "color": [168, 171, 172], "isthing": 0, "id": 100, "name": "cardboard"}, {"supercategory": "furniture-stuff", "color": [146, 112, 198], "isthing": 0, "id": 107, "name": "counter"}, {"supercategory": "textile", "color": [210, 170, 100], "isthing": 0, "id": 109, "name": "curtain"}, {"supercategory": "furniture-stuff", "color": [92, 136, 89], "isthing": 0, "id": 112, "name": "door-stuff"}, {"supercategory": "floor", "color": [218, 88, 184], "isthing": 0, "id": 118, "name": "floor-wood"}, {"supercategory": "plant", "color": [241, 129, 0], "isthing": 0, "id": 119, "name": "flower"}, {"supercategory": "food-stuff", "color": [217, 17, 255], "isthing": 0, "id": 122, "name": "fruit"}, {"supercategory": "ground", "color": [124, 74, 181], "isthing": 0, "id": 125, "name": "gravel"}, {"supercategory": "building", "color": [70, 70, 70], "isthing": 0, "id": 128, "name": "house"}, {"supercategory": "furniture-stuff", "color": [255, 228, 255], "isthing": 0, "id": 130, "name": "light"}, {"supercategory": "furniture-stuff", "color": [154, 208, 0], "isthing": 0, "id": 133, "name": "mirror-stuff"}, {"supercategory": "structural", "color": [193, 0, 92], "isthing": 0, "id": 138, "name": "net"}, {"supercategory": "textile", "color": [76, 91, 113], "isthing": 0, "id": 141, "name": "pillow"}, {"supercategory": "ground", "color": [255, 180, 195], "isthing": 0, "id": 144, "name": "platform"}, {"supercategory": "ground", "color": [106, 154, 176], "isthing": 0, "id": 145, "name": "playingfield"}, {"supercategory": "ground", "color": [230, 150, 140], "isthing": 0, "id": 147, "name": "railroad"}, {"supercategory": "water", "color": [60, 143, 255], "isthing": 0, "id": 148, "name": "river"}, {"supercategory": "ground", "color": [128, 64, 128], "isthing": 0, "id": 149, "name": "road"}, {"supercategory": "building", "color": [92, 82, 55], "isthing": 0, "id": 151, "name": "roof"}, {"supercategory": "ground", "color": [254, 212, 124], "isthing": 0, "id": 154, "name": "sand"}, {"supercategory": "water", "color": [73, 77, 174], "isthing": 0, "id": 155, "name": "sea"}, {"supercategory": "furniture-stuff", "color": [255, 160, 98], "isthing": 0, "id": 156, "name": "shelf"}, {"supercategory": "ground", "color": [255, 255, 255], "isthing": 0, "id": 159, "name": "snow"}, {"supercategory": "furniture-stuff", "color": [104, 84, 109], "isthing": 0, "id": 161, "name": "stairs"}, {"supercategory": "building", "color": [169, 164, 131], "isthing": 0, "id": 166, "name": "tent"}, {"supercategory": "textile", "color": [225, 199, 255], "isthing": 0, "id": 168, "name": "towel"}, {"supercategory": "wall", "color": [137, 54, 74], "isthing": 0, "id": 171, "name": "wall-brick"}, {"supercategory": "wall", "color": [135, 158, 223], "isthing": 0, "id": 175, "name": "wall-stone"}, {"supercategory": "wall", "color": [7, 246, 231], "isthing": 0, "id": 176, "name": "wall-tile"}, {"supercategory": "wall", "color": [107, 255, 200], "isthing": 0, "id": 177, "name": "wall-wood"}, {"supercategory": "water", "color": [58, 41, 149], "isthing": 0, "id": 178, "name": "water-other"}, {"supercategory": "window", "color": [183, 121, 142], "isthing": 0, "id": 180, "name": "window-blind"}, {"supercategory": "window", "color": [255, 73, 97], "isthing": 0, "id": 181, "name": "window-other"}, {"supercategory": "plant", "color": [107, 142, 35], "isthing": 0, "id": 184, "name": "tree-merged"}, {"supercategory": "structural", "color": [190, 153, 153], "isthing": 0, "id": 185, "name": "fence-merged"}, {"supercategory": "ceiling", "color": [146, 139, 141], "isthing": 0, "id": 186, "name": "ceiling-merged"}, {"supercategory": "sky", "color": [70, 130, 180], "isthing": 0, "id": 187, "name": "sky-other-merged"}, {"supercategory": "furniture-stuff", "color": [134, 199, 156], "isthing": 0, "id": 188, "name": "cabinet-merged"}, {"supercategory": "furniture-stuff", "color": [209, 226, 140], "isthing": 0, "id": 189, "name": "table-merged"}, {"supercategory": "floor", "color": [96, 36, 108], "isthing": 0, "id": 190, "name": "floor-other-merged"}, {"supercategory": "ground", "color": [96, 96, 96], "isthing": 0, "id": 191, "name": "pavement-merged"}, {"supercategory": "solid", "color": [64, 170, 64], "isthing": 0, "id": 192, "name": "mountain-merged"}, {"supercategory": "plant", "color": [152, 251, 152], "isthing": 0, "id": 193, "name": "grass-merged"}, {"supercategory": "ground", "color": [208, 229, 228], "isthing": 0, "id": 194, "name": "dirt-merged"}, {"supercategory": "raw-material", "color": [206, 186, 171], "isthing": 0, "id": 195, "name": "paper-merged"}, {"supercategory": "food-stuff", "color": [152, 161, 64], "isthing": 0, "id": 196, "name": "food-other-merged"}, {"supercategory": "building", "color": [116, 112, 0], "isthing": 0, "id": 197, "name": "building-other-merged"}, {"supercategory": "solid", "color": [0, 114, 143], "isthing": 0, "id": 198, "name": "rock-merged"}, {"supercategory": "wall", "color": [102, 102, 156], "isthing": 0, "id": 199, "name": "wall-other-merged"}, {"supercategory": "textile", "color": [250, 141, 255], "isthing": 0, "id": 200, "name": "rug-merged"}]}
View Code

- categories是整个数据集共有的!!!

json文件解析:

共有字段:

{
"info": info,
"images": [image],
"annotations": [annotation],
"licenses": [license],
}
首先读json文件:
with open(json_file, 'r') as f:
    coco_d = json.load(f)

image_info=coco_d['image']

ann=coco_d['annotations']

category_list=coco_d['categories'] //或者读文件:with open(panoptic_coco_categories, 'r') as f: categories_list = json.load(f)

可以建立:annotations.image_id==image_info.id

另外:segment_info.id通过标注的png文件转换:

def rgb2id(color):
    if isinstance(color, np.ndarray) and len(color.shape) == 3:
        if color.dtype == np.uint8:
            color = color.astype(np.uint32)
        return color[:, :, 0] + 256 * color[:, :, 1] + 256 * 256 * color[:, :, 2]
    return color[0] + 256 * color[1] + 256 * 256 * color[2]

segmentation = np.array(
    Image.open(os.path.join(segmentations_folder, ann['file_name'])), //读png标注文件
    dtype=np.uint8
)
segmentation_id = rgb2id(segmentation)

 问题:thing的color是唯一的,但是不同实例之间id是不一样的?那ids via ids=R+G*256+B*256^2.该转换有特殊处理吧???

 -> segment_id = id_generator.get_id(ann['category_id'])的get_color()中,当相同实例时候,color会在base基础上随机调整一下;

class IdGenerator():
    def __init__(self, categories):
        self.taken_colors = set([0, 0, 0])
        self.categories = categories
        for category in self.categories.values():
            if category['isthing'] == 0:
                self.taken_colors.add(tuple(category['color']))

    def get_color(self, cat_id):
        def random_color(base, max_dist=30):
            new_color = base + np.random.randint(low=-max_dist,
                                                 high=max_dist+1,
                                                 size=3)
            return tuple(np.maximum(0, np.minimum(255, new_color)))

        category = self.categories[cat_id]
        if category['isthing'] == 0:
            return category['color']
        base_color_array = category['color']
        base_color = tuple(base_color_array)
        if base_color not in self.taken_colors:
            self.taken_colors.add(base_color)
            return base_color
        else:
            while True:
                color = random_color(base_color_array)
                if color not in self.taken_colors:
                     self.taken_colors.add(color)
                     return color

    def get_id(self, cat_id):
        color = self.get_color(cat_id)
        return rgb2id(color)

    def get_id_and_color(self, cat_id):
        color = self.get_color(cat_id)
        return rgb2id(color), color

 

 

 

 

posted @ 2018-11-26 10:54  ranjiewen  阅读(2316)  评论(0编辑  收藏  举报