Session.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. from datetime import datetime, timedelta
  2. import pickle
  3. import random
  4. import subprocess
  5. from warnings import warn
  6. import os
  7. from tqdm import tqdm
  8. import matplotlib.image as mpimg
  9. from skimage import transform, io
  10. import IPython.display as display
  11. from py.FileUtils import list_folders, list_jpegs_recursive, verify_expected_subfolders
  12. from py.ImageUtils import display_images, get_image_date
  13. class Session:
  14. def __init__(self, folder: str):
  15. self.folder = folder
  16. # session name = folder name[33:], the first 33 characters are always the same
  17. self.name = os.path.basename(folder)[33:]
  18. print(f"Session '{self.name}' at folder: {self.folder}")
  19. assert self.name != ""
  20. verify_expected_subfolders(self.folder)
  21. self.scanned = False
  22. # maps lapse files to their exif dates (for statistic and prediction purposes)
  23. self.lapse_dates = {}
  24. # maps motion files to their exif dates (for statistic purposes)
  25. self.motion_dates = {}
  26. # maps exif dates to lapse files (for prediction purposes)
  27. self.lapse_map = {}
  28. # maps exif dates to motion files (for csv mapping purposes, generated on demand)
  29. self.motion_map = None
  30. self.load_scans()
  31. if not self.scanned:
  32. print("Session not scanned. Run session.scan() to create scan files")
  33. def load_scans(self):
  34. lapse_dates_file = os.path.join("session_scans", self.name, "lapse_dates.pickle")
  35. motion_dates_file = os.path.join("session_scans", self.name, "motion_dates.pickle")
  36. lapse_map_file = os.path.join("session_scans", self.name, "lapse_map.pickle")
  37. lapse_dates_exists = os.path.isfile(lapse_dates_file)
  38. motion_dates_exists = os.path.isfile(motion_dates_file)
  39. lapse_map_exists = os.path.isfile(lapse_map_file)
  40. if lapse_dates_exists and motion_dates_exists and lapse_map_exists:
  41. with open(lapse_dates_file, "rb") as handle:
  42. self.lapse_dates = pickle.load(handle)
  43. with open(motion_dates_file, "rb") as handle:
  44. self.motion_dates = pickle.load(handle)
  45. with open(lapse_map_file, "rb") as handle:
  46. self.lapse_map = pickle.load(handle)
  47. self.scanned = True
  48. print("Loaded scans.")
  49. else:
  50. if not (not lapse_dates_exists and not motion_dates_exists and not lapse_map_exists):
  51. warn(f"Warning: Only partial scan data available. Not loading.")
  52. self.scanned = False
  53. def save_scans(self):
  54. os.makedirs(os.path.join("session_scans", self.name), exist_ok=True)
  55. lapse_dates_file = os.path.join("session_scans", self.name, "lapse_dates.pickle")
  56. motion_dates_file = os.path.join("session_scans", self.name, "motion_dates.pickle")
  57. lapse_map_file = os.path.join("session_scans", self.name, "lapse_map.pickle")
  58. with open(lapse_dates_file, "wb") as handle:
  59. pickle.dump(self.lapse_dates, handle, protocol=pickle.HIGHEST_PROTOCOL)
  60. print(f"Saved {lapse_dates_file}")
  61. with open(motion_dates_file, "wb") as handle:
  62. pickle.dump(self.motion_dates, handle, protocol=pickle.HIGHEST_PROTOCOL)
  63. print(f"Saved {motion_dates_file}")
  64. with open(lapse_map_file, "wb") as handle:
  65. pickle.dump(self.lapse_map, handle, protocol=pickle.HIGHEST_PROTOCOL)
  66. print(f"Saved {lapse_map_file}")
  67. def scan(self, force=False, auto_save=True):
  68. if self.scanned and not force:
  69. raise ValueError("Session is already scanned. Use force=True to scan anyway and override scan progress.")
  70. # Scan motion dates
  71. print("Scanning motion dates...")
  72. self.motion_dates = {}
  73. motion_folder = os.path.join(self.folder, "Motion")
  74. for file in tqdm(list_jpegs_recursive(motion_folder)):
  75. self.motion_dates[os.path.relpath(file, motion_folder)] = get_image_date(file)
  76. # Scan lapse dates
  77. print("Scanning lapse dates...")
  78. self.lapse_dates = {}
  79. lapse_folder = os.path.join(self.folder, "Lapse")
  80. for file in tqdm(list_jpegs_recursive(lapse_folder)):
  81. self.lapse_dates[os.path.relpath(file, lapse_folder)] = get_image_date(file)
  82. # Create lapse map
  83. print("Creating lapse map...")
  84. self.lapse_map = {}
  85. for file, date in self.lapse_dates.items():
  86. if date in self.lapse_map:
  87. self.lapse_map[date].append(file)
  88. else:
  89. self.lapse_map[date] = [file]
  90. self.scanned = True
  91. # Auto save
  92. if auto_save:
  93. print("Saving...")
  94. self.save_scans()
  95. def check_lapse_duplicates(self) -> bool:
  96. total = 0
  97. total_duplicates = 0
  98. total_multiples = 0
  99. deviant_duplicates = []
  100. for date, files in tqdm(self.lapse_map.items()):
  101. total += 1
  102. if len(files) > 1:
  103. total_duplicates += 1
  104. file_size = -1
  105. for f in files:
  106. f_size = os.path.getsize(os.path.join(self.folder, "Lapse", f))
  107. if file_size == -1:
  108. file_size = f_size
  109. elif f_size != file_size:
  110. deviant_duplicates.append(date)
  111. break
  112. if len(files) > 2:
  113. total_multiples += 1
  114. deviant_duplicates.sort()
  115. print(f"* {total} lapse dates")
  116. print(f"* {total_duplicates} duplicates")
  117. print(f"* {total_multiples} multiples (more than two files per date)")
  118. print(f"* {len(deviant_duplicates)} deviant duplicates: {deviant_duplicates}")
  119. return total, total_duplicates, total_multiples, deviant_duplicates
  120. def open_images_for_date(self, date: datetime):
  121. img_names = self.lapse_map.get(date, [])
  122. if len(img_names) == 0:
  123. warn("No images for this date!")
  124. for i, img_name in enumerate(img_names):
  125. full_path = os.path.join(self.folder, "Lapse", img_name)
  126. print(f"#{i+1} {full_path}")
  127. subprocess.call(("xdg-open", full_path))
  128. def get_motion_image_from_filename(self, filename: str) -> "MotionImage":
  129. if filename in self.motion_dates:
  130. return MotionImage(self, filename, self.motion_dates[filename])
  131. else:
  132. raise ValueError(f"Unknown motion file name: {filename}")
  133. def __generate_motion_map(self):
  134. """Populates self.motion_map which maps dates to motion images
  135. """
  136. if self.motion_map is not None:
  137. return
  138. print("Generating motion map...")
  139. self.motion_map = {}
  140. for filename, date in self.motion_dates.items():
  141. if date in self.motion_map:
  142. self.motion_map[date].append(filename)
  143. else:
  144. self.motion_map[date] = [filename]
  145. def get_motion_images_from_date(self, date: datetime):
  146. self.__generate_motion_map()
  147. filenames = self.motion_map.get(date, [])
  148. return [MotionImage(self, filename, date) for filename in filenames]
  149. def get_random_motion_image(self, day_only=False, night_only=False) -> "MotionImage":
  150. if len(self.motion_dates) == 0:
  151. raise ValueError("No motion images in session!")
  152. img = None
  153. while img is None or (day_only and img.is_nighttime()) or (night_only and img.is_daytime()):
  154. filename = random.choice(list(self.motion_dates.keys()))
  155. img = MotionImage(self, filename, self.motion_dates[filename])
  156. return img
  157. def get_random_motion_image_set(self, day_only=False, night_only=False) -> list:
  158. """Returns a list of all motion images with the same date +- 10 min.
  159. The date is picked randomly from all available dates.
  160. May loop indefinitely if there are no matching motion images.
  161. Args:
  162. day_only (bool, optional): Only pick daytime images. Defaults to False.
  163. night_only (bool, optional): Only pick nighttime images. Defaults to False.
  164. Raises:
  165. ValueError: No motion images in session
  166. Returns:
  167. list: Non-empty list of motion images with the same date
  168. """
  169. self.__generate_motion_map()
  170. if len(self.motion_map) == 0:
  171. raise ValueError("No motion images in session!")
  172. imgs = []
  173. date = None
  174. while len(imgs) == 0 or (day_only and imgs[0].is_nighttime()) or (night_only and imgs[0].is_daytime()):
  175. date = random.choice(list(self.motion_map.keys()))
  176. filenames = self.motion_map.get(date, [])
  177. imgs = [MotionImage(self, filename, date) for filename in filenames]
  178. # include all images within +- 10 min
  179. for other_date in self.motion_map.keys():
  180. if date != other_date and abs((date - other_date).total_seconds()) <= 60 * 10:
  181. filenames = self.motion_map.get(other_date, [])
  182. imgs += [MotionImage(self, filename, other_date) for filename in filenames]
  183. return imgs
  184. def generate_motion_image_sets(self) -> list:
  185. self.__generate_motion_map()
  186. if len(self.motion_map) == 0:
  187. raise ValueError("No motion images in session!")
  188. imgs = []
  189. dates = sorted(list(self.motion_map.keys()))
  190. start_date = dates[0]
  191. for date in dates:
  192. if abs((date - start_date).total_seconds()) > 60 * 20:
  193. # end image time series
  194. yield imgs
  195. start_date = date
  196. imgs = []
  197. # continue time series
  198. filenames = self.motion_map.get(date, [])
  199. imgs += [MotionImage(self, filename, date) for filename in filenames]
  200. # end of all time series
  201. yield imgs
  202. def generate_motion_images(self):
  203. """Yields all motion images in this session.
  204. Yields:
  205. MotionImage: A MotionImage
  206. """
  207. for file, date in self.motion_dates.items():
  208. yield MotionImage(self, file, date)
  209. def get_closest_lapse_images(self, motion_file: str):
  210. """Returns the lapse images taken closest before and after this image, respectively.
  211. If no such image is found, the corresponding returned image will be None.
  212. Args:
  213. motion_file (str): Filename of the motion image
  214. Returns:
  215. (MotionImage or None, MotionImage or None): Closest lapse images. Each image can be None if not found.
  216. """
  217. date: datetime = self.motion_dates[motion_file]
  218. previous_date = date.replace(minute=0, second=0)
  219. next_date = previous_date + timedelta(hours=1)
  220. i = 0
  221. while not previous_date in self.lapse_map:
  222. previous_date -= timedelta(hours=1)
  223. i += 1
  224. if i > 24:
  225. # no previous lapse image exists
  226. previous_date = None
  227. break
  228. i = 0
  229. while not next_date in self.lapse_map:
  230. next_date += timedelta(hours=1)
  231. i += 1
  232. if i > 24:
  233. # no next lapse image exists
  234. next_date = None
  235. break
  236. if previous_date is not None and len(self.lapse_map[previous_date]) > 1:
  237. warn(f"There are multiple lapse images for date {previous_date}! Choosing the first one.")
  238. if next_date is not None and len(self.lapse_map[next_date]) > 1:
  239. warn(f"There are multiple lapse images for date {next_date}! Choosing the first one.")
  240. previous_img = None if previous_date is None else LapseImage(self, self.lapse_map[previous_date][0], previous_date)
  241. next_img = None if next_date is None else LapseImage(self, self.lapse_map[next_date][0], next_date)
  242. return previous_img, next_img
  243. class SessionImage:
  244. def __init__(self, session: Session, subfolder: str, filename: str, date: datetime):
  245. self.session = session
  246. self.subfolder = subfolder
  247. self.filename = filename
  248. self.date = date
  249. if not os.path.isfile(self.get_full_path()):
  250. raise ValueError(f"File {subfolder}/{filename} in session folder {session.folder} not found!")
  251. def get_full_path(self) -> str:
  252. return os.path.join(self.session.folder, self.subfolder, self.filename)
  253. def open(self):
  254. full_path = self.get_full_path()
  255. print(f"Opening {full_path}...")
  256. subprocess.call(("xdg-open", full_path))
  257. def read(self, truncate_y = (40, 40), scale=1, gray=True):
  258. full_path = self.get_full_path()
  259. img = io.imread(full_path, as_gray=gray)
  260. # truncate
  261. if truncate_y is not None:
  262. if truncate_y[0] > 0 and truncate_y[1] > 0:
  263. img = img[truncate_y[0]:(-truncate_y[1]),:]
  264. elif truncate_y[0] > 0:
  265. img = img[truncate_y[0]:,:]
  266. elif truncate_y[1] > 0:
  267. img = img[:(-truncate_y[1]),:]
  268. # scale
  269. if scale is not None and scale < 1:
  270. img = transform.rescale(img, scale, multichannel=not gray)
  271. return img
  272. def is_daytime(self):
  273. return 6 <= self.date.hour <= 18
  274. def is_nighttime(self):
  275. return not self.is_daytime()
  276. def to_ipython_image(self, width=500, height=None):
  277. return display.Image(filename=self.get_full_path(), width=width, height=height)
  278. class MotionImage(SessionImage):
  279. def __init__(self, session: Session, filename: str, date: datetime):
  280. super().__init__(session, "Motion", filename, date)
  281. if not self.filename in session.motion_dates:
  282. raise ValueError(f"File name {filename} not in session!")
  283. def get_closest_lapse_images(self):
  284. before, after = self.session.get_closest_lapse_images(self.filename)
  285. rel = -1
  286. # rel = 0 if motion image was taken at before lapse image, rel = 1 if motion image was taken at after lapse image
  287. if before is None and after is not None:
  288. rel = 1
  289. elif before is not None and after is None:
  290. rel = 0
  291. elif before is not None and after is not None:
  292. rel = (self.date - before.date).total_seconds() / (after.date - before.date).total_seconds()
  293. else:
  294. warn("No before and no after image!")
  295. return before, after, rel
  296. class LapseImage(SessionImage):
  297. def __init__(self, session: Session, filename: str, date: datetime):
  298. super().__init__(session, "Lapse", filename, date)
  299. if not self.filename in session.lapse_dates:
  300. raise ValueError(f"File name {filename} not in session!")