{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"1+1","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-29T12:18:37.957181Z","iopub.execute_input":"2021-07-29T12:18:37.957556Z","iopub.status.idle":"2021-07-29T12:18:37.973416Z","shell.execute_reply.started":"2021-07-29T12:18:37.957481Z","shell.execute_reply":"2021-07-29T12:18:37.972432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pycocotools","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:18:41.224419Z","iopub.execute_input":"2021-07-29T12:18:41.224748Z","iopub.status.idle":"2021-07-29T12:18:58.221705Z","shell.execute_reply.started":"2021-07-29T12:18:41.224722Z","shell.execute_reply":"2021-07-29T12:18:58.220565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom pycocotools.coco import COCO\nfrom PIL import Image, ImageDraw\nimport matplotlib.pyplot as plt\njson_path = '/kaggle/input/cowboyoutfits/train.json'\nimg_path = \"/kaggle/input/cowboyoutfits/images\"","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:19:01.736644Z","iopub.execute_input":"2021-07-29T12:19:01.737015Z","iopub.status.idle":"2021-07-29T12:19:01.751121Z","shell.execute_reply.started":"2021-07-29T12:19:01.736979Z","shell.execute_reply":"2021-07-29T12:19:01.750154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load coco data\ncoco = COCO(annotation_file=json_path)\n\n# get all image index info\nids = list(sorted(coco.imgs.keys()))\nprint(\"number of images: {}\".format(len(ids)))\n\n# get all coco class labels\ncoco_classes = dict([(v[\"id\"], v[\"name\"]) for k, v in coco.cats.items()])","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:19:07.225629Z","iopub.execute_input":"2021-07-29T12:19:07.226022Z","iopub.status.idle":"2021-07-29T12:19:07.425489Z","shell.execute_reply.started":"2021-07-29T12:19:07.225987Z","shell.execute_reply":"2021-07-29T12:19:07.424484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 遍历前三张图像\nfor img_id in ids[65:69]:\n    # 获取对应图像id的所有annotations idx信息\n    ann_ids = coco.getAnnIds(imgIds=img_id)\n\n    # 根据annotations idx信息获取所有标注信息\n    targets = coco.loadAnns(ann_ids)\n\n    # get image file name\n    path = coco.loadImgs(img_id)[0]['file_name']\n\n    # read image\n    img = Image.open(os.path.join(img_path, path)).convert('RGB')\n    # img.save(\"./pre_img/\" + path + \".jpg\")\n    draw = ImageDraw.Draw(img)\n    # draw box to image\n    for target in targets:\n        x, y, w, h = target[\"bbox\"]\n        x1, y1, x2, y2 = x, y, int(x + w), int(y + h)\n        draw.rectangle((x1, y1, x2, y2))\n        draw.text((x1, y1), coco_classes[target[\"category_id\"]])\n    # img.save(\"./pre_img/\" + path + \"_result.jpg\")\n\n    # show image\n    plt.imshow(img)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:19:12.911133Z","iopub.execute_input":"2021-07-29T12:19:12.911468Z","iopub.status.idle":"2021-07-29T12:19:13.987727Z","shell.execute_reply.started":"2021-07-29T12:19:12.911442Z","shell.execute_reply":"2021-07-29T12:19:13.986962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}