text: import torch
  import torch.backends.cudnn as cudnn
  import torch.utils.data
  import torch.nn.functional as F
  from AlphanumericCharacterDetection.utils import CTCLabelConverter, AttnLabelConverter
  from AlphanumericCharacterDetection.dataset import RawDataset, AlignCollate
  from AlphanumericCharacterDetection.model import Model
  import time
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  cudnn.benchmark = True
  cudnn.deterministic = True
  from string import printable
  class driver():
      def __init__(self):
          self.image_folder = ""
          self.workers = 4
          self.batch_size = 192
          self.saved_model = "TPS-ResNet-BiLSTM-Attn-case-sensitive.pth"
          self.batch_max_length = 25
          self.imgH = 32
          self.imgW = 100
          self.rgb = False
          self.character = "0123456789abcdefghijklmnopqrstuvwxyz"
          self.sensitive = True
          self.PAD = False
          self.Transformation = "TPS"
          self.FeatureExtraction = "ResNet"
          self.SequenceModeling = "BiLSTM"
          self.Prediction = "Attn"
          self.num_fiducial = 20
          self.input_channel = 1
          self.output_channel = 512
          self.hidden_size = 256
          self.num_gpu = torch.cuda.device_count()
  
  opt = driver()
  if opt.sensitive:
  	opt.character = printable[:-6]  # same with ASTER setting (use 94 char)
  if 'CTC' in opt.Prediction:
      converter = CTCLabelConverter(opt.character)
  else:
      converter = AttnLabelConverter(opt.character)
  opt.num_class = len(converter.character)
  model = Model(opt)
  print('model input parameters', opt.imgH, opt.imgW, opt.num_fiducial, opt.input_channel, opt.output_channel,
            opt.hidden_size, opt.num_class, opt.batch_max_length, opt.Transformation, opt.FeatureExtraction,
            opt.SequenceModeling, opt.Prediction)
  model = torch.nn.DataParallel(model).to(device)
  
  # load model
  print('loading pretrained model from %s' % opt.saved_model)
  model.load_state_dict(torch.load("AlphanumericCharacterDetection/saved_models/" + opt.saved_model, map_location=device))
  
      # prepare data. two demo images from https://github.com/bgshih/crnn#run-demo
  AlignCollate_demo = AlignCollate(imgH=opt.imgH, imgW=opt.imgW, keep_ratio_with_pad=opt.PAD)
  
  def Recognize(directory):
      global opt
      global model
      global AlignCollate_demo
      opt.image_folder = directory
  
      if opt.rgb:
          opt.input_channel = 3
      demo_data = RawDataset(root=opt.image_folder, opt=opt)  # use RawDataset
      demo_loader = torch.utils.data.DataLoader(
          demo_data, batch_size=opt.batch_size,
          shuffle=False,
          num_workers=int(opt.workers),
          collate_fn=AlignCollate_demo, pin_memory=True)
      results = []
      # predict
      model.eval()
      with torch.no_grad():
          for image_tensors, image_path_list in demo_loader:
              batch_size = image_tensors.size(0)
              image = image_tensors.to(device)
              # For max length prediction
              length_for_pred = torch.IntTensor([opt.batch_max_length] * batch_size).to(device)
              text_for_pred = torch.LongTensor(batch_size, opt.batch_max_length + 1).fill_(0).to(device)
              if 'CTC' in opt.Prediction:
                  preds = model(image, text_for_pred)
                  # Select max probabilty (greedy decoding) then decode index to character
                  preds_size = torch.IntTensor([preds.size(1)] * batch_size)
                  _, preds_index = preds.max(2)
                  # preds_index = preds_index.view(-1)
                  preds_str = converter.decode(preds_index, preds_size)
              else:
                  preds = model(image, text_for_pred, is_train=False)
                  # select max probabilty (greedy decoding) then decode index to character
                  _, preds_index = preds.max(2)
                  preds_str = converter.decode(preds_index, length_for_pred)
              preds_prob = F.softmax(preds, dim=2)
              preds_max_prob, _ = preds_prob.max(dim=2)
              for pred, pred_max_prob in zip(preds_str, preds_max_prob):
                  if 'Attn' in opt.Prediction:
                      pred_EOS = pred.find('[s]')
                      pred = pred[:pred_EOS]  # prune after "end of sentence" token ([s])
                      pred_max_prob = pred_max_prob[:pred_EOS]
                  # calculate confidence score (= multiply of pred_max_prob)
                  confidence_score = pred_max_prob.cumprod(dim=0)[-1]
                  results.append((pred,confidence_score))
      return results
  
  