Session.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. 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. # A session represents the images taken from a single camera trap at a single position.
  14. # Each session has a subfolder in the dataset directory specifying the session name.
  15. # Each session has Lapse, Motion, and Full images, which can be accessed via this class.
  16. class Session:
  17. def __init__(self, folder: str):
  18. self.folder = folder
  19. # session name = folder name[33:], the first 33 characters are always the same
  20. self.name = os.path.basename(folder)[33:]
  21. print(f"Session '{self.name}' at folder: {self.folder}")
  22. assert self.name != ""
  23. verify_expected_subfolders(self.folder)
  24. self.scanned = False
  25. # maps lapse files to their exif dates (for statistic and prediction purposes)
  26. self.lapse_dates = {}
  27. # maps motion files to their exif dates (for statistic purposes)
  28. self.motion_dates = {}
  29. # maps exif dates to lapse files (for prediction purposes)
  30. self.lapse_map = {}
  31. # maps exif dates to motion files (for csv mapping purposes, generated on demand)
  32. self.motion_map = None
  33. self.load_scans()
  34. if not self.scanned:
  35. print("Session not scanned. Run session.scan() to create scan files")
  36. def load_scans(self):
  37. """ Loads scan results (lapse dates, motion dates, lapse map) from files.
  38. Use save_scans() or scan(auto_save=True) to save scan results.
  39. """
  40. lapse_dates_file = os.path.join("session_scans", self.name, "lapse_dates.pickle")
  41. motion_dates_file = os.path.join("session_scans", self.name, "motion_dates.pickle")
  42. lapse_map_file = os.path.join("session_scans", self.name, "lapse_map.pickle")
  43. lapse_dates_exists = os.path.isfile(lapse_dates_file)
  44. motion_dates_exists = os.path.isfile(motion_dates_file)
  45. lapse_map_exists = os.path.isfile(lapse_map_file)
  46. if lapse_dates_exists and motion_dates_exists and lapse_map_exists:
  47. with open(lapse_dates_file, "rb") as handle:
  48. self.lapse_dates = pickle.load(handle)
  49. with open(motion_dates_file, "rb") as handle:
  50. self.motion_dates = pickle.load(handle)
  51. with open(lapse_map_file, "rb") as handle:
  52. self.lapse_map = pickle.load(handle)
  53. self.scanned = True
  54. print("Loaded scans.")
  55. else:
  56. if not (not lapse_dates_exists and not motion_dates_exists and not lapse_map_exists):
  57. warn(f"Warning: Only partial scan data available. Not loading.")
  58. self.scanned = False
  59. def save_scans(self):
  60. """ Saves scan results (lapse dates, motion dates, lapse map) to files using pickle.
  61. Use load_scans() to load scan results.
  62. The output directory is ./session_scans/{session.name}
  63. """
  64. os.makedirs(os.path.join("session_scans", self.name), exist_ok=True)
  65. lapse_dates_file = os.path.join("session_scans", self.name, "lapse_dates.pickle")
  66. motion_dates_file = os.path.join("session_scans", self.name, "motion_dates.pickle")
  67. lapse_map_file = os.path.join("session_scans", self.name, "lapse_map.pickle")
  68. with open(lapse_dates_file, "wb") as handle:
  69. pickle.dump(self.lapse_dates, handle, protocol=pickle.HIGHEST_PROTOCOL)
  70. print(f"Saved {lapse_dates_file}")
  71. with open(motion_dates_file, "wb") as handle:
  72. pickle.dump(self.motion_dates, handle, protocol=pickle.HIGHEST_PROTOCOL)
  73. print(f"Saved {motion_dates_file}")
  74. with open(lapse_map_file, "wb") as handle:
  75. pickle.dump(self.lapse_map, handle, protocol=pickle.HIGHEST_PROTOCOL)
  76. print(f"Saved {lapse_map_file}")
  77. def get_lapse_folder(self) -> str:
  78. """Returns the path of the Lapse folder."""
  79. return os.path.join(self.folder, "Lapse")
  80. def get_motion_folder(self) -> str:
  81. """Returns the path of the Motion folder."""
  82. return os.path.join(self.folder, "Motion")
  83. def get_full_folder(self) -> str:
  84. """Returns the path of the Full folder."""
  85. return os.path.join(self.folder, "Full")
  86. def scan(self, force=False, auto_save=True):
  87. """Scans Motion and Lapse images for their EXIF dates. This populates the fields
  88. motion_dates, lapse_dates and motion_map.
  89. Args:
  90. force (bool, optional): Scan even if this session was already scanned. Defaults to False.
  91. auto_save (bool, optional): Save scan results after scan. Defaults to True.
  92. Raises:
  93. ValueError: Session was already scanned and force=False.
  94. """
  95. if self.scanned and not force:
  96. raise ValueError("Session is already scanned. Use force=True to scan anyway and override scan progress.")
  97. # Scan motion dates
  98. print("Scanning motion dates...")
  99. self.motion_dates = {}
  100. motion_folder = self.get_motion_folder()
  101. for file in tqdm(list_jpegs_recursive(motion_folder)):
  102. self.motion_dates[os.path.relpath(file, motion_folder)] = get_image_date(file)
  103. # Scan lapse dates
  104. print("Scanning lapse dates...")
  105. self.lapse_dates = {}
  106. lapse_folder = self.get_lapse_folder()
  107. for file in tqdm(list_jpegs_recursive(lapse_folder)):
  108. self.lapse_dates[os.path.relpath(file, lapse_folder)] = get_image_date(file)
  109. # Create lapse map
  110. print("Creating lapse map...")
  111. self.lapse_map = {}
  112. for file, date in self.lapse_dates.items():
  113. if date in self.lapse_map:
  114. self.lapse_map[date].append(file)
  115. else:
  116. self.lapse_map[date] = [file]
  117. self.scanned = True
  118. # Auto save
  119. if auto_save:
  120. print("Saving...")
  121. self.save_scans()
  122. def check_lapse_duplicates(self):
  123. """Checks the Lapse images for duplicates and prints the results.
  124. A duplicate means there are two or more Lapse images with the same EXIF date.
  125. A multiple means there are three or more such images (includes duplicates).
  126. Deviant duplicate means there are two or more images which have the same EXIF date but are not identical (have different file sizes).
  127. Returns:
  128. total (int), total_duplicates (int), total_multiples (int), deviant_duplicates (int)
  129. """
  130. total = 0
  131. total_duplicates = 0
  132. total_multiples = 0
  133. deviant_duplicates = []
  134. for date, files in tqdm(self.lapse_map.items()):
  135. total += 1
  136. if len(files) > 1:
  137. total_duplicates += 1
  138. file_size = -1
  139. for f in files:
  140. f_size = os.path.getsize(os.path.join(self.folder, "Lapse", f))
  141. if file_size == -1:
  142. file_size = f_size
  143. elif f_size != file_size:
  144. deviant_duplicates.append(date)
  145. break
  146. if len(files) > 2:
  147. total_multiples += 1
  148. deviant_duplicates.sort()
  149. print(f"* {total} lapse dates")
  150. print(f"* {total_duplicates} duplicates")
  151. print(f"* {total_multiples} multiples (more than two files per date)")
  152. print(f"* {len(deviant_duplicates)} deviant duplicates: {deviant_duplicates}")
  153. return total, total_duplicates, total_multiples, deviant_duplicates
  154. def open_images_for_date(self, date: datetime):
  155. """Open all lapse images with the specified EXIF date using the system image viewer.
  156. Args:
  157. date (datetime): Lapse date.
  158. """
  159. img_names = self.lapse_map.get(date, [])
  160. if len(img_names) == 0:
  161. warn("No images for this date!")
  162. for i, img_name in enumerate(img_names):
  163. full_path = os.path.join(self.folder, "Lapse", img_name)
  164. print(f"#{i+1} {full_path}")
  165. subprocess.call(("xdg-open", full_path))
  166. def get_motion_image_from_filename(self, filename: str) -> "MotionImage":
  167. """Returns a MotionImage instance from the filename of a motion image.
  168. Args:
  169. filename (str): File name of motion image.
  170. Raises:
  171. ValueError: Unknown motion file name.
  172. Returns:
  173. MotionImage: MotionImage instance.
  174. """
  175. if filename in self.motion_dates:
  176. return MotionImage(self, filename, self.motion_dates[filename])
  177. else:
  178. raise ValueError(f"Unknown motion file name: {filename}")
  179. def __generate_motion_map(self):
  180. """Populates self.motion_map which maps dates to motion images"""
  181. if self.motion_map is not None:
  182. return
  183. print("Generating motion map...")
  184. self.motion_map = {}
  185. for filename, date in self.motion_dates.items():
  186. if date in self.motion_map:
  187. self.motion_map[date].append(filename)
  188. else:
  189. self.motion_map[date] = [filename]
  190. def get_motion_images_from_date(self, date: datetime):
  191. """Returns MotionImage instances for all motion images with the specified EXIF date.
  192. Args:
  193. date (datetime): Motion date.
  194. """
  195. self.__generate_motion_map()
  196. filenames = self.motion_map.get(date, [])
  197. return [MotionImage(self, filename, date) for filename in filenames]
  198. def get_random_motion_image(self, day_only=False, night_only=False) -> "MotionImage":
  199. """Returns a MotionImage instance of a random Motion image.
  200. Args:
  201. day_only (bool, optional): Only return daytime images. Defaults to False.
  202. night_only (bool, optional): Only return nighttime images. Defaults to False.
  203. Raises:
  204. ValueError: No motion images in this session.
  205. Returns:
  206. MotionImage: Random MotionImage or None if not found
  207. """
  208. if len(self.motion_dates) == 0:
  209. raise ValueError("No motion images in session!")
  210. img = None
  211. while img is None or (day_only and img.is_nighttime()) or (night_only and img.is_daytime()):
  212. filename = random.choice(list(self.motion_dates.keys()))
  213. img = MotionImage(self, filename, self.motion_dates[filename])
  214. return img
  215. def get_random_motion_image_set(self, day_only=False, night_only=False) -> list:
  216. """Returns a list of all motion images with the same date +- 10 min.
  217. The date is picked randomly from all available dates.
  218. May loop indefinitely if there are no matching motion images.
  219. Args:
  220. day_only (bool, optional): Only pick daytime images. Defaults to False.
  221. night_only (bool, optional): Only pick nighttime images. Defaults to False.
  222. Raises:
  223. ValueError: No motion images in session
  224. Returns:
  225. list: Non-empty list of motion images with the same date
  226. """
  227. self.__generate_motion_map()
  228. if len(self.motion_map) == 0:
  229. raise ValueError("No motion images in session!")
  230. imgs = []
  231. date = None
  232. while len(imgs) == 0 or (day_only and imgs[0].is_nighttime()) or (night_only and imgs[0].is_daytime()):
  233. date = random.choice(list(self.motion_map.keys()))
  234. filenames = self.motion_map.get(date, [])
  235. imgs = [MotionImage(self, filename, date) for filename in filenames]
  236. # include all images within +- 5 min
  237. for other_date in self.motion_map.keys():
  238. if date != other_date and abs((date - other_date).total_seconds()) <= 60 * 5:
  239. filenames = self.motion_map.get(other_date, [])
  240. imgs += [MotionImage(self, filename, other_date) for filename in filenames]
  241. return imgs
  242. def generate_motion_image_sets(self) -> list:
  243. """Generator function which yields consecutively taken motion image sets.
  244. Raises:
  245. ValueError: No motion images in this session.
  246. Returns:
  247. list: _description_
  248. Yields:
  249. Iterator[list of MotionImage]: consecutive motion image set
  250. """
  251. self.__generate_motion_map()
  252. if len(self.motion_map) == 0:
  253. raise ValueError("No motion images in session!")
  254. imgs = []
  255. dates = sorted(list(self.motion_map.keys()))
  256. start_date = dates[0]
  257. for date in dates:
  258. if abs((date - start_date).total_seconds()) > 60 * 5:
  259. # end image time series
  260. yield imgs
  261. start_date = date
  262. imgs = []
  263. # continue time series
  264. filenames = self.motion_map.get(date, [])
  265. imgs += [MotionImage(self, filename, date) for filename in filenames]
  266. # end of all time series
  267. yield imgs
  268. def generate_motion_images(self):
  269. """Yields all motion images in this session.
  270. Yields:
  271. MotionImage: A MotionImage
  272. """
  273. for file, date in self.motion_dates.items():
  274. yield MotionImage(self, file, date)
  275. def generate_lapse_images(self):
  276. """Yields all lapse images in this session.
  277. Yields:
  278. LapseImage: A LapseImage
  279. """
  280. for file, date in self.lapse_dates.items():
  281. yield LapseImage(self, file, date)
  282. def get_closest_lapse_images(self, motion_file: str):
  283. """Returns the lapse images taken closest before and after this image, respectively.
  284. If no such image is found, the corresponding returned image will be None.
  285. Args:
  286. motion_file (str): Filename of the motion image
  287. Returns:
  288. (MotionImage or None, MotionImage or None): Closest lapse images. Each image can be None if not found.
  289. """
  290. date: datetime = self.motion_dates[motion_file]
  291. previous_date = date.replace(minute=0, second=0)
  292. next_date = previous_date + timedelta(hours=1)
  293. i = 0
  294. while not previous_date in self.lapse_map:
  295. previous_date -= timedelta(hours=1)
  296. i += 1
  297. if i > 24:
  298. # no previous lapse image exists
  299. previous_date = None
  300. break
  301. i = 0
  302. while not next_date in self.lapse_map:
  303. next_date += timedelta(hours=1)
  304. i += 1
  305. if i > 24:
  306. # no next lapse image exists
  307. next_date = None
  308. break
  309. if previous_date is not None and len(self.lapse_map[previous_date]) > 1:
  310. warn(f"There are multiple lapse images for date {previous_date}! Choosing the first one.")
  311. if next_date is not None and len(self.lapse_map[next_date]) > 1:
  312. warn(f"There are multiple lapse images for date {next_date}! Choosing the first one.")
  313. previous_img = None if previous_date is None else LapseImage(self, self.lapse_map[previous_date][0], previous_date)
  314. next_img = None if next_date is None else LapseImage(self, self.lapse_map[next_date][0], next_date)
  315. return previous_img, next_img
  316. # Abstract class which represents an image in a session (either Motion or Lapse).
  317. class SessionImage:
  318. def __init__(self, session: Session, subfolder: str, filename: str, date: datetime):
  319. self.session = session
  320. self.subfolder = subfolder
  321. self.filename = filename
  322. self.date = date
  323. if not os.path.isfile(self.get_full_path()):
  324. raise ValueError(f"File {subfolder}/{filename} in session folder {session.folder} not found!")
  325. def get_full_path(self) -> str:
  326. """Returns the full path of this image. """
  327. return os.path.join(self.session.folder, self.subfolder, self.filename)
  328. def open(self):
  329. """Open this image using the system image viewer. """
  330. full_path = self.get_full_path()
  331. print(f"Opening {full_path}...")
  332. subprocess.call(("xdg-open", full_path))
  333. def read(self, truncate_y = (40, 40), scale=1, gray=True):
  334. """Read this image into a numpy array.
  335. Args:
  336. truncate_y (tuple, optional): Crop of the image at the top and bottom, respectively. Defaults to (40, 40).
  337. scale (int, optional): Scale factor for rescaling. Defaults to 1.
  338. gray (bool, optional): If True, read the image as grayscale. Defaults to True.
  339. Returns:
  340. np.array: image
  341. """
  342. full_path = self.get_full_path()
  343. img = io.imread(full_path, as_gray=gray)
  344. # truncate
  345. if truncate_y is not None:
  346. if truncate_y[0] > 0 and truncate_y[1] > 0:
  347. img = img[truncate_y[0]:(-truncate_y[1]),:]
  348. elif truncate_y[0] > 0:
  349. img = img[truncate_y[0]:,:]
  350. elif truncate_y[1] > 0:
  351. img = img[:(-truncate_y[1]),:]
  352. # scale
  353. if scale is not None and scale < 1:
  354. img = transform.rescale(img, scale, multichannel=not gray)
  355. return img
  356. def read_opencv(self, truncate_y = (40, 40), scale=1, gray=True):
  357. """Read this image into an OpenCV Mat.
  358. Args:
  359. truncate_y (tuple, optional): Crop of the image at the top and bottom, respectively. Defaults to (40, 40).
  360. scale (int, optional): Scale factor for rescaling. Defaults to 1.
  361. gray (bool, optional): If True, read the image as grayscale. Defaults to True.
  362. Returns:
  363. OpenCV Mat: image
  364. """
  365. full_path = self.get_full_path()
  366. img = cv.imread(full_path)
  367. # grayscale
  368. if gray:
  369. img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
  370. # truncate
  371. if truncate_y is not None:
  372. if truncate_y[0] > 0 and truncate_y[1] > 0:
  373. img = img[truncate_y[0]:(-truncate_y[1])]
  374. elif truncate_y[0] > 0:
  375. img = img[truncate_y[0]:]
  376. elif truncate_y[1] > 0:
  377. img = img[:(-truncate_y[1])]
  378. # scale
  379. if scale is not None and scale < 1:
  380. img = cv.resize(img, None, fx=scale, fy=scale, interpolation=cv.INTER_LINEAR)
  381. return img
  382. def is_daytime(self):
  383. """Returns True if this image was taken at daytime based on the EXIF date. """
  384. return 6 <= self.date.hour <= 18
  385. def is_nighttime(self):
  386. """Returns True if this image was taken at nighttime based on the EXIF date. """
  387. return not self.is_daytime()
  388. def to_ipython_image(self, width=500, height=None):
  389. """Return an IPython image displaying this image. """
  390. return display.Image(filename=self.get_full_path(), width=width, height=height)
  391. # Represents a single Motion image. Should only be instantiated by Session.
  392. class MotionImage(SessionImage):
  393. def __init__(self, session: Session, filename: str, date: datetime):
  394. super().__init__(session, "Motion", filename, date)
  395. if not self.filename in session.motion_dates:
  396. raise ValueError(f"File name {filename} not in session!")
  397. def get_closest_lapse_images(self):
  398. """ Returns the closest lapse images before and after and the rel-value.
  399. rel is a value between 0 and 1. The close rel is to 0 (1), the closer the motion image is too
  400. the before (after) lapse image. If no lapse images were found, rel is -1.
  401. Returns:
  402. before (LapseImage or None), after (LapseImage or None), rel (float)
  403. """
  404. before, after = self.session.get_closest_lapse_images(self.filename)
  405. rel = -1
  406. # rel = 0 if motion image was taken at before lapse image, rel = 1 if motion image was taken at after lapse image
  407. if before is None and after is not None:
  408. rel = 1
  409. elif before is not None and after is None:
  410. rel = 0
  411. elif before is not None and after is not None:
  412. rel = (self.date - before.date).total_seconds() / (after.date - before.date).total_seconds()
  413. else:
  414. warn("No before and no after image!")
  415. return before, after, rel
  416. # Represents a single Lapse image. Should only be instantiated by Session.
  417. class LapseImage(SessionImage):
  418. def __init__(self, session: Session, filename: str, date: datetime):
  419. super().__init__(session, "Lapse", filename, date)
  420. if not self.filename in session.lapse_dates:
  421. raise ValueError(f"File name {filename} not in session!")