text: import argparse
  import glob
  import logging
  import os
  import pickle
  import sys
  from typing import Any, ClassVar, Dict, List
  
  import cv2
  import torch
  import numpy as np
  
  from detectron2.config import CfgNode, get_cfg
  from detectron2.data.detection_utils import read_image
  from detectron2.engine.defaults import DefaultPredictor
  from detectron2.structures.instances import Instances
  from detectron2.utils.logger import setup_logger
  
  from densepose import add_densepose_config
  from densepose.structures import DensePoseChartPredictorOutput, DensePoseEmbeddingPredictorOutput
  from densepose.utils.logger import verbosity_to_level
  
  from densepose.vis.extractor import (
      CompoundExtractor,
      DensePoseOutputsExtractor,
      DensePoseResultExtractor,
      create_extractor,
  )
  
  DOC = """Recognize police gesture with CSE. Requires detectron2 and DensePose modules.
  """
  
  LOGGER_NAME = "apply_net"
  logger = logging.getLogger(LOGGER_NAME)
  
  _ACTION_REGISTRY: Dict[str, "Action"] = {}
  
  def register_action(cls: type):
      """
      Decorator for action classes to automate action registration
      """
      global _ACTION_REGISTRY
      _ACTION_REGISTRY[cls.COMMAND] = cls
      return cls
  
  class Action(object):
      @classmethod
      def add_arguments(cls: type, parser: argparse.ArgumentParser):
          parser.add_argument(
              "-v",
              "--verbosity",
              action="count",
              help="Verbose mode. Multiple -v options increase the verbosity.",
          )
  
  class InferenceAction(Action):
      @classmethod
      def add_arguments(cls: type, parser: argparse.ArgumentParser):
          super(InferenceAction, cls).add_arguments(parser)
          parser.add_argument("cfg", metavar="<config>", help="Config file")
          parser.add_argument("model", metavar="<model>", help="Model file")
          parser.add_argument("input", metavar="<input>", help="Input data")
          parser.add_argument(
              "--opts",
              help="Modify config options using the command-line 'KEY VALUE' pairs",
              default=[],
              nargs=argparse.REMAINDER,
          )
  
      @classmethod
      def execute(cls: type, args: argparse.Namespace):
          logger.info(f"Loading config from {args.cfg}")
          opts = []
          cfg = cls.setup_config(args.cfg, args.model, args, opts)
          logger.info(f"Loading model from {args.model}")
          predictor = DefaultPredictor(cfg)
          logger.info(f"Loading data from {args.input}")
          file_list = cls._get_input_file_list(args.input)
          if len(file_list) == 0:
              logger.warning(f"No input images for {args.input}")
              return
          context = cls.create_context(args, cfg)
          for file_name in file_list:
              img = read_image(file_name, format="BGR")  # predictor expects BGR image.
              with torch.no_grad():
                  outputs = predictor(img)["instances"]
                  cls.execute_on_outputs(context, {"file_name": file_name, "image": img}, outputs)
          cls.postexecute(context)
  
      @classmethod
      def setup_config(
          cls: type, config_fpath: str, model_fpath: str, args: argparse.Namespace, opts: List[str]
      ):
          cfg = get_cfg()
          add_densepose_config(cfg)
          cfg.merge_from_file(config_fpath)
          cfg.merge_from_list(args.opts)
          if opts:
              cfg.merge_from_list(opts)
          cfg.MODEL.WEIGHTS = model_fpath
          cfg.freeze()
          return cfg
  
      @classmethod
      def _get_input_file_list(cls: type, input_spec: str):
          if os.path.isdir(input_spec):
              file_list = [
                  os.path.join(input_spec, fname)
                  for fname in os.listdir(input_spec)
                  if os.path.isfile(os.path.join(input_spec, fname))
              ]
          elif os.path.isfile(input_spec):
              file_list = [input_spec]
          else:
              file_list = glob.glob(input_spec)
          return file_list
  
  
  @register_action
  class DumpAction(InferenceAction):
      """
      Dump action that outputs results to a pickle file
      """
  
      COMMAND: ClassVar[str] = "dump"
  
      @classmethod
      def add_parser(cls: type, subparsers: argparse._SubParsersAction):
          parser = subparsers.add_parser(cls.COMMAND, help="Dump model outputs to a file.")
          cls.add_arguments(parser)
          parser.set_defaults(func=cls.execute)
  
      @classmethod
      def add_arguments(cls: type, parser: argparse.ArgumentParser):
          super(DumpAction, cls).add_arguments(parser)
          parser.add_argument(
              "--output",
              metavar="<dump_file>",
              default="results.pkl",
              help="File name to save dump to",
          )
  
      @classmethod
      def execute_on_outputs(
          cls: type, context: Dict[str, Any], entry: Dict[str, Any], outputs: Instances
      ):
          image_fpath = entry["file_name"]
          logger.info(f"Processing {image_fpath}")
          result = {"file_name": image_fpath}
          if outputs.has("scores"):
              result["scores"] = outputs.get("scores").cpu()
          if outputs.has("pred_boxes"):
              result["pred_boxes_XYXY"] = outputs.get("pred_boxes").tensor.cpu()
              if outputs.has("pred_densepose"):
                  if isinstance(outputs.pred_densepose, DensePoseChartPredictorOutput):
                      extractor = DensePoseResultExtractor()
                  elif isinstance(outputs.pred_densepose, DensePoseEmbeddingPredictorOutput):
                      extractor = DensePoseOutputsExtractor()
                  result["pred_densepose"] = extractor(outputs)[0]
          context["results"].append(result)
  
      @classmethod
      def create_context(cls: type, args: argparse.Namespace, cfg: CfgNode):
          context = {"results": [], "out_fname": args.output}
          return context
  
      @classmethod
      def postexecute(cls: type, context: Dict[str, Any]):
          out_fname = context["out_fname"]
          out_dir = os.path.dirname(out_fname)
          if len(out_dir) > 0 and not os.path.exists(out_dir):
              os.makedirs(out_dir)
          with open(out_fname, "wb") as hFile:
              pickle.dump(context["results"], hFile)
              logger.info(f"Output saved to {out_fname}")
  
  
  def create_argument_parser() -> argparse.ArgumentParser:
      parser = argparse.ArgumentParser(
          description=DOC,
          formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=120),
      )
      parser.set_defaults(func=lambda _: parser.print_help(sys.stdout))
      subparsers = parser.add_subparsers(title="Actions")
      for _, action in _ACTION_REGISTRY.items():
          action.add_parser(subparsers)
      return parser
  
  
  def main():
      parser = create_argument_parser()
      args = parser.parse_args()
      verbosity = args.verbosity if hasattr(args, "verbosity") else None
      global logger
      logger = setup_logger(name=LOGGER_NAME)
      logger.setLevel(verbosity_to_level(verbosity))
      args.func(args)
  
  
  if __name__ == "__main__":
      main()