doc_scan_infer.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import cv2
  2. import numpy as np
  3. import glob
  4. import evaluation
  5. import os
  6. import shutil
  7. import time
  8. def args_processor():
  9. import argparse
  10. parser = argparse.ArgumentParser()
  11. parser.add_argument("-i", "--images", default="example_imgs", help="Document image folder")
  12. parser.add_argument('--model-type', default="resnet",
  13. help='model type to be used. Example : resnet32, resnet20, densenet, test')
  14. parser.add_argument("-o", "--output", default="example_imgs/output", help="The folder to store results")
  15. parser.add_argument("-rf", "--retainFactor", help="Floating point in range (0,1) specifying retain factor",
  16. default="0.85", type=float)
  17. parser.add_argument("-cm", "--cornerModel", help="Model for corner point refinement",
  18. default="../cornerModelWell")
  19. parser.add_argument("-cp", "--cornerParams", help="Model for corner point refinement",
  20. default="../cornerModelWell")
  21. parser.add_argument("-dm", "--documentModel", help="Model for document corners detection",
  22. default="../documentModelWell")
  23. parser.add_argument("-dp", "--documentParams", help="Model for document corners detection",
  24. default="../documentModelWell")
  25. return parser.parse_args()
  26. if __name__ == "__main__":
  27. args = args_processor()
  28. corners_extractor = evaluation.corner_extractor.GetCorners(args.documentModel, args.documentParams)
  29. corner_refiner = evaluation.corner_refiner.corner_finder(args.cornerModel, args.cornerParams)
  30. outputPath = f"{args.output}/res_scan.jpg"
  31. img = cv2.imread(args.images)
  32. oImg = img
  33. extracted_corners = corners_extractor.get(oImg)
  34. corner_address = []
  35. # Refine the detected corners using corner refiner
  36. for corner in extracted_corners:
  37. corner_img = corner[0]
  38. refined_corner = np.array(corner_refiner.get_location(corner_img, args.retainFactor))
  39. # Converting from local co-ordinate to global co-ordinates of the image
  40. refined_corner[0] += corner[1]
  41. refined_corner[1] += corner[2]
  42. # Final results
  43. corner_address.append(refined_corner)
  44. for a in range(0, len(extracted_corners)):
  45. cv2.line(oImg, tuple(corner_address[a % 4]), tuple(corner_address[(a + 1) % 4]), (255, 0, 0), 4)
  46. cv2.imwrite(outputPath, oImg)