forked from dwiel/gphoto2-timelapse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timelapse.py
172 lines (136 loc) · 4.52 KB
/
timelapse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
NOTE: many settings in here are specific to the NIKON3000 and will need to be
tweaked for other cameras
TODO:
* slow down interval at night
* do an image diff between the past few images to see how fast things are
changing. If they are changing slowly, slow down the interval
"""
from datetime import datetime, timedelta
import os
import time
import subprocess
import sun
# the time between photos
#DELTA = timedelta(seconds = 20)
DELTA = timedelta(minutes = 5)
picture_folder = '/home/pi/gphoto2-timelapse/photos'
DEBUG = False
def log(message) :
print datetime.utcnow(), message
def run(cmd) :
reset_nikon()
# try running the command once and if it fails, reset_nikon
# and then try once more
for i in range(2) :
log("running %s" % cmd)
p = subprocess.Popen(
'sudo ' + cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
(stdout, stderr) = p.communicate()
ret = p.returncode
if stdout.strip() != '' or DEBUG :
print 'stdout', stdout
print 'end stdout'
if stderr.strip() != '' or DEBUG :
print 'stderr', stderr
print 'end stderr'
if DEBUG :
print 'ret', ret
if ret == 0 :
return ret, stdout, stderr
elif ret == 1 :
if 'No camera found' in stderr :
print '#### no camera found'
print 'TODO: reboot?'
print 'TODO: reset power on camera via GP IO?'
return ret, stdout, stderr
else :
# other error like tried to delete a file where there was none, etc
return ret, stdout, stderr
else :
reset_nikon()
return ret, stdout, stderr
def reset_nikon() :
log('reset usb')
import os
ret = os.popen('lsusb').read()
for line in ret.split('\n') :
if 'Nikon' not in line : continue
ret = os.popen("./usbreset /dev/bus/usb/%s/%s" % (line[4:7], line[15:18])).read()
if 'successful' not in ret :
print 'ret', ret
import re
def list_files() :
folder = ''
files = []
ret, stdout, _ = run('gphoto2 --list-files')
for line in stdout.split('\n') :
if not line : continue
if line[0] == '#' :
files.append((folder, line.split()[0][1:], line.split()[1]))
else :
m = re.match(".*'(.*)'", line)
if m :
folder = m.group(1)
if folder[-1] != '/' :
folder += '/'
else :
log('warning, unkown output of --list-files: ' + line)
return files
workaround = True
def take_picture(filename = None) :
if not filename :
filename = datetime.utcnow().strftime("%Y%m%d-%H%M%S.jpg")
log('taking picture')
if workaround :
# this works around --capture-image-and-download not working
# get rid of any existing files on the card
for folder, number, _ in list_files() :
delete_picture(from_folder = folder)
# take the picture
run("gphoto2 --capture-image")
# copy the picture from the camera to local disk
run("gphoto2 --get-file=1 --filename=%s/%s" % (picture_folder, filename))
## delete file off of camera
#delete_picture()
else :
return run("gphoto2 --capture-image-and-download --filename %s/%s" % (picture_folder, filename))
def delete_picture(from_folder = None) :
log('deleting picture')
if from_folder :
print 'from_folder', from_folder
ret, stdout, stderr = run("gphoto2 --delete-file=1 --folder=%s" % from_folder)
if 'There are no files in folder' in stdout :
return ret
# try deleting from all 3 known folders, in the order of most likely
ret, stdout, stderr = run("gphoto2 --delete-file=1 --folder=/store_00010001")
if 'There are no files in folder' in stderr :
ret, stdout, stderr = run("gphoto2 --delete-file=1 --folder=/store_00010001/DCIM/100NIKON")
if 'There are no files in folder' in stderr :
ret, stdout, stderr = run("gphoto2 --delete-file=1 --folder=/")
return ret
def reset_settings() :
log('reseting settings')
run("gphoto2 --set-config /main/capturesettings/flashmode=1")
run("gphoto2 --set-config /main/capturesettings/focusmode=0")
reset_settings()
while True :
t = datetime.utcnow()
# only take pictures when it is light out
if sun.is_light(t) :
reset_nikon()
take_picture()
else :
print "Waiting for the sun to come out"
# remove the picture from camera memory since there isn't much there
reset_nikon()
delete_picture()
# wait for 1 minute
# we can't just do sleep(5 * 60) because taking the picture takes time
print datetime.utcnow(), 'waiting ...'
while datetime.utcnow() < t + DELTA :
time.sleep(1)