├── README.md ├── Draw_box_fromxml.py ├── Labels.txt └── json2xml.py /README.md: -------------------------------------------------------------------------------- 1 | 2 | MS COCO 数据集主页:http://mscoco.org/ 3 | 4 | Github:https://github.com/huipengzhang/cocoapi 5 | 6 | 关于 API 更多的细节在网站: http://mscoco.org/dataset/#download 7 | 8 | 执行:python json2xml.py 9 | -------------------------------------------------------------------------------- /Draw_box_fromxml.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | import cv2 3 | import xml.etree.cElementTree as ET 4 | 5 | image = './0.jpg' 6 | x_file = './0.xml' 7 | 8 | 9 | image = cv2.imread(image) 10 | #cv2.imshow(image) 11 | 12 | tree = ET.parse(x_file) 13 | root = tree.getroot() 14 | 15 | for child in root[4][4]: 16 | print(child.text) 17 | 18 | 19 | xmin = root[5][4][0].text 20 | ymin = root[5][4][1].text 21 | xmax = root[5][4][2].text 22 | ymax = root[5][4][3].text 23 | 24 | print(root[4][4][2].text) 25 | 26 | xy = [int(xmin), int(ymin), int(xmax), int(ymax)] 27 | 28 | r_image = cv2.rectangle(image, (xy[0], xy[1]), (xy[2], xy[3]), (255, 0, 0), 2) # kare çiz 29 | 30 | cv2.imshow("rectangle", r_image) 31 | 32 | 33 | cv2.waitKey(0) 34 | cv2.imwrite('test.jpg', r_image) 35 | cv2.destroyAllWindows() 36 | -------------------------------------------------------------------------------- /Labels.txt: -------------------------------------------------------------------------------- 1 | 0: unlabeled 2 | 1: person 3 | 2: bicycle 4 | 3: car 5 | 4: motorcycle 6 | 5: airplane 7 | 6: bus 8 | 7: train 9 | 8: truck 10 | 9: boat 11 | 10: traffic light 12 | 11: fire hydrant 13 | 12: street sign 14 | 13: stop sign 15 | 14: parking meter 16 | 15: bench 17 | 16: bird 18 | 17: cat 19 | 18: dog 20 | 19: horse 21 | 20: sheep 22 | 21: cow 23 | 22: elephant 24 | 23: bear 25 | 24: zebra 26 | 25: giraffe 27 | 26: hat 28 | 27: backpack 29 | 28: umbrella 30 | 29: shoe 31 | 30: eye glasses 32 | 31: handbag 33 | 32: tie 34 | 33: suitcase 35 | 34: frisbee 36 | 35: skis 37 | 36: snowboard 38 | 37: sports ball 39 | 38: kite 40 | 39: baseball bat 41 | 40: baseball glove 42 | 41: skateboard 43 | 42: surfboard 44 | 43: tennis racket 45 | 44: bottle 46 | 45: plate 47 | 46: wine glass 48 | 47: cup 49 | 48: fork 50 | 49: knife 51 | 50: spoon 52 | 51: bowl 53 | 52: banana 54 | 53: apple 55 | 54: sandwich 56 | 55: orange 57 | 56: broccoli 58 | 57: carrot 59 | 58: hot dog 60 | 59: pizza 61 | 60: donut 62 | 61: cake 63 | 62: chair 64 | 63: couch 65 | 64: potted plant 66 | 65: bed 67 | 66: mirror 68 | 67: dining table 69 | 68: window 70 | 69: desk 71 | 70: toilet 72 | 71: door 73 | 72: tv 74 | 73: laptop 75 | 74: mouse 76 | 75: remote 77 | 76: keyboard 78 | 77: cell phone 79 | 78: microwave 80 | 79: oven 81 | 80: toaster 82 | 81: sink 83 | 82: refrigerator 84 | 83: blender 85 | 84: book 86 | 85: clock 87 | 86: vase 88 | 87: scissors 89 | 88: teddy bear 90 | 89: hair drier 91 | 90: toothbrush 92 | 91: hair brush 93 | 92: banner 94 | 93: blanket 95 | 94: branch 96 | 95: bridge 97 | 96: building-other 98 | 97: bush 99 | 98: cabinet 100 | 99: cage 101 | 100: cardboard 102 | 101: carpet 103 | 102: ceiling-other 104 | 103: ceiling-tile 105 | 104: cloth 106 | 105: clothes 107 | 106: clouds 108 | 107: counter 109 | 108: cupboard 110 | 109: curtain 111 | 110: desk-stuff 112 | 111: dirt 113 | 112: door-stuff 114 | 113: fence 115 | 114: floor-marble 116 | 115: floor-other 117 | 116: floor-stone 118 | 117: floor-tile 119 | 118: floor-wood 120 | 119: flower 121 | 120: fog 122 | 121: food-other 123 | 122: fruit 124 | 123: furniture-other 125 | 124: grass 126 | 125: gravel 127 | 126: ground-other 128 | 127: hill 129 | 128: house 130 | 129: leaves 131 | 130: light 132 | 131: mat 133 | 132: metal 134 | 133: mirror-stuff 135 | 134: moss 136 | 135: mountain 137 | 136: mud 138 | 137: napkin 139 | 138: net 140 | 139: paper 141 | 140: pavement 142 | 141: pillow 143 | 142: plant-other 144 | 143: plastic 145 | 144: platform 146 | 145: playingfield 147 | 146: railing 148 | 147: railroad 149 | 148: river 150 | 149: road 151 | 150: rock 152 | 151: roof 153 | 152: rug 154 | 153: salad 155 | 154: sand 156 | 155: sea 157 | 156: shelf 158 | 157: sky-other 159 | 158: skyscraper 160 | 159: snow 161 | 160: solid-other 162 | 161: stairs 163 | 162: stone 164 | 163: straw 165 | 164: structural-other 166 | 165: table 167 | 166: tent 168 | 167: textile-other 169 | 168: towel 170 | 169: tree 171 | 170: vegetable 172 | 171: wall-brick 173 | 172: wall-concrete 174 | 173: wall-other 175 | 174: wall-panel 176 | 175: wall-stone 177 | 176: wall-tile 178 | 177: wall-wood 179 | 178: water-other 180 | 179: waterdrops 181 | 180: window-blind 182 | 181: window-other 183 | 182: wood -------------------------------------------------------------------------------- /json2xml.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | ''' 3 | Read annotations list 4 | 5 | for ant in annotations_list: 6 | if cat in cats: 7 | get imname 8 | if imnum.jpg in impath: 9 | add object to imnum.xml 10 | else: 11 | copy imname.jpg as imnum.jpg to impath 12 | make imnum.xml 13 | add object to imnum.xml 14 | 15 | TO DO: make txt files as well as xml 16 | ''' 17 | import os 18 | import json 19 | import cv2 20 | from lxml import etree 21 | import xml.etree.cElementTree as ET 22 | import time 23 | 24 | id_list = [1,2,3,18,19,62,73,74,76,84] 25 | names_list = ['person', 'bicycle','car','dog','horse','chair','laptop','mouse','keyboard','book'] 26 | im_ext = 'jpg' 27 | COCO_images = 'F:/cocoapi/train2017/' 28 | Json_addr = 'F:/cocoapi/annotations2017/instances_train2017.json' 29 | im_num = 0 30 | ob_count = 0 31 | im_pairs = dict() 32 | 33 | main_dir = '_'.join(names_list) 34 | if not os.path.isdir(main_dir): 35 | os.mkdir(main_dir) 36 | xml_dir = os.path.join(main_dir, 'annotations_xml') 37 | if not os.path.isdir(xml_dir): 38 | os.mkdir(xml_dir) 39 | 40 | im_dir = os.path.join(main_dir, 'images') 41 | if not os.path.isdir(im_dir): 42 | os.mkdir(im_dir) 43 | 44 | 45 | print('Reading JSON ...') 46 | 47 | with open(Json_addr) as json_data: 48 | annotation_list = json.load(json_data) 49 | 50 | start_time = time.time() 51 | print('--- Start Operation ---', start_time) 52 | 53 | for i in range(0, len(annotation_list["annotations"])): 54 | category_id = annotation_list["annotations"][i]["category_id"] 55 | 56 | if category_id in id_list: 57 | # print('HIT -->', im_num) 58 | cat_name = names_list[id_list.index(category_id)] 59 | im_id = (str(annotation_list["annotations"][i]["image_id"])) 60 | xmin = int(annotation_list["annotations"][i]["bbox"][0]) 61 | ymin = int(annotation_list["annotations"][i]["bbox"][1]) 62 | xmax = int(xmin+annotation_list["annotations"][i]["bbox"][2]) 63 | ymax = int(ymin+annotation_list["annotations"][i]["bbox"][3]) 64 | 65 | z = '0' 66 | for sf in range((len(im_id)), 11): # imname 12 basamaklı olması için 67 | z = z + "0" 68 | im_name = z + im_id 69 | 70 | if os.path.exists(os.path.join(im_dir, str(im_pairs.get(im_name, 'None')) + '.' + im_ext)): 71 | # ---add object to imnum.xml--- 72 | 73 | # read the xml root 74 | tree = ET.parse(os.path.join(xml_dir, str(im_pairs[im_name]) + '.xml')) 75 | root = tree.getroot() 76 | 77 | # Convert root to etree 78 | 79 | xml_str = ET.tostring(root) 80 | troot = etree.fromstring(xml_str) # etree object 81 | 82 | # create new object element 83 | ob = etree.Element('object') 84 | etree.SubElement(ob, 'name').text = cat_name 85 | etree.SubElement(ob, 'pose').text = 'Unspecified' 86 | etree.SubElement(ob, 'truncated').text = '0' 87 | etree.SubElement(ob, 'difficult').text = '0' 88 | 89 | bbox = etree.SubElement(ob, 'bndbox') 90 | etree.SubElement(bbox, 'xmin').text = str(xmin) 91 | etree.SubElement(bbox, 'ymin').text = str(ymin) 92 | etree.SubElement(bbox, 'xmax').text = str(xmax) 93 | etree.SubElement(bbox, 'ymax').text = str(ymax) 94 | 95 | # prettify the object 96 | xml_str = etree.tostring(ob, pretty_print=True) 97 | ob_pretty = etree.fromstring(xml_str) 98 | 99 | # append etree object to etree root(troot) 100 | troot.append(ob_pretty) 101 | 102 | # overwrite the old xml 103 | xml_str = etree.tostring(troot, pretty_print=True) 104 | 105 | with open(os.path.join(xml_dir, str(im_pairs[im_name]) + '.xml'), 'wb') as output: 106 | output.write(xml_str) 107 | 108 | print('--- Added {} to '.format(cat_name), str(im_pairs[im_name]) + '.xml' ' ---') 109 | 110 | else: 111 | 112 | # Copy image as im_num.jpg 113 | with open(os.path.join(COCO_images, im_name + '.' + im_ext), 'rb') as rf: 114 | with open(os.path.join(im_dir, str(im_num) + '.' + im_ext), 'wb') as wf: 115 | for line in rf: 116 | wf.write(line) 117 | # make imnum.xml 118 | 119 | # -get imsize(widht, height, depth) 120 | 121 | # Resimlerin olduğu klasör 122 | im_cv2 = cv2.imread(os.path.join(COCO_images, im_name + '.' + im_ext)) 123 | height, width, depth = im_cv2.shape 124 | 125 | # Form the file 126 | 127 | annotation = ET.Element('annotation') 128 | ET.SubElement(annotation, 'folder').text = im_dir 129 | ET.SubElement(annotation, 'filename').text = str(im_num) + '.' + im_ext 130 | ET.SubElement(annotation, 'segmented').text = '0' 131 | size = ET.SubElement(annotation, 'size') 132 | ET.SubElement(size, 'width').text = str(width) 133 | ET.SubElement(size, 'height').text = str(height) 134 | ET.SubElement(size, 'depth').text = str(depth) 135 | 136 | ob = ET.SubElement(annotation, 'object') 137 | ET.SubElement(ob, 'name').text = cat_name 138 | ET.SubElement(ob, 'pose').text = 'Unspecified' 139 | ET.SubElement(ob, 'truncated').text = '0' 140 | ET.SubElement(ob, 'difficult').text = '0' 141 | 142 | bbox = ET.SubElement(ob, 'bndbox') 143 | ET.SubElement(bbox, 'xmin').text = str(xmin) 144 | ET.SubElement(bbox, 'ymin').text = str(ymin) 145 | ET.SubElement(bbox, 'xmax').text = str(xmax) 146 | ET.SubElement(bbox, 'ymax').text = str(ymax) 147 | 148 | # Save the file 149 | 150 | xml_str = ET.tostring(annotation) 151 | root = etree.fromstring(xml_str) 152 | xml_str = etree.tostring(root, pretty_print=True) # Entire content of the xml 153 | 154 | save_path = os.path.join(xml_dir, str(im_num) + '.' + 'xml') # Create save path with imnum.xml 155 | 156 | with open(save_path, 'wb') as temp_xml: 157 | temp_xml.write(xml_str) 158 | # keep record of which xml is paired with which image from coco_Set 159 | im_pairs[im_name] = im_num 160 | 161 | print('Copied imfile--> {} --- Object count--> {}'.format(str(im_num) + '.' + im_ext, ob_count)) 162 | 163 | im_num += 1 164 | ob_count += 1 165 | print('Finished with {} objects in {} images in {} seconds'.format(ob_count, im_num, time.time() - start_time)) 166 | --------------------------------------------------------------------------------