corner_data_generator.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ''' Document Localization using Recursive CNN
  2. Maintainer : Khurram Javed
  3. Email : kjaved@ualberta.ca '''
  4. import os
  5. import cv2
  6. import numpy as np
  7. import dataprocessor
  8. from utils import utils
  9. def args_processor():
  10. import argparse
  11. parser = argparse.ArgumentParser()
  12. parser.add_argument("-i", "--input-dir", help="Path to data files (Extract images using video_to_image.py first")
  13. parser.add_argument("-o", "--output-dir", help="Directory to store results")
  14. parser.add_argument("-v", "--visualize", help="Draw the point on the corner", default=False, type=bool)
  15. parser.add_argument("--dataset", default="smartdoc", help="'smartdoc' or 'selfcollected' dataset")
  16. return parser.parse_args()
  17. if __name__ == '__main__':
  18. args = args_processor()
  19. input_directory = args.input_dir
  20. if not os.path.isdir(args.output_dir):
  21. os.mkdir(args.output_dir)
  22. import csv
  23. # Dataset iterator
  24. if args.dataset=="smartdoc":
  25. dataset_test = dataprocessor.dataset.SmartDocDirectories(input_directory)
  26. elif args.dataset=="selfcollected":
  27. dataset_test = dataprocessor.dataset.SelfCollectedDataset(input_directory)
  28. else:
  29. print ("Incorrect dataset type; please choose between smartdoc or selfcollected")
  30. assert(False)
  31. with open(os.path.join(args.output_dir, 'gt.csv'), 'a') as csvfile:
  32. spamwriter = csv.writer(csvfile, delimiter=',',
  33. quotechar='|', quoting=csv.QUOTE_MINIMAL)
  34. # Counter for file naming
  35. counter = 0
  36. for data_elem in dataset_test.myData:
  37. img_path = data_elem[0]
  38. target = data_elem[1].reshape((4, 2))
  39. img = cv2.imread(img_path)
  40. if args.dataset=="selfcollected":
  41. target = target / (img.shape[1], img.shape[0])
  42. target = target * (1920, 1920)
  43. img = cv2.resize(img, (1920, 1920))
  44. corner_cords = target
  45. for angle in range(0, 1, 90):
  46. img_rotate, gt_rotate = utils.rotate(img, corner_cords, angle)
  47. for random_crop in range(0, 1):
  48. img_list, gt_list = utils.get_corners(img_rotate, gt_rotate)
  49. for a in range(0, 4):
  50. counter += 1
  51. f_name = str(counter).zfill(8)
  52. # print(gt_list[a])
  53. gt_store = list(np.array(gt_list[a]) / (300, 300))
  54. img_store = cv2.resize(img_list[a], (64, 64))
  55. if args.visualize:
  56. cv2.circle(img_store, tuple(list((np.array(gt_store)*64).astype(int))), 2, (255, 0, 0), 2)
  57. cv2.imwrite(os.path.join(args.output_dir, f_name + ".jpg"),
  58. img_store, [int(cv2.IMWRITE_JPEG_QUALITY), 80])
  59. spamwriter.writerow((f_name + ".jpg", tuple(gt_store)))
  60. print(f_name + ".jpg done!")