class_docstr: Prepare document data for Document Classification.
  OCR (Optical character recognition) is applied to get the document texts and bounding boxes.
  Both texts and images will be processed.
class_docstr_tok: ["Prepare","document","data","for","Document","Classification",".","OCR","(","Optical","character","recognition",")","is","applied","to","get","the","document","texts","and","bounding","boxes",".","Both","texts","and","images","will","be","processed","."]
docstr: Read documents, process them, and stack them. One sample has one document image.
  
  Parameters
  ----------
  document_features
      One sample has one document image column in a pd.DataFrame.
  feature_modalities
      What modality each column belongs to.
  is_training
      Whether to process document images in the training mode.
  image_mode
      A string which defines the type and depth of a pixel in the image.
      For example, RGB, RGBA, CMYK, and etc.
  
  Returns
  -------
  A dictionary containing one sample's document and its features.
docstr_tok: ["Read","documents",",","process","them",",","and","stack","them",".","One","sample","has","one","document","image",".","Parameters","--","--","--","--","--","document_features","One","sample","has","one","document","image","column","in","a","pd.DataFrame",".","feature_modalities","What","modality","each","column","belongs","to",".","is_training","Whether","to","process","document","images","in","the","training","mode",".","image_mode","A","string","which","defines","the","type","and","depth","of","a","pixel","in","the","image",".","For","example",",","RGB",",","RGBA",",","CMYK",",","and","etc",".","Returns","--","--","--","-","A","dictionary","containing","one","sample","'s","document","and","its","features","."]
code: def process_one_sample(
      self,
      document_features: Dict[str, Union[NDArray, list]],
      feature_modalities: Dict[str, Union[NDArray, list]],
      is_training: bool,
      image_mode: Optional[str] = "RGB",
  ):
      """
      Read documents, process them, and stack them. One sample has one document image.
  
      Parameters
      ----------
      document_features
          One sample has one document image column in a pd.DataFrame.
      feature_modalities
          What modality each column belongs to.
      is_training
          Whether to process document images in the training mode.
      image_mode
          A string which defines the type and depth of a pixel in the image.
          For example, RGB, RGBA, CMYK, and etc.
  
      Returns
      -------
      A dictionary containing one sample's document and its features.
      """
      ret = {}
      for (
          per_col_name,
          per_col_image_features,
      ) in document_features.items():
          try:
              # Process PDF documents.
              if feature_modalities[per_col_name] == DOCUMENT_PDF:
                  import fitz
  
                  # Load the pdf file.
                  pdf_doc = fitz.open(per_col_image_features[0])
                  first_page = pdf_doc.load_page(0)
                  pix = first_page.get_pixmap(matrix=fitz.Matrix(1, 1))
                  # Convert pdf into PIL images.
                  with PIL.Image.frombytes(
                      image_mode, [pix.width, pix.height], pix.samples
                  ) as doc_image:
                      doc_image = doc_image.convert(image_mode)
                      (
                          words,
                          normalized_word_boxes,
                      ) = self.get_ocr_features(
                          per_col_image_features[0], doc_image
                      )
              else:
                  # Process document image.
                  with PIL.Image.open(
                      per_col_image_features[0]
                  ) as doc_image:
                      doc_image = doc_image.convert(image_mode)
                      (
                          words,
                          normalized_word_boxes,
                      ) = self.get_ocr_features(
                          per_col_image_features[0], doc_image
                      )
          except ImportError as e:
              raise e
          except Exception as e:
              if self.missing_value_strategy.lower() == "zero":
                  logger.debug(f"Using a zero image due to '{e}'")
                  doc_image = PIL.Image.new(
                      image_mode, (self.size, self.size), color=0
                  )
                  doc_image = doc_image.convert(image_mode)
                  words = ""  # empty words
                  normalized_word_boxes = [self.pad_token_box]
              else:
                  raise e
  
          if is_training:
              doc_image = self.train_processor(doc_image)
          else:
              doc_image = self.val_processor(doc_image)
  
          if self.is_text_only_flag:
              # Padding of token_boxes up the bounding boxes to the sequence length.
              token_boxes = []
              all_tokens = []
              for word, box in zip(words, normalized_word_boxes):
                  # Tokenize bounding box separately.
                  word_tokens = self.tokenizer.tokenize(word)
                  all_tokens.append(word_tokens)
                  token_boxes.extend([box] * len(word_tokens))
  
              encoding = self.tokenizer(
                  " ".join(words),
                  padding="max_length",
                  truncation=True,
                  return_token_type_ids=True,
              )
  
              # Truncation of token_boxes
              special_tokens_count = (
                  2  # one cls token and one sep token
              )
              if (
                  len(token_boxes)
                  > self.text_max_len - special_tokens_count
              ):
                  token_boxes = token_boxes[
                      : (self.text_max_len - special_tokens_count)
                  ]
              # add bounding boxes of cls + sep tokens
              token_boxes = (
                  [self.pad_token_box]
                  + token_boxes
                  + [self.sep_token_box]
              )
  
              padding_length = self.text_max_len - sum(
                  encoding.attention_mask
              )
              token_boxes += [self.pad_token_box] * padding_length
  
          else:
              if not isinstance(words, list):
                  words = [words]
              encoding = self.tokenizer(
                  words,
                  boxes=normalized_word_boxes,
                  padding="max_length",
                  truncation=True,
                  return_token_type_ids=True,
              )
              token_boxes = encoding.bbox
  
          ret.update(
              {
                  self.text_token_ids_key: np.array(
                      encoding.input_ids, dtype=np.int32
                  ),
                  self.text_attention_mask_key: encoding.attention_mask,
                  self.text_bbox_key: np.array(
                      token_boxes, dtype=np.int32
                  ),
                  self.text_segment_ids_key: np.array(
                      encoding.token_type_ids, dtype=np.int32
                  ),
              }
          )
          if not self.is_text_only_flag:
              ret.update({self.document_pixel_value_key: doc_image})
  
      return ret
  
imports: import logging
  import os
  import warnings
  from io import BytesIO
  from typing import Any, Dict, List, Optional, Union
  import numpy
  import PIL
  import pytesseract
  import torch
  from nptyping import NDArray
  from PIL import ImageFile
  from torch import nn
  from torchvision import transforms
  from ..constants import AUTOMM, BBOX, DOCUMENT_PDF
  from .collator import PadCollator, StackCollator
  from .utils import construct_image_processor, image_mean_std
label_desc: Use image node_id 6 for calling the DocumentProcessor obj's underlying member method code with example usage: obj.process_one_sample(document_features, feature_modalities, is_training, image_mode) and returns: ret