demo.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ''' Document Localization using Recursive CNN
  2. Maintainer : Khurram Javed
  3. Email : kjaved@ualberta.ca '''
  4. import math
  5. import cv2
  6. import numpy as np
  7. import evaluation
  8. def args_processor():
  9. import argparse
  10. parser = argparse.ArgumentParser()
  11. parser.add_argument("-i", "--imagePath", default="../058.jpg", help="Path to the document image")
  12. parser.add_argument("-o", "--outputPath", default="../output.jpg", help="Path to store the result")
  13. parser.add_argument("-rf", "--retainFactor", help="Floating point in range (0,1) specifying retain factor",
  14. default="0.85")
  15. parser.add_argument("-cm", "--cornerModel", help="Model for corner point refinement",
  16. default="../cornerModelWell")
  17. parser.add_argument("-dm", "--documentModel", help="Model for document corners detection",
  18. default="../documentModelWell")
  19. return parser.parse_args()
  20. if __name__ == "__main__":
  21. args = args_processor()
  22. corners_extractor = evaluation.corner_extractor.GetCorners(args.documentModel)
  23. corner_refiner = evaluation.corner_refiner.corner_finder(args.cornerModel)
  24. img = cv2.imread(args.imagePath)
  25. oImg = img
  26. extracted_corners = corners_extractor.get(oImg)
  27. corner_address = []
  28. # Refine the detected corners using corner refiner
  29. image_name = 0
  30. for corner in extracted_corners:
  31. image_name += 1
  32. corner_img = corner[0]
  33. refined_corner = np.array(corner_refiner.get_location(corner_img, 0.85))
  34. # Converting from local co-ordinate to global co-ordinates of the image
  35. refined_corner[0] += corner[1]
  36. refined_corner[1] += corner[2]
  37. # Final results
  38. corner_address.append(refined_corner)
  39. print(corner_address)
  40. width = int(math.sqrt(math.pow(corner_address[1][0]-corner_address[0][0], 2) + math.pow(corner_address[1][1]-corner_address[0][1], 2)))
  41. height = int(math.sqrt(math.pow(corner_address[2][0]-corner_address[1][0], 2) + math.pow(corner_address[2][1]-corner_address[1][1], 2)))
  42. print(width, height)
  43. # if width > height:
  44. # t = width
  45. # width = height
  46. # height = t
  47. # print(width, height)
  48. pts1 = np.float32([[corner_address[0][0], corner_address[0][1]], [corner_address[1][0], corner_address[1][1]], [corner_address[3][0], corner_address[3][1]], [corner_address[2][0], corner_address[2][1]]])
  49. pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
  50. M = cv2.getPerspectiveTransform(pts1, pts2)
  51. warped = cv2.warpPerspective(img, M, (width, height))
  52. # cv2.imshow('ww', warped)
  53. # cv2.waitKey()
  54. # cv2.destroyAllWindows()
  55. cv2.imwrite(str(args.outputPath).split('.')[0] + '_warped.png', warped)
  56. for a in range(0, len(extracted_corners)):
  57. cv2.line(oImg, tuple(corner_address[a % 4]), tuple(corner_address[(a + 1) % 4]), (255, 0, 0), 4)
  58. cv2.imwrite(args.outputPath, oImg)