code: def filter_images_with_few_keypoints(
      dataset_dicts, min_keypoints_per_image
  ):
      """
      Filter out images with too few number of keypoints.
  
      Args:
          dataset_dicts (list[dict]): annotations in Detectron2 Dataset format.
  
      Returns:
          list[dict]: the same format as dataset_dicts, but filtered.
      """
      num_before = len(dataset_dicts)
  
      def visible_keypoints_in_image(dic):
          # Each keypoints field has the format [x1, y1, v1, ...], where v is visibility
          annotations = dic["annotations"]
          return sum(
              (np.array(ann["keypoints"][2::3]) > 0).sum()
              for ann in annotations
              if "keypoints" in ann
          )
  
      dataset_dicts = [
          x
          for x in dataset_dicts
          if visible_keypoints_in_image(x) >= min_keypoints_per_image
      ]
      num_after = len(dataset_dicts)
      logger = logging.getLogger(__name__)
      logger.info(
          "Removed {} images with fewer than {} keypoints.".format(
              num_before - num_after, min_keypoints_per_image
          )
      )
      return dataset_dicts
  
imports: import itertools
  import logging
  import numpy
  import operator
  import pickle
  from collections import OrderedDict, defaultdict
  from typing import Any, Callable, Dict, List, Optional, Union
  import torch
  import torch.utils.data
  from tabulate import tabulate
  from termcolor import colored
  from detectron2.config import configurable
  from detectron2.structures import BoxMode
  from detectron2.utils.comm import get_world_size
  from detectron2.utils.env import seed_all_rng
  from detectron2.utils.file_io import PathManager
  from detectron2.utils.logger import _log_api_usage, log_first_n
  from .catalog import DatasetCatalog, MetadataCatalog
  from .common import AspectRatioGroupedDataset, DatasetFromList, MapDataset, ToIterableDataset
  from .dataset_mapper import DatasetMapper
  from .detection_utils import check_metadata_consistency
  from .samplers import InferenceSampler, RandomSubsetTrainingSampler, RepeatFactorTrainingSampler, TrainingSampler
label_desc: Use image node_id 2 for calling a global function with example usage: filter_images_with_few_keypoints(dataset_dicts, min_keypoints_per_image) and returns: dataset_dicts, sum