code: def plot_few_images_with_bbox(
      output_metadata,
      candidate_labels,
      plot_count=10,
      class_to_label_name_map=None,
      topk=1,
      filter_key="gender_skintone",
      filter_value="male_darker",
      confidence_threshold=0.0,
  ):
      # print(output_metadata)
      preds_values = [
          item["prediction"] for item in list(output_metadata.values())
      ]
      confidence_scores = []
      if "confidence_scores" in list(output_metadata.values())[0]:
          confidence_scores = [
              item["confidence_scores"]
              for item in list(output_metadata.values())
          ]
      print(f"Found confidence scores: {len(confidence_scores)}")
      media_ids = [
          item["orig_path"] for item in list(output_metadata.values())
      ]
      gender_presentation = [
          item["gender"] for item in list(output_metadata.values())
      ]
      age_presentation = [
          item["age"] for item in list(output_metadata.values())
      ]
      bbox_annotations = []
      # print(list(output_metadata.values())[0])
      if "bbox" in list(output_metadata.values())[0]:
          bbox_annotations = [
              item["bbox"] for item in list(output_metadata.values())
          ]
      attribute_values = [
          item[filter_key] for item in list(output_metadata.values())
      ]
      num_images = len(preds_values)
      count = 0
      for idx in range(num_images):
          if plot_count > 0 and count >= plot_count:
              return
          if attribute_values[idx] == filter_value:
              img_predictions = preds_values[idx][:topk]
              img_scores = []
              if len(confidence_scores) > 0:
                  img_scores = confidence_scores[idx][:topk]
              if class_to_label_name_map:
                  img_predictions = [
                      class_to_label_name_map[item]
                      for item in img_predictions
                  ]
              found_intersection_preds = list(
                  set(candidate_labels).intersection(
                      set(img_predictions)
                  )
              )
              if len(found_intersection_preds) > 0:
                  if (
                      len(confidence_scores) > 0
                      and confidence_threshold > 0.0
                  ):
                      intersection_preds_scores = [
                          float(img_scores[img_predictions.index(pred)])
                          for pred in found_intersection_preds
                      ]
                      # print(found_intersection_preds)
                      # print(intersection_preds_scores)
                      if not np.any(
                          np.array(intersection_preds_scores)
                          >= confidence_threshold
                      ):
                          continue
                  try:
                      # print(f"img: {media_ids[idx]}\npredictions: {img_predictions}")
                      print(
                          f"\t\t\t\t\t\t\t\t\timg: {media_ids[idx]}\n\t\t\t\t\t\t\t\t\tpredictions: {img_predictions}"
                      )
                      if len(img_scores) > 0:
                          print(
                              f"\t\t\t\t\t\t\t\t\t\tscores: {img_scores}"
                          )
                          print(
                              f"\t\t\t\t\t\t\t\t\t\tGender: {gender_presentation[idx]}, Age: {age_presentation[idx]}"
                          )
                      with PathManager.open(
                          media_ids[idx], "rb"
                      ) as fopen:
                          img = Image.open(fopen).convert("RGB")
                      if len(bbox_annotations) > 0:
                          # print('ha box annotation.....')
                          bbox = [
                              float(item)
                              for item in bbox_annotations[idx]
                          ]
                          fig_size = plt.rcParams["figure.figsize"]
                          fig_size[0] = 5
                          fig_size[1] = 5
                          plt.imshow(img)
                          plt.axis("off")
                          plt.show()
                          img = img.crop(bbox)
                          bbox_size = min(img.size)
                          width, height = img.size
                          ratio = max(img.size) / min(img.size)
                          if ratio >= 1.2:
                              if width < height:
                                  bbox = (0, 0, bbox_size, bbox_size)
                              else:
                                  bbox = (
                                      int((width - bbox_size) / 2),
                                      0,
                                      int((width - bbox_size) / 2)
                                      + bbox_size,
                                      height,
                                  )
                              print(f"\t\t\t\t\t\t\t\t\t\tbbox: {bbox}")
                              img = img.crop(bbox)
                          else:
                              print(
                                  f"\t\t\t\t\t\t\t\t\t\tSkipping the cropping....bbox: {bbox}"
                              )
                          img = T.Resize(size=[224, 224])(img)
                      else:
                          # print("NO BBOX ANNOTATION...")
                          img = T.Resize(size=[224, 224])(img)
                      fig_size = plt.rcParams["figure.figsize"]
                      fig_size[0] = 5
                      fig_size[1] = 5
                      plt.imshow(img)
                      plt.axis("off")
                      plt.show()
                      count += 1
                  except Exception:
                      pass
  
file_path: vissl/projects/fairness_indicators/harmful_label_associations/inference_label_assoc_openimages_miap_test.py
imports: import matplotlib.pyplot
  import numpy
  import pandas
  import torchvision.transforms
  from fvcore.common.file_io import PathManager
  from PIL import Image
  from vissl.utils.io import load_file
label_desc: Use image node_id 8 for calling a global function with example usage: plot_few_images_with_bbox(output_metadata, candidate_labels, plot_count, class_to_label_name_map, topk, filter_key, filter_value, confidence_threshold) without return types