1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| def get_annotations(label_list, datadir, Annotations = 'annotations', Images = 'images'): filenames = os.listdir(os.path.join(datadir, Annotations)) records = [] ct = 0 for fname in filenames: fid = fname.split('.')[0] fpath = os.path.join(datadir, Annotations, fname) img_file = os.path.join(datadir, Images, fid + '.jpg') tree = ET.parse(fpath)
if tree.find('id') is None: im_id = np.array([ct]) else: im_id = np.array([int(tree.find('id').text)])
objs = tree.findall('object') im_w = float(tree.find('size').find('width').text) im_h = float(tree.find('size').find('height').text) gt_bbox = np.zeros((len(objs), 4), dtype=np.float32) gt_class = np.zeros((len(objs), ), dtype=np.int32) is_crowd = np.zeros((len(objs), ), dtype=np.int32) difficult = np.zeros((len(objs), ), dtype=np.int32) for i, obj in enumerate(objs): cname = obj.find('name').text gt_class[i] = label_list[cname] _difficult = int(obj.find('difficult').text) x1 = float(obj.find('bndbox').find('xmin').text) y1 = float(obj.find('bndbox').find('ymin').text) x2 = float(obj.find('bndbox').find('xmax').text) y2 = float(obj.find('bndbox').find('ymax').text) x1 = max(0, x1) y1 = max(0, y1) x2 = min(im_w - 1, x2) y2 = min(im_h - 1, y2) gt_bbox[i] = [(x1+x2)/2.0 , (y1+y2)/2.0, x2-x1+1., y2-y1+1.] is_crowd[i] = 0 difficult[i] = _difficult
voc_rec = { 'im_file': img_file, 'im_id': im_id, 'h': im_h, 'w': im_w, 'is_crowd': is_crowd, 'gt_class': gt_class, 'gt_bbox': gt_bbox, 'gt_poly': [], 'difficult': difficult } if len(objs) != 0: records.append(voc_rec) ct += 1 return records
''' record格式: {'im_file': 'Car2024\\Images\\block1.jpg', 'im_id': array([0]), 'h': 240.0, 'w': 320.0, 'is_crowd': array([0, 0, 0]), 'gt_class': array([11, 8, 8]), 'gt_bbox': array([[110.5, 21.5, 36. , 30. ], [ 99.5, 70. , 32. , 41. ], [266. , 164.5, 49. , 70. ]], dtype=float32), 'gt_poly': [], 'difficult': array([0, 0, 0])} '''
label_list = {'spy': 0, 'safety': 1, 'bridge': 2, 'danger': 3, 'tumble': 4, 'thief': 5, 'evil': 6, 'bomb': 7, 'cone': 8, 'crosswalk': 9, 'prop': 10, 'block': 11, 'patient': 12} records = get_annotations(label_list, train_dir, Annotations = 'Annotations', Images = 'Images')
|