code: def main():
      # for cmd
      args = parse_args(mMain=True)
      image_dir = args.image_dir
      if is_link(image_dir):
          download_with_progressbar(image_dir, "tmp.jpg")
          image_file_list = ["tmp.jpg"]
      else:
          image_file_list = get_image_file_list(args.image_dir)
      if len(image_file_list) == 0:
          logger.error("no images find in {}".format(args.image_dir))
          return
      if args.type == "ocr":
          engine = PaddleOCR(**(args.__dict__))
      elif args.type == "structure":
          engine = PPStructure(**(args.__dict__))
      else:
          raise NotImplementedError
  
      for img_path in image_file_list:
          img_name = os.path.basename(img_path).split(".")[0]
          logger.info("{}{}{}".format("*" * 10, img_path, "*" * 10))
          if args.type == "ocr":
              result = engine.ocr(
                  img_path,
                  det=args.det,
                  rec=args.rec,
                  cls=args.use_angle_cls,
                  bin=args.binarize,
                  inv=args.invert,
                  alpha_color=args.alphacolor,
              )
              if result is not None:
                  for idx in range(len(result)):
                      res = result[idx]
                      for line in res:
                          logger.info(line)
          elif args.type == "structure":
              img, flag_gif, flag_pdf = check_and_read(img_path)
              if not flag_gif and not flag_pdf:
                  img = cv2.imread(img_path)
  
              if args.recovery and args.use_pdf2docx_api and flag_pdf:
                  from pdf2docx.converter import Converter
  
                  docx_file = os.path.join(
                      args.output, "{}.docx".format(img_name)
                  )
                  cv = Converter(img_path)
                  cv.convert(docx_file)
                  cv.close()
                  logger.info("docx save to {}".format(docx_file))
                  continue
  
              if not flag_pdf:
                  if img is None:
                      logger.error(
                          "error in loading image:{}".format(img_path)
                      )
                      continue
                  img_paths = [[img_path, img]]
              else:
                  img_paths = []
                  for index, pdf_img in enumerate(img):
                      os.makedirs(
                          os.path.join(args.output, img_name),
                          exist_ok=True,
                      )
                      pdf_img_path = os.path.join(
                          args.output,
                          img_name,
                          img_name + "_" + str(index) + ".jpg",
                      )
                      cv2.imwrite(pdf_img_path, pdf_img)
                      img_paths.append([pdf_img_path, pdf_img])
  
              all_res = []
              for index, (new_img_path, img) in enumerate(img_paths):
                  logger.info(
                      "processing {}/{} page:".format(
                          index + 1, len(img_paths)
                      )
                  )
                  new_img_name = os.path.basename(new_img_path).split(
                      "."
                  )[0]
                  result = engine(img, img_idx=index)
                  save_structure_res(
                      result, args.output, img_name, index
                  )
  
                  if args.recovery and result != []:
                      from copy import deepcopy
                      from ppstructure.recovery.recovery_to_doc import (
                          sorted_layout_boxes,
                      )
  
                      h, w, _ = img.shape
                      result_cp = deepcopy(result)
                      result_sorted = sorted_layout_boxes(result_cp, w)
                      all_res += result_sorted
  
              if args.recovery and all_res != []:
                  try:
                      from ppstructure.recovery.recovery_to_doc import (
                          convert_info_docx,
                      )
  
                      convert_info_docx(
                          img, all_res, args.output, img_name
                      )
                  except Exception as ex:
                      logger.error(
                          "error in layout recovery image:{}, err msg: {}".format(
                              img_name, ex
                          )
                      )
                      continue
  
              for item in all_res:
                  item.pop("img")
                  item.pop("res")
                  logger.info(item)
              logger.info("result save to {}".format(args.output))
  
imports: import os
  import sys
  import importlib
  import paddle
  import cv2
  import logging
  import numpy
  from pathlib import Path
  import base64
  from io import BytesIO
  from PIL import Image
  from ppocr.utils.logging import get_logger
  from tools.infer import predict_system
  from ppocr.utils.utility import check_and_read, get_image_file_list, alpha_to_color, binarize_img
  from ppocr.utils.network import maybe_download, download_with_progressbar, is_link, confirm_model_dir_url
  from tools.infer.utility import draw_ocr, str2bool, check_gpu
  from ppstructure.utility import init_args, draw_structure_result
  from ppstructure.predict_system import StructureSystem, save_structure_res, to_excel
label_desc: Use image node_id 7 for calling a global function with example usage: main() without return types