title: I am converting annotated data(JSON) to COCO format while making Tensorflow Object detetction API, this is the error I am facing from last 2 days
body: <p>import os
  import argparse
  import json</p>
  <p>from labelme import utils
  import numpy as np
  import glob
  import PIL.Image</p>
  <p>class labelme2coco(object):
  def <strong>init</strong>(self, labelme_json=[&quot;new_images\Train&quot;],
  save_json_path=&quot;new_images\Train_Coco/coco.json&quot;):
  &quot;&quot;&quot;
  :param labelme_json: the list of all labelme json file paths
  :param save_json_path: the path to save new json
  &quot;&quot;&quot;
  self.labelme_json = labelme_json
  self.save_json_path = save_json_path
  self.images = []
  self.categories = []
  self.annotations = []
  self.label = []
  self.annID = 1
  self.height = 0
  self.width = 0</p>
  <pre><code>    self.save_json()
  
  def data_transfer(self):
      for num, json_file in enumerate(self.labelme_json):
          with open(json_file, &quot;r&quot;) as fp:
              data = json.load(fp)
              self.images.append(self.image(data, num))
              for shapes in data[&quot;shapes&quot;]:
                  label = shapes[&quot;label&quot;].split(&quot;_&quot;)
                  if label not in self.label:
                      self.label.append(label)
                  points = shapes[&quot;points&quot;]
                  self.annotations.append(self.annotation(points, label, num))
                  self.annID += 1
  
      # Sort all text labels so they are in the same order across data splits.
      self.label.sort()
      for label in self.label:
          self.categories.append(self.category(label))
      for annotation in self.annotations:
          annotation[&quot;category_id&quot;] = self.getcatid(annotation[&quot;category_id&quot;])
  
  def image(self, data, num):
      image = {}
      img = utils.img_b64_to_arr(data[&quot;imageData&quot;])
      height, width = img.shape[:2]
      img = None
      image[&quot;height&quot;] = height
      image[&quot;width&quot;] = width
      image[&quot;id&quot;] = num
      image[&quot;file_name&quot;] = data[&quot;imagePath&quot;].split(&quot;/&quot;)[-1]
  
      self.height = height
      self.width = width
  
      return image
  
  def category(self, label):
      category = {}
      category[&quot;supercategory&quot;] = label[0]
      category[&quot;id&quot;] = len(self.categories)
      category[&quot;name&quot;] = label[0]
      return category
  
  def annotation(self, points, label, num):
      annotation = {}
      contour = np.array(points)
      x = contour[:, 0]
      y = contour[:, 1]
      area = 0.5 * np.abs(np.dot(x, np.roll(y, 1)) - np.dot(y, np.roll(x, 1)))
      annotation[&quot;segmentation&quot;] = [list(np.asarray(points).flatten())]
      annotation[&quot;iscrowd&quot;] = 0
      annotation[&quot;area&quot;] = area
      annotation[&quot;image_id&quot;] = num
  
      annotation[&quot;bbox&quot;] = list(map(float, self.getbbox(points)))
  
      annotation[&quot;category_id&quot;] = label[0]  # self.getcatid(label)
      annotation[&quot;id&quot;] = self.annID
      return annotation
  
  def getcatid(self, label):
      for category in self.categories:
          if label == category[&quot;name&quot;]:
              return category[&quot;id&quot;]
      print(&quot;label: {} not in categories: {}.&quot;.format(label, self.categories))
      exit()
      return -1
  
  def getbbox(self, points):
      polygons = points
      mask = self.polygons_to_mask([self.height, self.width], polygons)
      return self.mask2box(mask)
  
  def mask2box(self, mask):
  
      index = np.argwhere(mask == 1)
      rows = index[:, 0]
      clos = index[:, 1]
  
      left_top_r = np.min(rows)  # y
      left_top_c = np.min(clos)  # x
  
      right_bottom_r = np.max(rows)
      right_bottom_c = np.max(clos)
  
      return [
          left_top_c,
          left_top_r,
          right_bottom_c - left_top_c,
          right_bottom_r - left_top_r,
      ]
  
  def polygons_to_mask(self, img_shape, polygons):
      mask = np.zeros(img_shape, dtype=np.uint8)
      mask = PIL.Image.fromarray(mask)
      xy = list(map(tuple, polygons))
      PIL.ImageDraw.Draw(mask).polygon(xy=xy, outline=1, fill=1)
      mask = np.array(mask, dtype=bool)
      return mask
  
  def data2coco(self):
      data_coco = {}
      data_coco[&quot;images&quot;] = self.images
      data_coco[&quot;categories&quot;] = self.categories
      data_coco[&quot;annotations&quot;] = self.annotations
      return data_coco
  
  def save_json(self):
      print(&quot;save coco json&quot;)
      self.data_transfer()
      self.data_coco = self.data2coco()
  
      print(self.save_json_path)
      os.makedirs(
          os.path.dirname(os.path.abspath(self.save_json_path)), exist_ok=True
      )
      json.dump(self.data_coco, open(self.save_json_path, &quot;w&quot;), indent=4)
  </code></pre>
  <p>if <strong>name</strong> == &quot;<strong>main</strong>&quot;:</p>
  <pre><code>parser = argparse.ArgumentParser(
      description=&quot;labelme annotation to coco data json file.&quot;
  )
  parser.add_argument(
      labelme_images=&quot;new_images\Train&quot;,
      help=&quot;Directory to labelme images and annotation json files.&quot;,
      type=str,
  )
  parser.add_argument(
      &quot;--output&quot;, help=&quot;Output json file path.&quot;, default=&quot;new_images\Train_Coco/coco.json&quot;
  )
  args = parser.parse_args()
  labelme_json = glob.glob(os.path.join(args.labelme_images, &quot;*.json&quot;))
  labelme2coco(labelme_json, args.output)
  </code></pre>
  <p><strong>Error which I am getting</strong>
  <a href="https://i.stack.imgur.com/F5nLD.png" rel="nofollow noreferrer">Type-Error and redirecting to argparse library</a></p>
  <p><strong>The tutorial I am following is also attached</strong> <a href="https://gilberttanner.com/blog/train-a-mask-r-cnn-model-with-the-tensorflow-object-detection-api/" rel="nofollow noreferrer">Train a Mask R-CNN model with the Tensorflow Object Detection API</a></p>