Session.py 16 KB

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