本文整理汇总了Python中pycocotools.coco.COCO.annToMask方法的典型用法代码示例。如果您正苦于以下问题:Python COCO.annToMask方法的具体用法?Python COCO.annToMask怎么用?Python COCO.annToMask使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycocotools.coco.COCO
的用法示例。
在下文中一共展示了COCO.annToMask方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1:
# 需要导入模块: from pycocotools.coco import COCO [as 别名]
# 或者: from pycocotools.coco.COCO import annToMask [as 别名]
img_path = os.path.join(val_images_dir, "%012d.jpg" % img_id)
mask_miss_path = os.path.join(val_masks_dir, "mask_miss_%012d.png" % img_id)
mask_all_path = os.path.join(val_masks_dir, "mask_all_%012d.png" % img_id)
img = cv2.imread(img_path)
h, w, c = img.shape
mask_all = np.zeros((h, w), dtype=np.uint8)
mask_miss = np.zeros((h, w), dtype=np.uint8)
flag = 0
for p in img_anns:
seg = p["segmentation"]
if p["iscrowd"] == 1:
mask_crowd = coco.annToMask(p)
temp = np.bitwise_and(mask_all, mask_crowd)
mask_crowd = mask_crowd - temp
flag += 1
continue
else:
mask = coco.annToMask(p)
mask_all = np.bitwise_or(mask, mask_all)
if p["num_keypoints"] <= 0:
mask_miss = np.bitwise_or(mask, mask_miss)
if flag<1:
mask_miss = np.logical_not(mask_miss)
elif flag == 1:
示例2: CocoPoseGenerator
# 需要导入模块: from pycocotools.coco import COCO [as 别名]
# 或者: from pycocotools.coco.COCO import annToMask [as 别名]
class CocoPoseGenerator(object):
def __init__(self, args, json_dir=JOSN_DIR, mask_dir=MASK_DIR, image_dir=IMAGE_DIR):
self.args = args
self.json_dir = os.path.join(self.args.root_dir, json_dir)
if not os.path.exists(self.json_dir):
os.makedirs(self.json_dir)
self.image_dir = os.path.join(self.args.root_dir, image_dir)
if not os.path.exists(self.image_dir):
os.makedirs(self.image_dir)
self.mask_dir = os.path.join(self.args.root_dir, mask_dir)
if not os.path.exists(self.mask_dir):
os.makedirs(self.mask_dir)
self.coco = COCO(self.args.anno_file)
self.img_ids = list(self.coco.imgs.keys())
def generate_label(self):
for i, img_id in enumerate(self.img_ids):
json_dict = dict()
ann_ids = self.coco.getAnnIds(imgIds=img_id)
img_anns = self.coco.loadAnns(ann_ids)
num_persons = len(img_anns)
filename = self.coco.imgs[img_id]['file_name']
width = self.coco.imgs[img_id]['width']
height = self.coco.imgs[img_id]['height']
json_dict['height'] = height
json_dict['width'] = width
mask_list = list()
persons = list()
person_centers = list()
for p in range(num_persons):
if img_anns[p]['num_keypoints'] < 5 or img_anns[p]['area'] < 32 * 32:
mask_list.append(p)
continue
kpt = img_anns[p]['keypoints']
dic = dict()
# person center
person_center = [img_anns[p]['bbox'][0] + img_anns[p]['bbox'][2] / 2.0,
img_anns[p]['bbox'][1] + img_anns[p]['bbox'][3] / 2.0]
scale = img_anns[p]['bbox'][3] / self.args.input_size
# skip this person if the distance to exiting person is too small
flag = 0
for pc in person_centers:
dis = math.sqrt((person_center[0] - pc[0]) * (person_center[0] - pc[0])
+ (person_center[1] - pc[1]) * (person_center[1] - pc[1]))
if dis < pc[2] * 0.3:
flag = 1
break
if flag == 1:
mask_list.append(p)
continue
dic['bbox'] = img_anns[p]['bbox']
dic['objpos'] = person_center
dic['keypoints'] = np.zeros((17, 3)).tolist()
dic['scale'] = scale
for part in range(17):
dic['keypoints'][part][0] = kpt[part * 3]
dic['keypoints'][part][1] = kpt[part * 3 + 1]
# visiable is 1, unvisiable is 0 and not labeled is 2
if kpt[part * 3 + 2] == 2:
dic['keypoints'][part][2] = 1
elif kpt[part * 3 + 2] == 1:
dic['keypoints'][part][2] = 0
else:
dic['keypoints'][part][2] = 2
persons.append(dic)
person_centers.append(np.append(person_center, max(img_anns[p]['bbox'][2], img_anns[p]['bbox'][3])))
if len(persons) > 0:
persons = self.__coco_to_ours(persons)
json_dict['persons'] = persons
fw = open(os.path.join(self.json_dir, '{}.json'.format(filename.split('.')[0])), 'w')
fw.write(json.dumps(json_dict))
fw.close()
mask_all = np.zeros((height, width), dtype=np.uint8)
mask_miss = np.zeros((height, width), dtype=np.uint8)
flag = 0
for p in range(num_persons):
if img_anns[p]['iscrowd'] == 1:
mask_crowd = self.coco.annToMask(img_anns[p])
temp = np.bitwise_and(mask_all, mask_crowd)
mask_crowd = mask_crowd - temp
flag += 1
continue
else:
mask = self.coco.annToMask(img_anns[p])
#.........这里部分代码省略.........