-
Notifications
You must be signed in to change notification settings - Fork 0
/
timelaps.py
368 lines (307 loc) · 11.1 KB
/
timelaps.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import os
import time
import platform
import math
import atexit
MyPlatform = platform.system() # current operating system
timestampPath = "timestamp.txt"
numberPath = "number.txt"
settingsfolder = "./settings/" # linux
def execute(what): # commandline execute
os.system(what)
class camera(object):
def __init__(self):
global MyPlatform
if MyPlatform == "Linux":
folder = os.listdir("/dev")
if "video1" or "video0" in folder:
print("camera connected")
else:
print("please connect a camera, and run the script again")
exit()
else:
import pygame.camera
pygame.camera.init()
clist = pygame.camera.list_cameras()
if len(clist)>1:
print "there is more than one camera connected"
if not clist:
print "you need to connect a v4l2 supported camera"
exit()
#find the resoulution that works with the camera
loopcount = -1
sizes = [(1080,720), (2304,1536), (640,480), (320,240),(800,600)]
going = True
while going:
going = False
try:
self.size = sizes[loopcount]
self.cam = pygame.camera.Camera(clist[0], self.size) # define camera
except:
loopcount += 1
going = True
print "set resolution to: " + str(self.size)
def capture(self, imagestring, loopcount, writetime):
import pygame.camera
global MyPlatform #platform name
print("capturing %s"% imagestring)
timestart = time.time()
if MyPlatform == "Linux":
execute("sudo fswebcam -q --jpeg 95 -d /dev/video0 -i 0 -r 1080x720 --no-banner --no-timestamp %s"% imagestring) # -p YUYV, capture image
elif MyPlatform == "Windows":
import pygame.camera
surface = pygame.Surface(self.size)
if config["CameraBiContinious"] == "No" or "no":
self.cam.start()
image = self.cam.get_image(surface)
self.cam.stop()
else:
image = self.cam.get_image(surface)
pygame.image.save(image, imagestring)
timeused = time.time()-timestart
if writetime == "no":
self.logtimestamp(loopcount)
else:
self.writetimestamp(imagestring, loopcount)
time.sleep(0.25)
return timeused
def writetimestamp(self,image, a):
global timestampPath
date = time.strftime("%a %d.%m.%Y %H:%M:%S") # get current time, in this form day daynumber.month.year time:minute:second
failed = False
try:
import Image
import ImageDraw
import ImageFont
import ImageStat
except:
self.logtimestamp(a)
failed = True
if failed == False:
ont = ImageFont.truetype("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", 20) # get font
print("writing timestamp on image: " + date)
im = Image.open( image ) # problems with reading the imagestring, it is formated wrongly
canvas = ImageDraw.Draw( im )
canvas.rectangle( [(0,0), (280,40)] , fill=(0,0,0) )
canvas.text( (10,10), date, (255,255,255), font=font)
im.save(image)
def logtimestamp(self, a):
global timestampPath
date = time.strftime("%a %d.%m.%Y %H:%M:%S") # get current time, in this form day daynumber.month.year time:minute:second
x = open(timestampPath, "a")
x.write(date + "=-=" + str(a)+"\n")
x.close()
class SettingFunc(object):
def getsetting(self, filename):
x = open("settings/%s"% filename)
line = x.readline()
while line != '':
if list(line)[0] != '\n': # if not empty enter
#print list(line)
self.setting(line) # use the setting
else:
print("invalid setting: " + line)
line = x.readline()
def checksetting(self, settingsfolder, setting):
x = open(settingsfolder + setting)
lines = x.readlines()
print()
print("setting: " + setting)
for i in lines:
print(i[0: len(i)-2])
x.close()
def setting(self, sett):
#remove \n
lastchar = sett[len(sett)-1 : len(sett)]
if lastchar == '\n':
sett = sett[0:len(sett)-1]
print("setting setting:%s "% sett)
if sett != " " or "\n" or '\n' or '':
os.system("v4l2-ctl --set-ctrl %s"% sett) # sett the setting
class Nxt(object): # out of function
def __init__(self):
import nxt.locator
import nxt.motor
sock = nxt.locator.find_one_brick()
self.brick = sock.connect()
self.Motor = nxt.motor.Motor(self.brick, PORT_ALL)
self.loopcount = 0
def main(self,config): # called every minute, uses TurnInverval for when to execute
self.loopcount += 1
if loopcount == 1:
self.left = config["TurnInterval"]-1 # minus one because it counts current round
else:
self.left -= 1
if self.left == 0:
self.left = config["TurnInterval"]
self.Motor.update(config["TurnPower"], config["TurnAngle"], True)
class server(object):
def server(self):
self.socket.listen(5)
self.client, addr = s.accept()
def ServerUpdate(self, message):
c.send(message)
def main(config):
import socket
self.socket = socket.socket()
if config["SocketCapSyncType"] == "server":
host = config["InternetProtocolAdress"]
self.socket.bind((host,123))
server(self)
else:
host = socket.gethostname()
self.socket.bind((host,123))
client(self)
class usersettings(object):
def SocketCapSync(self):
global MyPlatform
if self.config["SocketCapSync"] == "yes":
self.config["SocketCapSyncType"] = input("server or client?:")
self.config["InternetProtocolAdress"] = input("ip? (if server then my ip, if client then their ip):")
else:
self.config["SocketCapSyncType"] = None
self.config["InternetProtocolAdress"] = None
def SinglePic(self):
if self.config["SinglePic"] == "no":
self.config["AutoChange"] = input("auto change camera settings (1=yes, 0=no): ")
self.config["AutoChange"] = self.NumbToBool(self.config["AutoChange"])
if self.config["AutoChange"] == True:
self.config["CurrentSettingName"] = input("current setting name: ")
else:
self.config["AutoChange"] = False
if MyPlatform == "Linux":
self.config["ExAlgo"] = input("Use homemade exposure algorithm? (yes=1, no=0: ")
self.config["ExAlgo"] = self.NumbToBool(self.config["ExAlgo"])
else:
self.config["SinglePicCaptureFolder"] = raw_input("folder to capture to (1,2,3):")
self.config["AutoChange"] = None
self.config["CurrentSettingName"] = None
self.config["ExAlgo"] = None
def Nxt(self):
if self.config["AutoRotate"] == "yes":
self.config["TurnInterval"] = int(input("Turn every x minute: "))
self.config["TurnPower"] = int(input("With how much power (1-100): "))
self.config["TurnAngle"] = int(input("How many high angle of turn every update: "))
else:
self.config["TurnInterval"] = None
self.config["TurnPower"] = None
self.config["TurnAngle"] = None
def Camera(self):
if self.config["CameraContinious"] == "no":
self.config["CameraBiContinious"] = raw_input("as close to continious as possible? (yes or no): ")
else:
self.config["CameraBiContinious"] = None
def main(self):
global currentsetting, MyPlatform
# Autochange, SinglePic, CurrentSettingName, SinglePicCaptureFolder, AutoRotate, TurnInterval, TurnPower, WriteTimestamp
# Camerastart, CameraContinious, CameraBiContinious
self.config = {"AutoChange" : False} # config dictionary
self.config["SocketCapSync"] = raw_input("Network Camera Syncing?: (yes or no):")
self.SocketCapSync()
self.config["SinglePic"] = raw_input("Single image?, (yes or no):")
self.SinglePic()
self.config["AutoRotate"] = raw_input("AutoRotate using nxt?: (yes or no):")
self.Nxt()
self.config["WriteTimestamp"] = raw_input("Write timestamp on image? (yes or no): ")
if MyPlatform == "Windows":
self.config["CameraContinious"] = raw_input("Do you want the camera to run continiously? (yes or no): ")
self.Camera()
else:
self.config["CameraStart"] = None
return self.config
def move(imagestring, numb):
print("moving to webserver")
execute("sudo cp %s /var/www/webcam/now/webcam%s.jpg"% (imagestring,str(numb)))
def movecurrent(current): #autochange
execute("sudo mv settings settings_%s"% current.upper() )
def WriteLoopcunt(loopcount): # write loopcount to text file
global numberPath
x = open(numberPath, "w")
x.write(str(loopcount))
x.close()
def exitfunction():
camera.cam.stop()
print "stopping camera"
print "will continue where you stopped when starting again"
#get picture number
x = open("number.txt")
loopcount = int(x.readline())
x.close()
#class init
SettingClass = SettingFunc()
camera = camera()
clas = usersettings()
config = clas.main()
if config["CameraContinious"] == "Yes" or "yes":
camera.cam.start()
atexit.register(exitfunction)
if MyPlatform == "Linux": #folder name fixes
prefix = "./"
folder = "selbulantimelapsv2"
if not os.path.exists(prefix+folder):
os.system("mkdir %s"% prefix+folder)
SettingsFiles = os.listdir(settingsfolder)
print("SettingFiles: " + str(SettingsFiles))
for sett in SettingsFiles:
SettingClass.checksetting(settingsfolder, sett) #just printing them, so that the user can spot mistakes
#wait for input
raw_input("continue?, (if not press Ctrl + C)")
else:
a = "\s"
prefix = os.getcwd()[2:len( os.getcwd() )]
folder = "\selbulantimelapsv2"
os.system("mkdir %s"% prefix+folder)
#setting("focus_auto=1")
while True:
timestart = time.time()
loopcount += 1
WriteLoopcunt(loopcount) # write to txt
if config["CameraBiContinious"] == "Yes" or "yes":
camera.cam.start()
if MyPlatform == "Linux":
if config["SinglePic"] == "no": # we are not doing singlepictures
SettingClass.setting("exposure_auto=3")
SettingClass.setting("exposure_auto_priority=1")
for SettFile in SettingFiles:
imagestring = prefix+folder+"/%s/%s.tif"% (str(SettFile), loopcount)
print("imagestring: %s"% imagestring)
SettingClass.getsetting(str(SettFile)) #get and set settings
CaptureTime = camera.capture(imagestring, loopcount) # capture image
move(imagestring, SettFile) # copy to webserver
print()
else: # singlepictures for linux
imagestring = prefix+folder+"/%s/%s.tif"% (config["SinglePicCaptureFolder"], loopcount)
if config["ExAlgo"] == True:
SettingClass.setting("exposure_auto=0")
focuslen = 5 # meter
fnumber = focuslen/0.008
exposuredur = 10 #second
ev = math.log( (math.pow(fnumber,2)/exposuretime), 2)
SettingClass.setting("exposure_absolute=" + str(ev))
else:
SettingClass.setting("exposure_auto=1")
SettingClass.setting("exposure_auto_priority=1")
CaptureTime = camera.capture(imagestring, loopcount) # capture image
move(imagestring, config["SinglePicCaptureFolder"]) # copy to webserver
print()
#auto change settings
if config["AutoChange"] == True:
targets = { (18,0) : "natt", (6,0) : "dag" }
time = ( int(time.strftime("%H")),int(time.strftime("%M")))
if time in targets:
targetsetting = targets[time]
movecurrent( currentsetting )
for type in targets.keys():
if targetsetting == type:
execute("sudo mv settings_%s settings"% type.upper() )
else: # windows just capture
imagestring = prefix+folder+"\%s\%s.tif"% (config["SinglePicCaptureFolder"], loopcount)
camera.capture(imagestring, loopcount, config["WriteTimestamp"]) # capture image
if config["CameraBiContinious"] == "Yes" or "yes":
camera.cam.stop()
#wait
timeused = time.time()-timestart
print("time used: %s time left to new round: %s secound of film: %s"% (str(timeused), str(60-timeused), float(loopcount/24.0)) )
print
print
time.sleep(60-timeused)