reconstruct_scene_mean.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # main imports
  2. import numpy as np
  3. import pandas as pd
  4. import os, sys, argparse
  5. # models imports
  6. from sklearn import linear_model
  7. from sklearn import svm
  8. from sklearn.utils import shuffle
  9. from joblib import dump, load
  10. # image processing imports
  11. from PIL import Image
  12. # modules and config imports
  13. sys.path.insert(0, '') # trick to enable import of main folder module
  14. import custom_config as cfg
  15. def reconstruct(_scene_name, _n):
  16. # construct the empty output image
  17. output_image = np.empty([cfg.number_of_rows, cfg.number_of_columns])
  18. # load scene and its `n` first pixel value data
  19. scene_path = os.path.join(cfg.dataset_path, _scene_name)
  20. for id_column in range(cfg.number_of_columns):
  21. folder_path = os.path.join(scene_path, str(id_column))
  22. for id_row in range(cfg.number_of_rows):
  23. pixel_filename = _scene_name + '_' + str(id_column) + '_' + str(id_row) + ".dat"
  24. pixel_file_path = os.path.join(folder_path, pixel_filename)
  25. with open(pixel_file_path, 'r') as f:
  26. # predict the expected pixel value
  27. lines = [float(l) for l in f.readlines()]
  28. mean = sum(lines[0:int(_n)]) / float(_n)
  29. output_image[id_row, id_column] = mean
  30. print("{0:.2f}%".format(id_column / cfg.number_of_columns * 100))
  31. sys.stdout.write("\033[F")
  32. return output_image
  33. def main():
  34. parser = argparse.ArgumentParser(description="Train model and saved it")
  35. parser.add_argument('--scene', type=str, help='Scene name to reconstruct', choices=cfg.scenes_list)
  36. parser.add_argument('--n', type=str, help='Number of samples to take')
  37. parser.add_argument('--image_name', type=str, help="The ouput image name")
  38. args = parser.parse_args()
  39. param_scene_name = args.scene
  40. param_n = args.n
  41. param_image_name = args.image_name
  42. output_image = reconstruct(param_scene_name, param_n)
  43. if not os.path.exists(cfg.reconstructed_folder):
  44. os.makedirs(cfg.reconstructed_folder)
  45. image_path = os.path.join(cfg.reconstructed_folder, param_image_name)
  46. img = Image.fromarray(np.uint8(output_image))
  47. img.save(image_path)
  48. if __name__== "__main__":
  49. main()