Ejemplo n.º 1
0
def processing(args):

    ann_path = args.ann_path
    json_path = args.json_path
    mask_dir = args.mask_dir
    
    coco = COCO(ann_path)
    ids = list(coco.imgs.keys())
    lists = []
    
    flielist_fp = open(args.filelist_path, 'w')
    masklist_fp = open(args.masklist_path, 'w')
    
    for i, img_id in enumerate(ids):
        ann_ids = coco.getAnnIds(imgIds=img_id)
        img_anns = coco.loadAnns(ann_ids)
    
        numPeople = len(img_anns)
        name = coco.imgs[img_id]['file_name']
        height = coco.imgs[img_id]['height']
        width = coco.imgs[img_id]['width']
    
        persons = []
        person_centers = []
    
        for p in range(numPeople):
    
            if img_anns[p]['num_keypoints'] < 5 or img_anns[p]['area'] < 32 * 32:
                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] / 368.0
    
            # 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:
                continue
            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:
            filelist_fp.write(name + '\n')
            info = dict()
            info['filename'] = name
            info['info'] = []
            cnt = 1
            for person in persons:
                dic = dict()
                dic['pos'] = person['objpos']
                dic['keypoints'] = np.zeros((18,3)).tolist()
                dic['scale'] = person['scale']
                for i in range(17):
                    dic['keypoints'][COCO_TO_OURS[i]][0] = person['keypoints'][i][0]
                    dic['keypoints'][COCO_TO_OURS[i]][1] = person['keypoints'][i][1]
                    dic['keypoints'][COCO_TO_OURS[i]][2] = person['keypoints'][i][2]
                dic['keypoints'][1][0] = (person['keypoints'][5][0] + person['keypoints'][6][0]) * 0.5
                dic['keypoints'][1][1] = (person['keypoints'][5][1] + person['keypoints'][6][1]) * 0.5
                if person['keypoints'][5][2] == person['keypoints'][6][2]:
                    dic['keypoints'][1][2] = person['keypoints'][5][2]
                elif person['keypoints'][5][2] == 2 or person['keypoints'][6][2] == 2:
                    dic['keypoints'][1][2] = 2
                else:
                    dic['keypoints'][1][2] = 0
                info['info'].append(dic)
            lists.append(info)
            
            mask_all = np.zeros((height, width), dtype=np.uint8)
            mask_miss = np.zeros((height, width), dtype=np.uint8)
            flag = 0
            for p in img_anns:
                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:
                mask_miss = np.logical_not(np.bitwise_or(mask_miss, mask_crowd))
                mask_all = np.bitwise_or(mask_all, mask_crowd)
            else:
                raise Exception('crowd segments > 1')
            np.save(os.path.join(mask_dir, name.split('.')[0] + '.npy'), mask_miss)
            masklist_fp.write(os.path.join(mask_dir, name.split('.')[0] + '.npy') + '\n')
        if i % 1000 == 0:
            print "Processed {} of {}".format(i, len(ids))
    
    masklist_fp.close()
    filelist_fp.close()
    print 'write json file'
    
    fp = open(json_path, 'w')
    fp.write(json.dumps(lists))
    fp.close()
    
    print 'done!'
Ejemplo n.º 2
0
class COCOTest():
    def __init__(self, image_set, year):
        #imdb.__init__(self, 'coco_' + year + '_' + image_set)
        # COCO specific config options
        self.config = {
            'top_k': 2000,
            'use_salt': True,
            'cleanup': True,
            'crowd_thresh': 0.7,
            'min_size': 2
        }
        # name, paths
        self._year = year
        self._image_set = image_set
        self._data_path = osp.join(cfg.DATA_DIR, 'coco')
        # load COCO API, classes, class <-> id mappings
        self._COCO = COCO(self._get_ann_file())
        cats = self._COCO.loadCats(self._COCO.getCatIds())
        #print self._COCO.anns[185487]
        # anns = [self._COCO.anns[185487]]
        #self._COCO.showAnns(anns)
        #image_ids = self._COCO.getImgIds()
        #print image_ids
        self.test()

    def _get_ann_file(self):
        # prefix = 'instances' if self._image_set.find('test') == -1 \
        #                      else 'image_info'
        # return osp.join(self._data_path, 'annotations',
        #                 prefix + '_' + self._image_set + self._year + '.json')
        return osp.join(self._data_path, 'annotations',
                        'person_keypoints_train2014.json')

    def get_img_file(self, im_ann):
        return osp.join(self._data_path, 'train2014', im_ann['file_name'])

    def test(self):
        image_ids = self._COCO.getImgIds()
        # print image_ids,'\n,len:',len(image_ids)
        for i in xrange(len(image_ids)):
            im_ann = self._COCO.loadImgs(image_ids[i])[0]
            print '\n:', i
            width = im_ann['width']
            height = im_ann['height']
            # print im_ann
            # print self.get_img_file(im_ann)

            annIds = self._COCO.getAnnIds(imgIds=image_ids[i], iscrowd=None)
            objs = self._COCO.loadAnns(annIds)
            # print annIds,objs
            im = cv2.imread(self.get_img_file(im_ann))
            # Sanitize bboxes -- some are invalid
            valid_objs = []
            im = im[:, :, (2, 1, 0)]
            im[:, :, :] = (0, 0, 0)
            #im = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
            fig, ax = plt.subplots(figsize=(12, 12))
            for obj in objs:
                print obj
                mask = self._COCO.annToMask(obj)
                #im[mask==0,:]=(0,0,0);
                im[mask == 1, :] = (255, 255, 255)
                # for i in range(width):
                #     for j in range(height):
                #         if(mask[i][j] == 0)

                x1 = np.max((0, obj['bbox'][0]))
                y1 = np.max((0, obj['bbox'][1]))
                x2 = np.min((width - 1, x1 + np.max((0, obj['bbox'][2] - 1))))
                y2 = np.min((height - 1, y1 + np.max((0, obj['bbox'][3] - 1))))
                # print mask.shape,x1,y1,x2,y2,width,height
                # print 'mask.shape[0]:mask_shape[1]',mask.shape[0],mask.shape[1]
                start_h = np.round(np.max((1, y1))).astype(np.int)
                end_h = np.round(np.min((height, y2))).astype(np.int)
                start_w = np.round(np.max((1, x1))).astype(np.int)
                end_w = np.round(np.min((width, x2))).astype(np.int)
                cropped_mask = mask[start_h:end_h, start_w:end_w]
                # print cropped_mask.shape
                # resize_mask = cv2.resize(cropped_mask, (28, 28), interpolation=cv2.INTER_NEAREST)
                # print resize_mask.shape,resize_mask
                if 'keypoints' in obj:
                    print 'category_id', obj['category_id']
                    print '\nkeypoints', obj['keypoints']
                    print '\nlens', obj['num_keypoints']