CoolFace
Apppublic

k20hcmus/FishEye8K

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
3likes
coco_utils.py109 linesDownload Raw Back to utils
1import cv22 3from pycocotools.coco import COCO4from pycocotools import mask as maskUtils5 6# coco id: https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/7all_instances_ids = [8    1, 2, 3, 4, 5, 6, 7, 8, 9, 10,9    11, 13, 14, 15, 16, 17, 18, 19, 20,10    21, 22, 23, 24, 25, 27, 28,11    31, 32, 33, 34, 35, 36, 37, 38, 39, 40,12    41, 42, 43, 44, 46, 47, 48, 49, 50,13    51, 52, 53, 54, 55, 56, 57, 58, 59, 60,14    61, 62, 63, 64, 65, 67, 70,15    72, 73, 74, 75, 76, 77, 78, 79, 80,16    81, 82, 84, 85, 86, 87, 88, 89, 90,17]18 19all_stuff_ids = [20    92, 93, 94, 95, 96, 97, 98, 99, 100,21    101, 102, 103, 104, 105, 106, 107, 108, 109, 110,22    111, 112, 113, 114, 115, 116, 117, 118, 119, 120,23    121, 122, 123, 124, 125, 126, 127, 128, 129, 130,24    131, 132, 133, 134, 135, 136, 137, 138, 139, 140,25    141, 142, 143, 144, 145, 146, 147, 148, 149, 150,26    151, 152, 153, 154, 155, 156, 157, 158, 159, 160,27    161, 162, 163, 164, 165, 166, 167, 168, 169, 170,28    171, 172, 173, 174, 175, 176, 177, 178, 179, 180,29    181, 182,30    # other31    183,32    # unlabeled33    0,34]35 36# panoptic id: https://github.com/cocodataset/panopticapi/blob/master/panoptic_coco_categories.json37panoptic_stuff_ids = [38    92, 93, 95, 100,39    107, 109,40    112, 118, 119,41    122, 125, 128, 130,42    133, 138,43    141, 144, 145, 147, 148, 149,44    151, 154, 155, 156, 159,45    161, 166, 168,46    171, 175, 176, 177, 178, 180,47    181, 184, 185, 186, 187, 188, 189, 190,48    191, 192, 193, 194, 195, 196, 197, 198, 199, 200,49    # unlabeled50    0,51]52 53def getCocoIds(name = 'semantic'):54    if 'instances' == name:55        return all_instances_ids56    elif 'stuff' == name:57        return all_stuff_ids58    elif 'panoptic' == name:59        return all_instances_ids + panoptic_stuff_ids60    else: # semantic61        return all_instances_ids + all_stuff_ids62 63def getMappingId(index, name = 'semantic'):64    ids = getCocoIds(name = name)65    return ids[index]66 67def getMappingIndex(id, name = 'semantic'):68    ids = getCocoIds(name = name)69    return ids.index(id)70 71# convert ann to rle encoded string72def annToRLE(ann, img_size):73    h, w = img_size74    segm = ann['segmentation']75    if list == type(segm):76        # polygon -- a single object might consist of multiple parts77        # we merge all parts into one mask rle code78        rles = maskUtils.frPyObjects(segm, h, w)79        rle = maskUtils.merge(rles)80    elif list == type(segm['counts']):81        # uncompressed RLE82        rle = maskUtils.frPyObjects(segm, h, w)83    else:84        # rle85        rle = ann['segmentation']86    return rle87 88# decode ann to mask martix89def annToMask(ann, img_size):90    rle = annToRLE(ann, img_size)91    m = maskUtils.decode(rle)92    return m93 94# convert mask to polygans95def convert_to_polys(mask):96    # opencv 3.297    contours, hierarchy = cv2.findContours((mask).astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)98 99    # before opencv 3.2100    # contours, hierarchy = cv2.findContours((mask).astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)101 102    segmentation = []103    for contour in contours:104        contour = contour.flatten().tolist()105        if 4 < len(contour):106            segmentation.append(contour)107 108    return segmentation109