code: def dict_to_tf_example(
      data,
      dataset_directory,
      label_map_dict,
      ignore_difficult_instances=False,
      image_subdirectory="JPEGImages",
      ann_json_dict=None,
  ):
      """Convert XML derived dict to tf.Example proto.
  
      Notice that this function normalizes the bounding box coordinates provided
      by the raw data.
  
      Args:
        data: dict holding PASCAL XML fields for a single image (obtained by running
          tfrecord_util.recursive_parse_xml_to_dict)
        dataset_directory: Path to root directory holding PASCAL dataset
        label_map_dict: A map from string label names to integers ids.
        ignore_difficult_instances: Whether to skip difficult instances in the
          dataset  (default: False).
        image_subdirectory: String specifying subdirectory within the PASCAL dataset
          directory holding the actual image data.
        ann_json_dict: annotation json dictionary.
  
      Returns:
        example: The converted tf.Example.
  
      Raises:
        ValueError: if the image pointed to by data['filename'] is not a valid JPEG
      """
      img_path = os.path.join(
          data["folder"], image_subdirectory, data["filename"]
      )
      full_path = os.path.join(dataset_directory, img_path)
      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)
      if image.format != "JPEG":
          raise ValueError("Image format not JPEG")
      key = hashlib.sha256(encoded_jpg).hexdigest()
  
      width = int(data["size"]["width"])
      height = int(data["size"]["height"])
      image_id = get_image_id(data["filename"])
      if ann_json_dict:
          image = {
              "file_name": data["filename"],
              "height": height,
              "width": width,
              "id": image_id,
          }
          ann_json_dict["images"].append(image)
  
      xmin = []
      ymin = []
      xmax = []
      ymax = []
      area = []
      classes = []
      classes_text = []
      truncated = []
      poses = []
      difficult_obj = []
      if "object" in data:
          for obj in data["object"]:
              difficult = bool(int(obj["difficult"]))
              if ignore_difficult_instances and difficult:
                  continue
  
              difficult_obj.append(int(difficult))
  
              xmin.append(float(obj["bndbox"]["xmin"]) / width)
              ymin.append(float(obj["bndbox"]["ymin"]) / height)
              xmax.append(float(obj["bndbox"]["xmax"]) / width)
              ymax.append(float(obj["bndbox"]["ymax"]) / height)
              area.append((xmax[-1] - xmin[-1]) * (ymax[-1] - ymin[-1]))
              classes_text.append(obj["name"].encode("utf8"))
              classes.append(label_map_dict[obj["name"]])
              truncated.append(int(obj["truncated"]))
              poses.append(obj["pose"].encode("utf8"))
  
              if ann_json_dict:
                  abs_xmin = int(obj["bndbox"]["xmin"])
                  abs_ymin = int(obj["bndbox"]["ymin"])
                  abs_xmax = int(obj["bndbox"]["xmax"])
                  abs_ymax = int(obj["bndbox"]["ymax"])
                  abs_width = abs_xmax - abs_xmin
                  abs_height = abs_ymax - abs_ymin
                  ann = {
                      "area": abs_width * abs_height,
                      "iscrowd": 0,
                      "image_id": image_id,
                      "bbox": [
                          abs_xmin,
                          abs_ymin,
                          abs_width,
                          abs_height,
                      ],
                      "category_id": label_map_dict[obj["name"]],
                      "id": get_ann_id(),
                      "ignore": 0,
                      "segmentation": [],
                  }
                  ann_json_dict["annotations"].append(ann)
  
      example = tf.train.Example(
          features=tf.train.Features(
              feature={
                  "image/height": tfrecord_util.int64_feature(height),
                  "image/width": tfrecord_util.int64_feature(width),
                  "image/filename": tfrecord_util.bytes_feature(
                      data["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")
                  ),
                  "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/area": tfrecord_util.float_list_feature(
                      area
                  ),
                  "image/object/class/text": tfrecord_util.bytes_list_feature(
                      classes_text
                  ),
                  "image/object/class/label": tfrecord_util.int64_list_feature(
                      classes
                  ),
                  "image/object/difficult": tfrecord_util.int64_list_feature(
                      difficult_obj
                  ),
                  "image/object/truncated": tfrecord_util.int64_list_feature(
                      truncated
                  ),
                  "image/object/view": tfrecord_util.bytes_list_feature(
                      poses
                  ),
              }
          )
      )
      return example
  
file_path: DALI/docs/examples/use_cases/tensorflow/efficientdet/dataset/create_pascal_tfrecord.py
imports: import hashlib
  import io
  import json
  import os
  from absl import app
  from absl import flags
  from absl import logging
  from lxml import etree
  import PIL.Image
  import tensorflow
  from dataset import tfrecord_util
label_desc: Use image node_id 3 for calling a global function with example usage: dict_to_tf_example(data, dataset_directory, label_map_dict, ignore_difficult_instances, image_subdirectory, ann_json_dict) and returns: example