utils.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # main imports
  2. import sys, math, os
  3. # processing imports
  4. import matplotlib.pyplot as plt
  5. from PIL import Image
  6. # modules imports
  7. sys.path.insert(0, '') # trick to enable import of main folder module
  8. import custom_config as cfg
  9. min_x = 100
  10. min_y = 100
  11. # utils variables
  12. zone_width, zone_height = cfg.image_zone_size
  13. scene_width, scene_height = cfg.image_scene_size
  14. nb_x_parts = math.floor(scene_width / zone_width)
  15. def get_zone_index(p_x, p_y):
  16. zone_index = math.floor(p_x / zone_width) + math.floor(p_y / zone_height) * nb_x_parts
  17. return zone_index
  18. def check_coordinates(p_x, p_y):
  19. if p_x < 0 or p_y < 0:
  20. return False
  21. if p_x >= scene_width or p_y >= scene_height:
  22. return False
  23. return True
  24. def extract_click_coordinate(line):
  25. data = line.split(' : ')[1].split(',')
  26. p_x, p_y = (int(data[0]), int(data[1]))
  27. return (p_x, p_y)
  28. def save_img_plot(scene_name, x_points, y_points, title, img_path):
  29. folder_scene = os.path.join(cfg.dataset_path, scene_name)
  30. images = [img for img in os.listdir(folder_scene) if '.png' in img]
  31. images = sorted(images)
  32. first_image_path = os.path.join(folder_scene, images[0])
  33. img = Image.open(first_image_path)
  34. plt.rcParams["figure.figsize"] = (20, 20)
  35. # Save here data information about subject
  36. plt.title(title, fontsize=30)
  37. plt.imshow(img)
  38. plt.scatter(x_points, y_points, color='red')
  39. for x_i, x in enumerate(cfg.zone_coodinates):
  40. plt.plot([x_i * 200, x_i * 200], [0, 800], color='blue')
  41. for y_i, y in enumerate(cfg.zone_coodinates):
  42. plt.plot([0, 800], [y_i * 200, y_i * 200], color='blue')
  43. plt.axis('off')
  44. plt.savefig(img_path, dpi=100)