code: def create_tf_example(
      image,
      image_dir,
      bbox_annotations=None,
      category_index=None,
      caption_annotations=None,
  ):
      """Converts image and annotations to a tf.Example proto.
  
      Args:
        image: dict with keys: [u'license', u'file_name', u'coco_url', u'height',
          u'width', u'date_captured', u'flickr_url', u'id']
        image_dir: directory containing the image files.
        bbox_annotations:
          list of dicts with keys: [u'segmentation', u'area', u'iscrowd',
            u'image_id', u'bbox', u'category_id', u'id'] Notice that bounding box
            coordinates in the official COCO dataset are given as [x, y, width,
            height] tuples using absolute coordinates where x, y represent the
            top-left (0-indexed) corner.  This function converts to the format
            expected by the Tensorflow Object Detection API (which is which is
            [ymin, xmin, ymax, xmax] with coordinates normalized relative to image
            size).
        category_index: a dict containing COCO category information keyed by the
          'id' field of each category.  See the label_map_util.create_category_index
          function.
        caption_annotations:
          list of dict with keys: [u'id', u'image_id', u'str'].
  
      Returns:
        example: The converted tf.Example
        num_annotations_skipped: Number of (invalid) annotations that were ignored.
  
      Raises:
        ValueError: if the image pointed to by data['filename'] is not a valid JPEG
      """
      image_height = image["height"]
      image_width = image["width"]
      filename = image["file_name"]
      image_id = image["id"]
  
      full_path = os.path.join(image_dir, filename)
      with tf.io.gfile.GFile(full_path, "rb") as fid:
          encoded_jpg = fid.read()
      encoded_jpg_io = io.BytesIO(encoded_jpg)
      image = PIL.Image.open(encoded_jpg_io)
      key = hashlib.sha256(encoded_jpg).hexdigest()
      feature_dict = {
          "image/height": tfrecord_util.int64_feature(image_height),
          "image/width": tfrecord_util.int64_feature(image_width),
          "image/filename": tfrecord_util.bytes_feature(
              filename.encode("utf8")
          ),
          "image/source_id": tfrecord_util.bytes_feature(
              str(image_id).encode("utf8")
          ),
          "image/key/sha256": tfrecord_util.bytes_feature(
              key.encode("utf8")
          ),
          "image/encoded": tfrecord_util.bytes_feature(encoded_jpg),
          "image/format": tfrecord_util.bytes_feature(
              "jpeg".encode("utf8")
          ),
      }
  
      num_annotations_skipped = 0
      xmin = []
      xmax = []
      ymin = []
      ymax = []
      is_crowd = []
      category_names = []
      category_ids = []
      area = []
  
      if bbox_annotations:
          for object_annotations in bbox_annotations:
              (x, y, width, height) = tuple(object_annotations["bbox"])
              if width <= 0 or height <= 0:
                  num_annotations_skipped += 1
                  continue
              if x + width > image_width or y + height > image_height:
                  num_annotations_skipped += 1
                  continue
              xmin.append(float(x) / image_width)
              xmax.append(float(x + width) / image_width)
              ymin.append(float(y) / image_height)
              ymax.append(float(y + height) / image_height)
              is_crowd.append(object_annotations["iscrowd"])
              category_id = int(object_annotations["category_id"])
              category_ids.append(category_id)
              category_names.append(
                  category_index[category_id]["name"].encode("utf8")
              )
              area.append(object_annotations["area"])
  
      feature_dict.update(
          {
              "image/object/bbox/xmin": tfrecord_util.float_list_feature(
                  xmin
              ),
              "image/object/bbox/xmax": tfrecord_util.float_list_feature(
                  xmax
              ),
              "image/object/bbox/ymin": tfrecord_util.float_list_feature(
                  ymin
              ),
              "image/object/bbox/ymax": tfrecord_util.float_list_feature(
                  ymax
              ),
              "image/object/class/text": tfrecord_util.bytes_list_feature(
                  category_names
              ),
              "image/object/class/label": tfrecord_util.int64_list_feature(
                  category_ids
              ),
              "image/object/is_crowd": tfrecord_util.int64_list_feature(
                  is_crowd
              ),
              "image/object/area": tfrecord_util.float_list_feature(
                  area
              ),
          }
      )
  
      if caption_annotations:
          captions = []
          for caption_annotation in caption_annotations:
              captions.append(
                  caption_annotation["caption"].encode("utf8")
              )
          feature_dict.update(
              {
                  "image/caption": tfrecord_util.bytes_list_feature(
                      captions
                  )
              }
          )
  
      example = tf.train.Example(
          features=tf.train.Features(feature=feature_dict)
      )
      return key, example, num_annotations_skipped
  
imports: import collections
  import hashlib
  import io
  import json
  import multiprocessing
  import os
  from absl import app
  from absl import flags
  from absl import logging
  import numpy
  import PIL.Image
  import tensorflow
  import tfrecord_util
  import label_map_util
label_desc: Use image node_id 1 for calling a global function with example usage: create_tf_example(image, image_dir, bbox_annotations, category_index, caption_annotations) and returns: key,  example,  num_annotations_skipped