code: def predict(
      detection_model: DetectionModel = None,
      model_type: str = "mmdet",
      model_path: str = None,
      model_config_path: str = None,
      model_confidence_threshold: float = 0.25,
      model_device: str = None,
      model_category_mapping: dict = None,
      model_category_remapping: dict = None,
      source: str = None,
      no_standard_prediction: bool = False,
      no_sliced_prediction: bool = False,
      image_size: int = None,
      slice_height: int = 512,
      slice_width: int = 512,
      overlap_height_ratio: float = 0.2,
      overlap_width_ratio: float = 0.2,
      postprocess_type: str = "GREEDYNMM",
      postprocess_match_metric: str = "IOS",
      postprocess_match_threshold: float = 0.5,
      postprocess_class_agnostic: bool = False,
      novisual: bool = False,
      view_video: bool = False,
      frame_skip_interval: int = 0,
      export_pickle: bool = False,
      export_crop: bool = False,
      dataset_json_path: bool = None,
      project: str = "runs/predict",
      name: str = "exp",
      visual_bbox_thickness: int = None,
      visual_text_size: float = None,
      visual_text_thickness: int = None,
      visual_hide_labels: bool = False,
      visual_hide_conf: bool = False,
      visual_export_format: str = "png",
      verbose: int = 1,
      return_dict: bool = False,
      force_postprocess_type: bool = False,
      **kwargs,
  ):
      """
      Performs prediction for all present images in given folder.
  
      Args:
          detection_model: sahi.model.DetectionModel
              Optionally provide custom DetectionModel to be used for inference. When provided,
              model_type, model_path, config_path, model_device, model_category_mapping, image_size
              params will be ignored
          model_type: str
              mmdet for 'MmdetDetectionModel', 'yolov5' for 'Yolov5DetectionModel'.
          model_path: str
              Path for the model weight
          model_config_path: str
              Path for the detection model config file
          model_confidence_threshold: float
              All predictions with score < model_confidence_threshold will be discarded.
          model_device: str
              Torch device, "cpu" or "cuda"
          model_category_mapping: dict
              Mapping from category id (str) to category name (str) e.g. {"1": "pedestrian"}
          model_category_remapping: dict: str to int
              Remap category ids after performing inference
          source: str
              Folder directory that contains images or path of the image to be predicted. Also video to be predicted.
          no_standard_prediction: bool
              Dont perform standard prediction. Default: False.
          no_sliced_prediction: bool
              Dont perform sliced prediction. Default: False.
          image_size: int
              Input image size for each inference (image is scaled by preserving asp. rat.).
          slice_height: int
              Height of each slice.  Defaults to ``512``.
          slice_width: int
              Width of each slice.  Defaults to ``512``.
          overlap_height_ratio: float
              Fractional overlap in height of each window (e.g. an overlap of 0.2 for a window
              of size 512 yields an overlap of 102 pixels).
              Default to ``0.2``.
          overlap_width_ratio: float
              Fractional overlap in width of each window (e.g. an overlap of 0.2 for a window
              of size 512 yields an overlap of 102 pixels).
              Default to ``0.2``.
          postprocess_type: str
              Type of the postprocess to be used after sliced inference while merging/eliminating predictions.
              Options are 'NMM', 'GREEDYNMM', 'LSNMS' or 'NMS'. Default is 'GRREDYNMM'.
          postprocess_match_metric: str
              Metric to be used during object prediction matching after sliced prediction.
              'IOU' for intersection over union, 'IOS' for intersection over smaller area.
          postprocess_match_threshold: float
              Sliced predictions having higher iou than postprocess_match_threshold will be
              postprocessed after sliced prediction.
          postprocess_class_agnostic: bool
              If True, postprocess will ignore category ids.
          novisual: bool
              Dont export predicted video/image visuals.
          view_video: bool
              View result of prediction during video inference.
          frame_skip_interval: int
              If view_video or export_visual is slow, you can process one frames of 3(for exp: --frame_skip_interval=3).
          export_pickle: bool
              Export predictions as .pickle
          export_crop: bool
              Export predictions as cropped images.
          dataset_json_path: str
              If coco file path is provided, detection results will be exported in coco json format.
          project: str
              Save results to project/name.
          name: str
              Save results to project/name.
          visual_bbox_thickness: int
          visual_text_size: float
          visual_text_thickness: int
          visual_hide_labels: bool
          visual_hide_conf: bool
          visual_export_format: str
              Can be specified as 'jpg' or 'png'
          verbose: int
              0: no print
              1: print slice/prediction durations, number of slices
              2: print model loading/file exporting durations
          return_dict: bool
              If True, returns a dict with 'export_dir' field.
          force_postprocess_type: bool
              If True, auto postprocess check will e disabled
      """
      # assert prediction type
      if no_standard_prediction and no_sliced_prediction:
          raise ValueError(
              "'no_standard_prediction' and 'no_sliced_prediction' cannot be True at the same time."
          )
  
      # auto postprocess type
      if (
          not force_postprocess_type
          and model_confidence_threshold < LOW_MODEL_CONFIDENCE
          and postprocess_type != "NMS"
      ):
          logger.warning(
              f"Switching postprocess type/metric to NMS/IOU since confidence threshold is low ({model_confidence_threshold})."
          )
          postprocess_type = "NMS"
          postprocess_match_metric = "IOU"
  
      # for profiling
      durations_in_seconds = dict()
  
      # init export directories
      save_dir = Path(
          increment_path(Path(project) / name, exist_ok=False)
      )  # increment run
      crop_dir = save_dir / "crops"
      visual_dir = save_dir / "visuals"
      visual_with_gt_dir = save_dir / "visuals_with_gt"
      pickle_dir = save_dir / "pickles"
      if (
          not novisual
          or export_pickle
          or export_crop
          or dataset_json_path is not None
      ):
          save_dir.mkdir(parents=True, exist_ok=True)  # make dir
  
      # init image iterator
      # TODO: rewrite this as iterator class as in https://github.com/ultralytics/yolov5/blob/d059d1da03aee9a3c0059895aa4c7c14b7f25a9e/utils/datasets.py#L178
      source_is_video = False
      num_frames = None
      if dataset_json_path:
          coco: Coco = Coco.from_coco_dict_or_path(dataset_json_path)
          image_iterator = [
              str(Path(source) / Path(coco_image.file_name))
              for coco_image in coco.images
          ]
          coco_json = []
      elif os.path.isdir(source):
          image_iterator = list_files(
              directory=source,
              contains=IMAGE_EXTENSIONS,
              verbose=verbose,
          )
      elif Path(source).suffix in VIDEO_EXTENSIONS:
          source_is_video = True
          (
              read_video_frame,
              output_video_writer,
              video_file_name,
              num_frames,
          ) = get_video_reader(
              source,
              save_dir,
              frame_skip_interval,
              not novisual,
              view_video,
          )
          image_iterator = read_video_frame
      else:
          image_iterator = [source]
  
      # init model instance
      time_start = time.time()
      if detection_model is None:
          detection_model = AutoDetectionModel.from_pretrained(
              model_type=model_type,
              model_path=model_path,
              config_path=model_config_path,
              confidence_threshold=model_confidence_threshold,
              device=model_device,
              category_mapping=model_category_mapping,
              category_remapping=model_category_remapping,
              load_at_init=False,
              image_size=image_size,
              **kwargs,
          )
          detection_model.load_model()
      time_end = time.time() - time_start
      durations_in_seconds["model_load"] = time_end
  
      # iterate over source images
      durations_in_seconds["prediction"] = 0
      durations_in_seconds["slice"] = 0
  
      input_type_str = "video frames" if source_is_video else "images"
      for ind, image_path in enumerate(
          tqdm(
              image_iterator,
              f"Performing inference on {input_type_str}",
              total=num_frames,
          )
      ):
          # get filename
          if source_is_video:
              video_name = Path(source).stem
              relative_filepath = video_name + "_frame_" + str(ind)
          elif os.path.isdir(
              source
          ):  # preserve source folder structure in export
              relative_filepath = str(Path(image_path)).split(
                  str(Path(source))
              )[-1]
              relative_filepath = (
                  relative_filepath[1:]
                  if relative_filepath[0] == os.sep
                  else relative_filepath
              )
          else:  # no process if source is single file
              relative_filepath = Path(image_path).name
  
          filename_without_extension = Path(relative_filepath).stem
  
          # load image
          image_as_pil = read_image_as_pil(image_path)
  
          # perform prediction
          if not no_sliced_prediction:
              # get sliced prediction
              prediction_result = get_sliced_prediction(
                  image=image_as_pil,
                  detection_model=detection_model,
                  slice_height=slice_height,
                  slice_width=slice_width,
                  overlap_height_ratio=overlap_height_ratio,
                  overlap_width_ratio=overlap_width_ratio,
                  perform_standard_pred=not no_standard_prediction,
                  postprocess_type=postprocess_type,
                  postprocess_match_metric=postprocess_match_metric,
                  postprocess_match_threshold=postprocess_match_threshold,
                  postprocess_class_agnostic=postprocess_class_agnostic,
                  verbose=1 if verbose else 0,
              )
              object_prediction_list = (
                  prediction_result.object_prediction_list
              )
              durations_in_seconds[
                  "slice"
              ] += prediction_result.durations_in_seconds["slice"]
          else:
              # get standard prediction
              prediction_result = get_prediction(
                  image=image_as_pil,
                  detection_model=detection_model,
                  shift_amount=[0, 0],
                  full_shape=None,
                  postprocess=None,
                  verbose=0,
              )
              object_prediction_list = (
                  prediction_result.object_prediction_list
              )
  
          durations_in_seconds[
              "prediction"
          ] += prediction_result.durations_in_seconds["prediction"]
          # Show prediction time
          if verbose:
              tqdm.write(
                  "Prediction time is: {:.2f} ms".format(
                      prediction_result.durations_in_seconds[
                          "prediction"
                      ]
                      * 1000
                  )
              )
  
          if dataset_json_path:
              if source_is_video is True:
                  raise NotImplementedError(
                      "Video input type not supported with coco formatted dataset json"
                  )
  
              # append predictions in coco format
              for object_prediction in object_prediction_list:
                  coco_prediction = (
                      object_prediction.to_coco_prediction()
                  )
                  coco_prediction.image_id = coco.images[ind].id
                  coco_prediction_json = coco_prediction.json
                  if coco_prediction_json["bbox"]:
                      coco_json.append(coco_prediction_json)
              if not novisual:
                  # convert ground truth annotations to object_prediction_list
                  coco_image: CocoImage = coco.images[ind]
                  object_prediction_gt_list: List[ObjectPrediction] = []
                  for coco_annotation in coco_image.annotations:
                      coco_annotation_dict = coco_annotation.json
                      category_name = coco_annotation.category_name
                      full_shape = [coco_image.height, coco_image.width]
                      object_prediction_gt = (
                          ObjectPrediction.from_coco_annotation_dict(
                              annotation_dict=coco_annotation_dict,
                              category_name=category_name,
                              full_shape=full_shape,
                          )
                      )
                      object_prediction_gt_list.append(
                          object_prediction_gt
                      )
                  # export visualizations with ground truths
                  output_dir = str(
                      visual_with_gt_dir
                      / Path(relative_filepath).parent
                  )
                  color = (0, 255, 0)  # original annotations in green
                  result = visualize_object_predictions(
                      np.ascontiguousarray(image_as_pil),
                      object_prediction_list=object_prediction_gt_list,
                      rect_th=visual_bbox_thickness,
                      text_size=visual_text_size,
                      text_th=visual_text_thickness,
                      color=color,
                      hide_labels=visual_hide_labels,
                      hide_conf=visual_hide_conf,
                      output_dir=None,
                      file_name=None,
                      export_format=None,
                  )
                  color = (255, 0, 0)  # model predictions in red
                  _ = visualize_object_predictions(
                      result["image"],
                      object_prediction_list=object_prediction_list,
                      rect_th=visual_bbox_thickness,
                      text_size=visual_text_size,
                      text_th=visual_text_thickness,
                      color=color,
                      hide_labels=visual_hide_labels,
                      hide_conf=visual_hide_conf,
                      output_dir=output_dir,
                      file_name=filename_without_extension,
                      export_format=visual_export_format,
                  )
  
          time_start = time.time()
          # export prediction boxes
          if export_crop:
              output_dir = str(
                  crop_dir / Path(relative_filepath).parent
              )
              crop_object_predictions(
                  image=np.ascontiguousarray(image_as_pil),
                  object_prediction_list=object_prediction_list,
                  output_dir=output_dir,
                  file_name=filename_without_extension,
                  export_format=visual_export_format,
              )
          # export prediction list as pickle
          if export_pickle:
              save_path = str(
                  pickle_dir
                  / Path(relative_filepath).parent
                  / (filename_without_extension + ".pickle")
              )
              save_pickle(
                  data=object_prediction_list, save_path=save_path
              )
  
          # export visualization
          if not novisual or view_video:
              output_dir = str(
                  visual_dir / Path(relative_filepath).parent
              )
              result = visualize_object_predictions(
                  np.ascontiguousarray(image_as_pil),
                  object_prediction_list=object_prediction_list,
                  rect_th=visual_bbox_thickness,
                  text_size=visual_text_size,
                  text_th=visual_text_thickness,
                  hide_labels=visual_hide_labels,
                  hide_conf=visual_hide_conf,
                  output_dir=output_dir
                  if not source_is_video
                  else None,
                  file_name=filename_without_extension,
                  export_format=visual_export_format,
              )
              if not novisual and source_is_video:  # export video
                  output_video_writer.write(result["image"])
  
          # render video inference
          if view_video:
              cv2.imshow(
                  "Prediction of {}".format(str(video_file_name)),
                  result["image"],
              )
              cv2.waitKey(1)
  
          time_end = time.time() - time_start
          durations_in_seconds["export_files"] = time_end
  
      # export coco results
      if dataset_json_path:
          save_path = str(save_dir / "result.json")
          save_json(coco_json, save_path)
  
      if (
          not novisual
          or export_pickle
          or export_crop
          or dataset_json_path is not None
      ):
          print(
              f"Prediction results are successfully exported to {save_dir}"
          )
  
      # print prediction duration
      if verbose == 2:
          print(
              "Model loaded in",
              durations_in_seconds["model_load"],
              "seconds.",
          )
          print(
              "Slicing performed in",
              durations_in_seconds["slice"],
              "seconds.",
          )
          print(
              "Prediction performed in",
              durations_in_seconds["prediction"],
              "seconds.",
          )
          if not novisual:
              print(
                  "Exporting performed in",
                  durations_in_seconds["export_files"],
                  "seconds.",
              )
  
      if return_dict:
          return {"export_dir": save_dir}
  
imports: import logging
  import os
  import time
  from typing import List, Optional
  from sahi.utils.import_utils import is_available
  from functools import cmp_to_key
  import numpy
  from tqdm import tqdm
  from sahi.auto_model import AutoDetectionModel
  from sahi.models.base import DetectionModel
  from sahi.postprocess.combine import GreedyNMMPostprocess, LSNMSPostprocess, NMMPostprocess, NMSPostprocess, PostprocessPredictions
  from sahi.prediction import ObjectPrediction, PredictionResult
  from sahi.slicing import slice_image
  from sahi.utils.coco import Coco, CocoImage
  from sahi.utils.cv import IMAGE_EXTENSIONS, VIDEO_EXTENSIONS, crop_object_predictions, cv2, get_video_reader, read_image_as_pil, visualize_object_predictions
  from sahi.utils.file import Path, increment_path, list_files, save_json, save_pickle
  from sahi.utils.import_utils import check_requirements
label_desc: Use image node_id 5 for calling a global function with example usage: predict(detection_model, model_type, model_path, model_config_path, model_confidence_threshold, model_device, model_category_mapping, model_category_remapping, source, no_standard_prediction, no_sliced_prediction, image_size, slice_height, slice_width, overlap_height_ratio, overlap_width_ratio, postprocess_type, postprocess_match_metric, postprocess_match_threshold, postprocess_class_agnostic, novisual, view_video, frame_skip_interval, export_pickle, export_crop, dataset_json_path, project, name, visual_bbox_thickness, visual_text_size, visual_text_thickness, visual_hide_labels, visual_hide_conf, visual_export_format, verbose, return_dict, force_postprocess_type) and returns: dict