corner_data_generator.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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("--dataset", default="smartdoc", help="'smartdoc' or 'selfcollected' dataset")
  15. return parser.parse_args()
  16. if __name__ == '__main__':
  17. args = args_processor()
  18. input_directory = args.input_dir
  19. if not os.path.isdir(args.output_dir):
  20. os.mkdir(args.output_dir)
  21. import csv
  22. # Dataset iterator
  23. if args.dataset=="smartdoc":
  24. dataset_test = dataprocessor.dataset.SmartDocDirectories(input_directory)
  25. elif args.dataset=="selfcollected":
  26. dataset_test = dataprocessor.dataset.SelfCollectedDataset(input_directory)
  27. else:
  28. print ("Incorrect dataset type; please choose between smartdoc or selfcollected")
  29. assert(False)
  30. with open(os.path.join(args.output_dir, 'gt.csv'), 'a', newline='') as csvfile:
  31. spamwriter = csv.writer(csvfile, delimiter=',',
  32. quotechar='|')
  33. # Counter for file naming
  34. counter = 0
  35. for data_elem in dataset_test.myData:
  36. img_path = data_elem[0]
  37. target = data_elem[1].reshape((4, 2))
  38. img = cv2.imread(img_path)
  39. if args.dataset=="selfcollected":
  40. target = target / (img.shape[1], img.shape[0])
  41. target = target * (1920, 1920)
  42. img = cv2.resize(img, (1920, 1920))
  43. corner_cords = target
  44. for angle in range(0, 90, 1):
  45. print(angle)
  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. # cv2.circle(img_store, tuple(list((np.array(gt_store)*64).astype(int))), 2, (255, 0, 0), 2)
  56. cv2.imwrite(os.path.join(args.output_dir, f_name + ".jpg"),
  57. img_store, [int(cv2.IMWRITE_JPEG_QUALITY), 80])
  58. spamwriter.writerow((f_name + ".jpg", tuple(gt_store)))