augmentData.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import os
  2. import cv2
  3. import numpy as np
  4. import utils
  5. def argsProcessor():
  6. import argparse
  7. parser = argparse.ArgumentParser()
  8. parser.add_argument("-i", "--dataPath", help="DataPath")
  9. parser.add_argument("-o", "--outputFiles", help="outputFiles", default="bar")
  10. return parser.parse_args()
  11. args = argsProcessor()
  12. output_dir = args.outputFiles
  13. if (not os.path.isdir(output_dir)):
  14. os.mkdir(output_dir)
  15. dir = args.dataPath
  16. import csv
  17. with open(output_dir+"/gt.csv", 'a') as csvfile:
  18. spamwriter_1 = csv.writer(csvfile, delimiter=',',
  19. quotechar='|', quoting=csv.QUOTE_MINIMAL)
  20. for image in os.listdir(dir):
  21. if image.endswith("jpg") or image.endswith("JPG"):
  22. if os.path.isfile(dir+"/"+image+".csv"):
  23. with open(dir+"/"+image+ ".csv", 'r') as csvfile:
  24. spamwriter = csv.reader(csvfile, delimiter=' ',
  25. quotechar='|', quoting=csv.QUOTE_MINIMAL)
  26. img = cv2.imread(dir +"/"+ image)
  27. print (image)
  28. gt= []
  29. for row in spamwriter:
  30. gt.append(row)
  31. # img = cv2.circle(img, (int(float(row[0])), int(float(row[1]))), 2,(255,0,0),90)
  32. gt =np.array(gt).astype(np.float32)
  33. gt = gt / (img.shape[1], img.shape[0])
  34. gt = gt * (1080, 1080)
  35. img = cv2.resize(img, (1080, 1080))
  36. print (gt)
  37. for angle in range(0,271,90):
  38. img_rotate, gt_rotate = utils.rotate(img, gt, angle)
  39. for random_crop in range(0,16):
  40. img_crop, gt_crop = utils.random_crop(img_rotate, gt_rotate)
  41. mah_size = img_crop.shape
  42. img_crop = cv2.resize(img_crop, (64, 64))
  43. gt_crop = np.array(gt_crop)
  44. # gt_crop = gt_crop*(1.0 / mah_size[1],1.0 / mah_size[0])
  45. # for a in range(0,4):
  46. # no=0
  47. # for a in range(0,4):
  48. # no+=1
  49. # cv2.circle(img_crop, tuple(((gt_crop[a]*64).astype(int))), 2,(255-no*60,no*60,0),9)
  50. # # # cv2.imwrite("asda.jpg", img)
  51. cv2.imwrite(output_dir + "/" +str(angle)+str(random_crop)+ image, img_crop)
  52. spamwriter_1.writerow((str(angle)+str(random_crop)+ image, tuple(list(gt_crop))))