{"id":403,"date":"2020-06-25T20:37:02","date_gmt":"2020-06-25T12:37:02","guid":{"rendered":"http:\/\/123.57.142.208\/?p=403"},"modified":"2020-06-25T20:37:02","modified_gmt":"2020-06-25T12:37:02","slug":"coco%e6%95%b0%e6%8d%ae%e9%9b%86%e8%bd%ac%e5%8c%96%e4%b8%bavoc%e6%95%b0%e6%8d%ae%e9%9b%86","status":"publish","type":"post","link":"http:\/\/43.142.23.155\/?p=403","title":{"rendered":"coco\u6570\u636e\u96c6\u8f6c\u5316\u4e3avoc\u6570\u636e\u96c6"},"content":{"rendered":"\n<p>\u6ca1\u5fcd\u4f4f\uff0c\u60f3\u7740\u53cd\u6b63\u8fd9\u4e2a\u8fd8\u633a\u7b80\u5355\u7684\u6211\u5c31\u5f00\u59cb\u51b2\u4e86&#8230;\u81f3\u4e8e\u6253\u6807\u7b7e\u6211\u660e\u5929\u518d\u5199\u5427\u5199\u5b8c\u8fd9\u4e2a\u5c31\u5ce1\u8c37\u53bb&#8230;<\/p>\n\n\n\n<p><a href=\"http:\/\/images.cocodataset.org\/zips\/train2017.zip\">http:\/\/images.cocodataset.org<\/a><\/p>\n\n\n\n<p>\u4ee5\u4e0acoco\u6570\u636e\u96c6\u7684\u7f51\u5740\uff0c\u91cc\u9762\u6709\u975e\u5e38\u591a\u7684\u6570\u636e\u96c6<\/p>\n\n\n\n<p>COCO\u6570\u636e\u96c6\u76ee\u6807\u68c0\u6d4b\u4e2d\u670990\u7c7b\uff1a\uff08\u81ea\u884c\u53d6\u7528\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>classes:\n     {1: 'person', 2: 'bicycle', 3: 'car', 4: 'motorcycle', 5: 'airplane', 6: 'bus', 7: 'train', 8: 'truck', 9: 'boat', 10: 'traffic light', 11: 'fire hydrant', 13: 'stop sign', 14: 'parking meter', 15: 'bench', 16: 'bird', 17: 'cat', 18: 'dog', 19: 'horse', 20: 'sheep', 21: 'cow', 22: 'elephant', 23: 'bear', 24: 'zebra', 25: 'giraffe', 27: 'backpack', 28: 'umbrella', 31: 'handbag', 32: 'tie', 33: 'suitcase', 34: 'frisbee', 35: 'skis', 36: 'snowboard', 37: 'sports ball', 38: 'kite', 39: 'baseball bat', 40: 'baseball glove', 41: 'skateboard', 42: 'surfboard', 43: 'tennis racket', 44: 'bottle', 46: 'wine glass', 47: 'cup', 48: 'fork', 49: 'knife', 50: 'spoon', 51: 'bowl', 52: 'banana', 53: 'apple', 54: 'sandwich', 55: 'orange', 56: 'broccoli', 57: 'carrot', 58: 'hot dog',59: 'pizza', 60: 'donut', 61: 'cake', 62: 'chair', 63: 'couch', 64: 'potted plant', 65: 'bed', 67: 'dining table', 70: 'toilet', 72: 'tv', 73: 'laptop', 74: 'mouse', 75: 'remote', 76: 'keyboard',77: 'cell phone', 78: 'microwave', 79: 'oven', 80: 'toaster', 81: 'sink', 82: 'refrigerator', 84: 'book', 85: 'clock', 86: 'vase', 87: 'scissors', 88: 'teddy bear', 89: 'hair drier', 90: 'toothbrush'}<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u5f53\u65f6\u505a\u8fc7\u4e00\u4e2a\u4ea4\u901a\u5de5\u5177\u7684\u76ee\u6807\u68c0\u6d4b\uff1a<br>\u200b\u200b\u200b\u200b\u200b\u200b\u200bclasses_names = [&#8216;car&#8217;, &#8216;bicycle&#8217;, &#8216;person&#8217;, &#8216;motorcycle&#8217;, &#8216;bus&#8217;, &#8216;truck&#8217;]<\/p>\n\n\n\n<p>\u5176\u5b9e\u7531\u4e8e\u6211\u81ea\u5df1\u5199\u7684\u4ee3\u7801\u5728\u6211\u90a3\u53f0\u795e\u8239\u91cc\uff08\u5199\u51fa\u6765\u7684\u65f6\u5019\u8fd8\u6ca1\u8fd9\u4e2a\u7535\u8111\uff09\uff0c\u75ab\u60c5\u5b66\u6821\u8fd8\u6ca1\u5f00\u5b66\uff0c\u6211\u5c31\u53ea\u597dPO\u4e00\u4e2a\u522b\u7684\u5927\u4f6c\u5199\u7684\uff1a\uff08\u5927\u6982\u770b\u4e00\u4e0b\u6ca1\u5565\u5927\u95ee\u9898\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from pycocotools.coco import COCO\nimport os\nimport shutil\nfrom tqdm import tqdm\nimport skimage.io as io\nimport matplotlib.pyplot as plt\nimport cv2\nfrom PIL import Image, ImageDraw\n \n#the path you want to save your results for coco to voc\nsavepath=\"E:\/datasets\/COCO\/result\/\"\nimg_dir=savepath+'images\/'\nanno_dir=savepath+'Annotations\/'\n# datasets_list=&#91;'train2014', 'val2014']\ndatasets_list=&#91;'train2017']\n \nclasses_names = &#91;'car', 'bicycle', 'person', 'motorcycle', 'bus', 'truck']\n#Store annotations and train2014\/val2014\/... in this folder\ndataDir= 'E:\/datasets\/COCO\/'\n \nheadstr = \"\"\"\\\n&lt;annotation>\n    &lt;folder>VOC&lt;\/folder>\n    &lt;filename>%s&lt;\/filename>\n    &lt;source>\n        &lt;database>My Database&lt;\/database>\n        &lt;annotation>COCO&lt;\/annotation>\n        &lt;image>flickr&lt;\/image>\n        &lt;flickrid>NULL&lt;\/flickrid>\n    &lt;\/source>\n    &lt;owner>\n        &lt;flickrid>NULL&lt;\/flickrid>\n        &lt;name>company&lt;\/name>\n    &lt;\/owner>\n    &lt;size>\n        &lt;width>%d&lt;\/width>\n        &lt;height>%d&lt;\/height>\n        &lt;depth>%d&lt;\/depth>\n    &lt;\/size>\n    &lt;segmented>0&lt;\/segmented>\n\"\"\"\nobjstr = \"\"\"\\\n    &lt;object>\n        &lt;name>%s&lt;\/name>\n        &lt;pose>Unspecified&lt;\/pose>\n        &lt;truncated>0&lt;\/truncated>\n        &lt;difficult>0&lt;\/difficult>\n        &lt;bndbox>\n            &lt;xmin>%d&lt;\/xmin>\n            &lt;ymin>%d&lt;\/ymin>\n            &lt;xmax>%d&lt;\/xmax>\n            &lt;ymax>%d&lt;\/ymax>\n        &lt;\/bndbox>\n    &lt;\/object>\n\"\"\"\n \ntailstr = '''\\\n&lt;\/annotation>\n'''\n \n#if the dir is not exists,make it,else delete it\ndef mkr(path):\n    if os.path.exists(path):\n        shutil.rmtree(path)\n        os.mkdir(path)\n    else:\n        os.mkdir(path)\nmkr(img_dir)\nmkr(anno_dir)\ndef id2name(coco):\n    classes=dict()\n    for cls in coco.dataset&#91;'categories']:\n        classes&#91;cls&#91;'id']]=cls&#91;'name']\n    return classes\n \ndef write_xml(anno_path,head, objs, tail):\n    f = open(anno_path, \"w\")\n    f.write(head)\n    for obj in objs:\n        f.write(objstr%(obj&#91;0],obj&#91;1],obj&#91;2],obj&#91;3],obj&#91;4]))\n    f.write(tail)\n \n \ndef save_annotations_and_imgs(coco,dataset,filename,objs):\n    #eg:COCO_train2014_000000196610.jpg-->COCO_train2014_000000196610.xml\n    anno_path=anno_dir+filename&#91;:-3]+'xml'\n    img_path=dataDir+dataset+'\/'+filename\n    print(img_path)\n    dst_imgpath=img_dir+filename\n \n    img=cv2.imread(img_path)\n    if (img.shape&#91;2] == 1):\n        print(filename + \" not a RGB image\")\n        return\n    shutil.copy(img_path, dst_imgpath)\n \n    head=headstr % (filename, img.shape&#91;1], img.shape&#91;0], img.shape&#91;2])\n    tail = tailstr\n    write_xml(anno_path,head, objs, tail)\n \n \ndef showimg(coco,dataset,img,classes,cls_id,show=True):\n    global dataDir\n    I=Image.open('%s\/%s\/%s'%(dataDir,dataset,img&#91;'file_name']))\n    #\u901a\u8fc7id\uff0c\u5f97\u5230\u6ce8\u91ca\u7684\u4fe1\u606f\n    annIds = coco.getAnnIds(imgIds=img&#91;'id'], catIds=cls_id, iscrowd=None)\n    # print(annIds)\n    anns = coco.loadAnns(annIds)\n    # print(anns)\n    # coco.showAnns(anns)\n    objs = &#91;]\n    for ann in anns:\n        class_name=classes&#91;ann&#91;'category_id']]\n        if class_name in classes_names:\n            print(class_name)\n            if 'bbox' in ann:\n                bbox=ann&#91;'bbox']\n                xmin = int(bbox&#91;0])\n                ymin = int(bbox&#91;1])\n                xmax = int(bbox&#91;2] + bbox&#91;0])\n                ymax = int(bbox&#91;3] + bbox&#91;1])\n                obj = &#91;class_name, xmin, ymin, xmax, ymax]\n                objs.append(obj)\n                draw = ImageDraw.Draw(I)\n                draw.rectangle(&#91;xmin, ymin, xmax, ymax])\n    if show:\n        plt.figure()\n        plt.axis('off')\n        plt.imshow(I)\n        plt.show()\n \n    return objs\n \nfor dataset in datasets_list:\n    #.\/COCO\/annotations\/instances_train2014.json\n    annFile='{}\/annotations\/instances_{}.json'.format(dataDir,dataset)\n \n    #COCO API for initializing annotated data\n    coco = COCO(annFile)\n    '''\n    COCO \u5bf9\u8c61\u521b\u5efa\u5b8c\u6bd5\u540e\u4f1a\u8f93\u51fa\u5982\u4e0b\u4fe1\u606f:\n    loading annotations into memory...\n    Done (t=0.81s)\n    creating index...\n    index created!\n    \u81f3\u6b64, json \u811a\u672c\u89e3\u6790\u5b8c\u6bd5, \u5e76\u4e14\u5c06\u56fe\u7247\u548c\u5bf9\u5e94\u7684\u6807\u6ce8\u6570\u636e\u5173\u8054\u8d77\u6765.\n    '''\n    #show all classes in coco\n    classes = id2name(coco)\n    print(classes)\n    #&#91;1, 2, 3, 4, 6, 8]\n    classes_ids = coco.getCatIds(catNms=classes_names)\n    print(classes_ids)\n    for cls in classes_names:\n        #Get ID number of this class\n        cls_id=coco.getCatIds(catNms=&#91;cls])\n        img_ids=coco.getImgIds(catIds=cls_id)\n        print(cls,len(img_ids))\n        # imgIds=img_ids&#91;0:10]\n        for imgId in tqdm(img_ids):\n            img = coco.loadImgs(imgId)&#91;0]\n            filename = img&#91;'file_name']\n            # print(filename)\n            objs=showimg(coco, dataset, img, classes,classes_ids,show=False)\n            print(objs)\n            save_annotations_and_imgs(coco, dataset, filename, objs)<\/code><\/pre>\n\n\n\n<p>\u56de\u53bb\u80fd\u627e\u5230\u7684\u8bdd\u6211\u5c31po\u81ea\u5df1\u7684\uff0c\u6bd5\u7adf\u81ea\u5df1\u7684\u6700\u9999\u4e86\uff08\u4e0b\u6b21\u4e00\u5b9a\u4e0b\u6b21\u4e00\u5b9a\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6ca1\u5fcd\u4f4f\uff0c\u60f3\u7740\u53cd\u6b63\u8fd9\u4e2a\u8fd8\u633a\u7b80\u5355\u7684\u6211\u5c31\u5f00\u59cb\u51b2\u4e86&#8230;\u81f3\u4e8e\u6253\u6807\u7b7e\u6211\u660e\u5929\u518d\u5199\u5427\u5199\u5b8c\u8fd9\u4e2a\u5c31\u5ce1\u8c37\u53bb&#8230; http:\/\/images.cocodataset.org \u4ee5\u4e0acoco\u6570\u636e\u96c6\u7684\u7f51\u5740\uff0c\u91cc\u9762&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-403","post","type-post","status-publish","format-standard","hentry","category-3"],"_links":{"self":[{"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/posts\/403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/43.142.23.155\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=403"}],"version-history":[{"count":0,"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/posts\/403\/revisions"}],"wp:attachment":[{"href":"http:\/\/43.142.23.155\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/43.142.23.155\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=403"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/43.142.23.155\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}