Guest User

Untitled

a guest
Jan 21st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. #!~/
  2.  
  3. # TODO
  4. # 1.) TRIGGER CAMERA.............................DONE
  5. # 2.) MOVE IMAGES FROM SD CARD TO SPEC. FOLDER...DONE
  6. # 2.) RENAME FILE TO DATE/TIME USING EXIF........DONE
  7. # 3.) INTERVELOMETER ACCOUNTING FOR DAYS, HOURS..DONE
  8. # 4.) GRAPHICAL USER INTERFACE
  9. # 5.) EMAIL ERROR REPORTING
  10. # 7.) GUIDE CAMERA CONTROLS
  11. # 6.) UPLOAD TO CLOUD STORAGE
  12.  
  13.  
  14. # Imports from Python 3.4 Native Library
  15. from __future__ import print_function
  16. import datetime as dt
  17. import time
  18. import logging
  19. import os
  20. import subprocess
  21. import sys
  22. import subprocess, signal
  23. import shutil
  24.  
  25. # Imports from PyPi Libraries
  26. import gphoto2 as gp
  27. import exifread
  28. from apscheduler.schedulers.background import BlockingScheduler
  29.  
  30. # Class Variables
  31. ##Photo save root folder on local device
  32. photo_local_root = '/home/pi/timelapse'
  33. ##Daily start, finish and interval times for timelapse
  34. START_HOUR = '16'
  35. FINISH_HOUR = '17'
  36. INTERVAL = 720 #Seconds
  37.  
  38. ##Calculated. Don't touch these!
  39. SCHEDULER = BlockingScheduler(timezone='US/Pacific')
  40. HOUR_BOUNDS = START_HOUR + '-' + FINISH_HOUR
  41. logging.basicConfig(filename='test.log', level=logging.INFO, \
  42. format='%(asctime)s:%(levelname)s:%(name)s:%(message)s')
  43.  
  44. #
  45. # Kill the 'gvfsd-gphoto2' process
  46. #
  47. def killGphoto():
  48. print("***")
  49. print("Killing Gphoto2...")
  50. p = subprocess.Popen(['killall', 'gvfsd-gphoto2'])
  51. out, err = p.communicate()
  52. print("Killall Command Delivered...")
  53. print("***")
  54.  
  55. #
  56. # Convert the exifread object to a readable date and time
  57. #
  58. def EXIF_DateTimetoStr(exifread):
  59. #Convert the IfDTag to string then
  60. #chop off text before '=' and after '@'
  61. result = repr(exifread).split('=', 1)[-1].split('@',)[0]
  62. #Strip of leading and tailing spaces then
  63. #replace spaces with underscores and colons with dashes
  64. #within the date portion
  65. result = result.strip().replace(' ', '_').replace(':', '-', 2)
  66. return result
  67.  
  68. #
  69. # Convert the exifread object to a readable date
  70. #
  71. def EXIF_DatetoStr(exifread):
  72. #Convert the IfDTag to string then
  73. #chop off text '=' and before, '@' and after
  74. result = repr(exifread).split('=', 1)[-1].split('@',)[0]
  75. #Strip of leading and tailing spaces then
  76. #chop off last space and after
  77. result = result.strip().split(' ',)[0]
  78. #Strip spaces one final time
  79. #Replace colons with dashes
  80. result = result.strip().replace(':', '-')
  81. return result
  82.  
  83. #
  84. # Capture Sequence
  85. #
  86. def captureSave(camera, context):
  87.  
  88. #Capture Action
  89. file_path = gp.check_result(gp.gp_camera_capture(
  90. camera, gp.GP_CAPTURE_IMAGE, context))
  91.  
  92. #Making Target Save photo_local_root Dir
  93. target = os.path.join(photo_local_root, file_path.name)
  94. #GP_FILE_TYPE_NORMAL for JPEG; GP_FILE_TYPE_RAW for RAW
  95. camera_file = gp.check_result(gp.gp_camera_file_get(
  96. camera, file_path.folder, file_path.name,
  97. gp.GP_FILE_TYPE_RAW, context))
  98. gp.check_result(gp.gp_file_save(camera_file, target))
  99. gp.check_result(gp.gp_camera_file_delete(camera, file_path.folder,
  100. file_path.name, context))
  101.  
  102. #Rename Based on EXIF Data
  103. target_open = open(target, 'rb')
  104. tags = exifread.process_file(target_open, \
  105. stop_tag='EXIF DateTimeOriginal')
  106. for tag in tags.keys():
  107. #Only Perform Following if is Date/Time
  108. #Change file extension here for RAW/JPEG
  109. if tag in ('EXIF DateTimeOriginal'):
  110. file_name = EXIF_DateTimetoStr(tags[tag]) + '.NEF'
  111. file_dir = os.path.join(photo_local_root,
  112. EXIF_DatetoStr(tags[tag]))
  113.  
  114. #Check existence of file_dir, the create it if false
  115. if not os.path.exists(file_dir):
  116. os.makedirs(file_dir)
  117.  
  118. #Rename and move the captured image then sleep
  119. shutil.move(target, os.path.join(file_dir, file_name))
  120. time.sleep(3)
  121.  
  122. #
  123. # Main Function
  124. #
  125. def main():
  126. #Kill gphoto2
  127. killGphoto()
  128.  
  129. #GP2 Log and Camera Setup
  130. gp.check_result(gp.use_python_logging())
  131. context = gp.gp_context_new()
  132. camera = gp.check_result(gp.gp_camera_new())
  133. gp.check_result(gp.gp_camera_init(camera, context))
  134.  
  135. #Declaration of Interval Schedulers
  136. SCHEDULER.add_job(captureSave, 'cron', args=[camera,context], \
  137. day_of_week='mon-fri', second='*/'+str(INTERVAL), \
  138. hour=HOUR_BOUNDS)
  139. print('Press Ctrl+{0} to exit'.format( \
  140. 'Break' if os.name == 'nt' else 'C'))
  141.  
  142. try:
  143. SCHEDULER.start()
  144. except (KeyboardInterrupt, SystemExit):
  145. SCHEDULER.remove_job('intervelo')
  146. pass
  147.  
  148. #Close Camera
  149. gp.check_result(gp.gp_camera_exit(camera, context))
  150.  
  151. return 0
  152.  
  153. if __name__ == "__main__":
  154. print("***PROCESS CLOSING***")
  155. sys.exit(main())
Add Comment
Please, Sign In to add comment