-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgonet4.py
More file actions
executable file
·711 lines (516 loc) · 22.8 KB
/
gonet4.py
File metadata and controls
executable file
·711 lines (516 loc) · 22.8 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
#!/usr/bin/python3
import serial
import subprocess
import socket
import os
import shutil
import time
from time import gmtime, strftime
from PIL import Image, ImageDraw, ImageFont, ExifTags
import glob
import re
from collections import deque
import syslog
from datetime import datetime
from picamera import PiCamera
from time import sleep
from fractions import Fraction
# shutter speed (exposure time) in microseconds
raspistill_ss = 6000000
tag_raspistill_ss = str(round(raspistill_ss/1000000, 2))
#How many images do want?
#number_of_images = 3
# Sensitivity (ISO)
ISO = 800
#Dynamic Range Compression (DRC) options :off,low,med,high
drc = 'off'
#White Balance: off,auto,sun,cloud,shade,tungsten,fluorescent,incandescent,flash,horizon
awb = 'off'
# Mannually set white balance gains
white_balance_gains = '1.03125, 1.8086'
#Brightness
br = 50
#interval between images in milliseconds
raspistill_tl = 0
# total time of the run in milliseconds (controls how many photos you take)
# Exposure time converted to milliseconds + interval between images X number of images + an extra interval
#raspistill_t = (((raspistill_ss/1000) + raspistill_tl) * (number_of_images +1))
#raspistill_t = (((raspistill_ss/1000) + raspistill_tl) * (number_of_images))
raspistill_t = 65000
run_start_time = time.time()
print ("run_start_time = " + str(run_start_time))
logfile = open("/home/pi/Tools/Camera/gonet.log","a+")
logfile.write("run_start_time = " + str(run_start_time) + "\n")
now = datetime.now()
logfile.write("run_start_time = " + now.strftime("%m/%d/%Y, %H:%M:%S") + "\n")
print ("run_start_time = " + now.strftime("%m/%d/%Y, %H:%M:%S"))
scratch_dir = "/home/pi/Tools/Camera/scratch/"
if not os.path.exists(scratch_dir):
os.makedirs(scratch_dir)
image_dir = "/home/pi/images/"
if not os.path.exists(image_dir):
os.makedirs(image_dir)
thumbs_dir = "/home/pi/_sfpg_data/thumb/"
if not os.path.exists(thumbs_dir):
os.makedirs(thumbs_dir)
# remove any zero length file from scratch dir
for filename in os.listdir(scratch_dir):
if os.path.getsize(scratch_dir + filename) == 0:
print("Deleting zero length file " + scratch_dir + filename)
logfile.write(("Deleting zero length file " + scratch_dir + filename) + "\n")
os.remove(scratch_dir + filename)
# move any reaming jpg files to image dir
for filename in os.listdir(scratch_dir):
if filename.endswith(".jpg"):
print("Moving " + scratch_dir + filename + " to " + image_dir + filename)
logfile.write(("Moving " + scratch_dir + filename + " to " + image_dir + filename) + "\n")
os.rename(scratch_dir + filename, image_dir + filename)
# Here date and time is captured from GPS $GPRMC if available.
# The time service from the network is shutdown, and system time is set from GPS value.
# Then the network time service is restarted in case the device is being tested indoors
# and network is accessible and GPS is not.
#
# This is in case the program is executed without access to netork.
# It is ran everytime the program is executed in case GPS is not valid when the GONet
# device is first turned on.
#
port = "/dev/serial0"
ser = serial.Serial(port, baudrate = 9600, timeout = 0.5)
print ("The sysdate at startup is: ")
subprocess.call('date')
i = 0
while i < 5:
time.sleep(1.0)
data = ser.read_until().decode('utf_8')
sdata = data.split(",")
if sdata[0] == '$GPRMC' and sdata[2] == 'V':
print (sdata)
elif sdata[0] == '$GPRMC' and sdata[2] != 'V':
sdate = sdata[9]
stime = sdata[1]
if sdate[2:4] == '01':
smonth = "JAN"
elif sdate[2:4] == '02':
smonth = "FEB"
elif sdate[2:4] == '03':
smonth = "MAR"
elif sdate[2:4] == '04':
smonth = "APR"
elif sdate[2:4] == '05':
smonth = "MAY"
elif sdate[2:4] == '06':
smonth = "JUN"
elif sdate[2:4] == '07':
smonth = "JUL"
elif sdate[2:4] == '08':
smonth = "AUG"
elif sdate[2:4] == '09':
smonth = "SEP"
elif sdate[2:4] == '10':
smonth = "OCT"
elif sdate[2:4] == '11':
smonth = "NOV"
elif sdate[2:4] == '12':
smonth = "DEC"
print (sdate[0:2] + " " + smonth + " " +"20"+ sdate[4:7] + " " + stime[0:2] + ":" + stime[2:4] + ":" + stime[4:6])
date_time = sdate[0:2] + " " + smonth + " " +"20"+ sdate[4:7] + " " + stime[0:2] + ":" + stime[2:4] + ":" + stime[4:6]
print ("Shutting down network time service")
command = ['sudo', 'systemctl', 'stop', 'systemd-timesyncd.service']
subprocess.call(command)
print("The sysdate before setting with GPS is:")
subprocess.call('date')
# Uncomment below for testing while on network.
# This "holds" the GPS time for 5 seconds.
# If on a network the time will be set by the network, the GPS will set the time 5 seconds, but 5 seconds slow, then then when the network is
# restarted, the time will be proplerly set.
#time.sleep(5.0)
print ("Setting time via GPS.")
syslog.syslog("Setting time via GPS")
command = ['sudo', 'date', '-s', date_time]
subprocess.call(command)
print("The sysdate after setting with GPS is:")
subprocess.call('date')
break
i += 1
print ("restarting netwotk time servivce")
command = ['sudo', 'systemctl', 'start', 'systemd-timesyncd.service']
subprocess.call(command)
subprocess.call('date')
##### End of setting sysdate #####
run_start_time = time.time()
print ("run_start_time = " + str(run_start_time))
#logfile = open("/home/pi/Tools/Camera/gonet.log","a+")
#logfile.write("run_start_time = " + str(run_start_time) + "\n")
#now = datetime.now()
#logfile.write("run_start_time = " + now.strftime("%m/%d/%Y, %H:%M:%S") + "\n")
#print ("run_start_time = " + now.strftime("%m/%d/%Y, %H:%M:%S"))
#logfile.write("images = " + str(number_of_images) + " interval = " + str(raspistill_tl) + " shutter = " + str(raspistill_ss) + "\n")
logfile.write("interval = " + str(raspistill_tl) + " shutter = " + str(raspistill_ss) + "\n")
###############################
##### Start of functions #####
###############################
def disk_stat(path):
disk = os.statvfs(path)
#percent = (disk.f_blocks - disk.f_bfree) * 100 / (disk.f_blocks -disk.f_bfree + disk.f_bavail) + 1
percent = (disk.f_bavail * 100.0) / disk.f_blocks
return percent
##### end of parse gga #####
def parse_gga(sdata):
lat = sdata[2]
lat_dir = sdata[3]
long = sdata[4]
long_dir = sdata[5]
alt = sdata[9]
return lat + " " + lat_dir + " " + long + " " + long_dir + " " + alt + " M"
##### end of parse gga #####
def convert_to_dd(x):
x=float(x)
degrees = int(x) // 100
minutes = (x - 100*degrees) / 60
dd = str(round((degrees + minutes),6))
return dd
##### end of convert_to_dd #####
#def lat_long_decode(coord):
# #Converts DDDMM.MMMMM > DD MM' SS.SSS"
# x = coord.split(".")
# head = x[0]
# tail = x[1]
# deg = head[0:-2]
# min = head[-2:]
# #sec = str((float(coord[-6:]) * 60.0))
# sec = str((float("." + tail) * 60.0))
#
# #return deg + " " + min + " " + sec + " "
# return deg + u"\u00b0 " + min + "\' " + sec + "\""
#
###### end of lat_long_decode #####
def convert_raw_gps_fix_to_image_gps_fix(raw_gps_fix):
#4203.4338X N 08748.7831X W 215.3 M
print("here is the raw gps fix for finding da error")
print(raw_gps_fix)
sraw_gps_fix = raw_gps_fix.split(" ")
#lat = lat_long_decode(sraw_gps_fix[0])
lat = convert_to_dd(sraw_gps_fix[0])
lat_dir = sraw_gps_fix[1]
#long = lat_long_decode(sraw_gps_fix[2])
long = convert_to_dd(sraw_gps_fix[2])
long_dir = sraw_gps_fix[3]
alt = sraw_gps_fix[4]
return lat + " " + lat_dir + " " + long + " " + long_dir + " " + alt + " M"
##### end of convert_raw_gps_fix_to_image_gps_fix #####
def convert_raw_gps_fix_to_exif_lat(raw_gps_fix):
raw_lat = (raw_gps_fix.split(" "))[0]
deg = raw_lat[0:2]
min = raw_lat[2:4]
sec = str(int(float(raw_lat[4:9]) * 60.0))
#sec = str(float(raw_lat[5:9]) * 60.0 / 10000)
return deg + "/1," + min + "/1," + sec + "/1"
#return deg + "/1," + min + "/1," + sec + "/1000"
##### end of convert_raw_gps_fix_to_exif_lat #####
def convert_raw_gps_fix_to_exif_lat_dir(raw_gps_fix):
print("I'm in convert_raw_gps_fix_to_exif_lat_dir")
return (raw_gps_fix.split(" "))[1]
##### end of convert_raw_gps_fix_to_exif_lat_dir #####
def convert_raw_gps_fix_to_exif_long(raw_gps_fix):
raw_lat = (raw_gps_fix.split(" "))[2]
deg = raw_lat[0:3]
min = raw_lat[3:5]
sec = str(int((float(raw_lat[5:10]) * 60.0)))
return deg + "/1," + min + "/1," + sec + "/1"
#return deg + "/1," + min + "/1," + sec + "/1000"
##### end of convert_raw_gps_fix_to_exif_long #####
def convert_raw_gps_fix_to_exif_long_dir(raw_gps_fix):
return (raw_gps_fix.split(" "))[3]
##### end of convert_raw_gps_fix_to_exif_long_dir #####
def convert_raw_gps_fix_to_exif_alt(raw_gps_fix):
return (raw_gps_fix.split(" "))[4]
##### end of convert_raw_gps_fix_to_exif_alt #####
def nmea_cksum(data):
# http://doschman.blogspot.com/2013/01/calculating-nmea-sentence-checksums.html
# Thiss is a string, will need to convert it to hex for
# proper comparsion below
ck_sum = data[len(data) - 4:]
# String slicing: Grabs all the characters
# between '$' and '*' and nukes any lingering
# newline or CRLF
chksumdata = re.sub("(\n|\r\n)","", data[data.find("$")+1:data.find("*")])
# Initializing our first XOR value
csum = 0
# For each char in chksumdata, XOR against the previous
# XOR'd char. The final XOR of the last char will be our
# checksum to verify against the checksum we sliced off
# the NMEA sentence
for c in chksumdata:
# XOR'ing value of csum against the next char in line
# and storing the new XOR value in csum
csum ^= ord(c)
# Do we have a validated sentence?
if hex(csum) == hex(int(ck_sum, 16)):
return True
return False
##### end of convert_raw_gps_fix_to_exif_long #####
#################################
##### Start of main program #####
#################################
print("free disk space = " + str(round(disk_stat('/'),2)) + "%")
if (disk_stat('/')) < 10:
print("exitng due to full disk")
os.system("(rm -rf /home/pi/Tools/Status/*; touch /home/pi/Tools/Status/Disk_Full; crontab -r) &")
exit()
start_looking_for_GPS_time = time.time()
setup_time = str(start_looking_for_GPS_time - run_start_time)
print ("setup_time = " + setup_time)
logfile.write("setup_time = " + setup_time + "\n")
os.system("(rm -rf /home/pi/Tools/Status/*; touch /home/pi/Tools/Status/Imaging...) &")
print ("Looking for GPS Data")
logfile.write("Looking for GPS Data" + "\n")
#while True:
gps_time_out = 0
while gps_time_out < 35:
time.sleep(1.0)
data = ser.readline().decode('utf_8')
#data = ser.read_until()
sdata = data.split(',')
if sdata[0] == "$GPGGA" and sdata[6] in ("1","2"):
if nmea_cksum(data):
print ("GPS Checksum Passed")
logfile.write("GPS Checksum Passed" + "\n")
for fl in glob.glob("/home/pi/Tools/Camera/GPS/GPGGA*"):
os.remove(fl)
filename = "/home/pi/Tools/Camera/GPS/" + data[1:]
mode = 0o644
os.mknod(filename, mode)
print (filename,)
logfile.write(filename)
raw_gps_fix = parse_gga(sdata)
gps_flag = 2
break
else:
print ("GPS Checksum Failed")
# end of if sdata[0] == "$GPGGA" and sdata[6] in ("1","2")
elif sdata[0] == "$GPGGA" and sdata[6] == "0":
if nmea_cksum(data):
print ("GPS Checksum Passed")
logfile.write("GPS Checksum Passed\n")
for fl in glob.glob("/home/pi/Tools/Camera/GPS/GPGGA*"):
os.remove(fl)
filename = "/home/pi/Tools/Camera/GPS/" + data[1:]
mode = 0o644
os.mknod(filename, mode)
print (filename,)
logfile.write(filename)
print (gps_time_out)
logfile.write(str(gps_time_out) + "\n")
if gps_time_out>= 25:
# We are receiving data from the GPS, not can't get a fix.
# Giving up, we must be in the basement.
gps_flag = 1
break
else:
print ("GPS Checksum Failed")
logfile.write("GPS Checksum Failed" + "\n")
gps_flag = 0
gps_time_out += 1
##### end of gps_time_out while #####
ser.close()
print ("gps_flag = " + str(gps_flag))
logfile.write("gps_flag = " + str(gps_flag) + "\n")
##### done with gps #####
##### manuipilate gps strings to make them useful #####
start_GPS_string_manipulation_time = time.time()
gps_aquisition = str(start_GPS_string_manipulation_time - start_looking_for_GPS_time)
print ("gps_aquisition = " + gps_aquisition)
logfile.write("gps_aquisition = " + gps_aquisition + "\n")
if gps_flag == 0:
image_gps_fix = "GPS Not Available"
print ("GPS Not Responsive, proceeding to collect images.")
logfile.write("GPS Not Responsive, proceeding to collect images.")
exif_lat = '00/1,00/1,00.00/1'
exif_lat_dir = 'X'
exif_long = '000/1,00/1,00.0000/1'
exif_long_dir = 'X'
exif_alt = '0'
for fl in glob.glob("/home/pi/Tools/Camera/GPS/GPGGA*"):
os.remove(fl)
filename = "/home/pi/Tools/Camera/GPS/GPGGA,ERROR"
mode = 0o644
os.mknod(filename, mode)
if gps_flag == 1:
image_gps_fix = "GPS Fix Not Available"
print ("GPS Not Fix Available, proceeding to collect images.")
logfile.write("GPS Not Fix Available, proceeding to collect images.")
exif_lat = '00/1,00/1,00.00/1'
exif_lat_dir = 'X'
exif_long = '000/1,00/1,00.0000/1'
exif_long_dir = 'X'
exif_alt = '0'
if gps_flag == 2:
image_gps_fix = convert_raw_gps_fix_to_image_gps_fix(raw_gps_fix)
print ("Raw GPS Data = " + raw_gps_fix)
logfile.write("Raw GPS Data = " + raw_gps_fix + "\n")
# print ("Processed GPS Data = " + image_gps_fix)
# logfile.write("Processed GPS Data = " + image_gps_fix + "\n")
exif_lat = convert_raw_gps_fix_to_exif_lat(raw_gps_fix)
exif_lat_dir = convert_raw_gps_fix_to_exif_lat_dir(raw_gps_fix)
exif_long = convert_raw_gps_fix_to_exif_long(raw_gps_fix)
exif_long_dir = convert_raw_gps_fix_to_exif_long_dir(raw_gps_fix)
exif_alt = convert_raw_gps_fix_to_exif_alt(raw_gps_fix)
##### done with gps string manipulation #####
##### Imaging begins here! #####
start_create_image_tag_time = time.time()
gps_string_manipulation = str(start_create_image_tag_time - start_GPS_string_manipulation_time)
print ("gps_string_manipulation = " + gps_string_manipulation)
logfile.write("gps_string_manipulation = " + gps_string_manipulation + "\n")
#Create image of a rectangle for text background
#img = Image.new('RGB', (1944, 120), color=(255,255,255))
img = Image.new('RGB', (1944, 120), color=(0,0,0))
# place text on image, rotate and save as foreground.jpeg
font = ImageFont.truetype("/home/pi/Tools/Camera/dejavu/DejaVuSans-Bold.ttf",40)
d = ImageDraw.Draw(img)
# Black Text
#d.text((20,10), "Adler / Far Horizons GONet hostname: " + socket.gethostname(), font=font, fill=(0,0,0))
# Old version check remove after further testing
##version = ''.join(os.listdir('/home/pi/Tools/Version'))
#version = ''.join(glob.glob(os.path.join('/home/pi/Tools/Version', '*'))).split("/")[5]
# end of old version check
version_dir = os.listdir("/home/pi/Tools/Version")
#print(version_dir)
#print (len(version_dir))
if len(version_dir) == 0:
print("Empty directory using UNK")
version = 'UNK'
#os.system('touch {}'.format("/home/pi/Tools/Version/UNK"))
else:
version = ''.join(glob.glob(os.path.join('/home/pi/Tools/Version', '*'))).split("/")[5]
print (version)
# White Text
d.text((20,10), "Adler / Far Horizons " + socket.gethostname() + " " + version + " Exp: " + tag_raspistill_ss + " S"\
+ " ISO: " + str(ISO) + " WB: " + white_balance_gains, font=font, fill=(255,255,255))
# Next Line
d.text((20,70), strftime("%y%m%d %H:%M:%S", gmtime()) + " UTC " + image_gps_fix , font=font, fill=(255,255,255))
img.rotate(90,expand = True).save(scratch_dir + 'foreground.jpeg', 'JPEG')
# take a picture with pi cam!
start_imaging_time = time.time()
create_image_tag = str(start_imaging_time - start_create_image_tag_time)
print ("create_image_tag = " + create_image_tag)
logfile.write("create_image_tag = " + create_image_tag + "\n")
########### start of raspistill ##############
#image_file_name = socket.gethostname()[-3:] + "_" + (strftime("%y%m%d_%H%M%S", gmtime())) + "_%03d"
image_file_name = "R_" + socket.gethostname()[-3:] + "_" + (strftime("%y%m%d_%H%M%S", gmtime())) + "_%d"
print ("image_file_name = " + image_file_name)
logfile.write("image_file_name = " + image_file_name + "\n")
##command = ['/usr/bin/raspistill', '-v',
# '-t', str(raspistill_t),
# '-ss', str(raspistill_ss),
# '-tl', str(raspistill_tl),
# '-ISO', str(ISO),
# '-drc', str(drc),
# '-awb', awb,
# #'-awbg', '1.03125, 1.8086',
# '-awbg', white_balance_gains,
# '-br', str(br),
# '-r',
# '-ts',
# #'--timeout', '1',
# '-st',
# #'-set',
# '-x', 'GPS.GPSLongitude=' + exif_long,
# '-x', 'GPS.GPSLongitudeRef=' + exif_long_dir,
# '-x', 'GPS.GPSLatitude=' + exif_lat,
# '-x', 'GPS.GPSLatitudeRef=' + exif_lat_dir,
# '-x', 'GPS.GPSAltitude=' + exif_alt,
# #'-x', 'IFD0.Artist=GONet ' + white_balance_gains,
# '-x', 'IFD0.Software=' + socket.gethostname() + ' ' + version + ' WB: ' + white_balance_gains,
# #'-x', 'IDF0.HostComputer= ' + socket.gethostname(),
# '-o', scratch_dir + image_file_name + '.jpg']
#subprocess.call(command)
########## end of raspistill ##############
########### Start of picamera ##############
camera = PiCamera()
sleep(2)
# Set a framerate of 1/6fps, then set shutter
# speed to 6s and ISO to 800
camera.framerate = Fraction(1, 6)
camera.shutter_speed = raspistill_ss
camera.iso = ISO
camera.drc_strength=drc
camera.awb_mode = awb
camera.awb_gains = (1.03125, 1.8086)
camera.brightness = br
camera.still_stats = True
camera.resolution = (2592, 1944)
camera.exif_tags['GPS.GPSLongitude'] = exif_long
camera.exif_tags['GPS.GPSLongitudeRef'] = exif_long_dir
camera.exif_tags['GPS.GPSLatitude'] = exif_lat
camera.exif_tags['GPS.GPSLatitudeRef'] = exif_lat_dir
camera.exif_tags['GPS.GPSAltitude'] = exif_alt
camera.exif_tags['IFD0.Software'] = socket.gethostname() + ' ' + version + ' WB: ' + str(white_balance_gains)
camera.exif_tags['IFD0.ImageDescription'] = 'some number'
camera.exif_tags['EXIF.UserComment'] = 'some other number'
camera.exif_tags['EXIF.HostComputer'] = 'some number'
camera.exif_tags['IFD0.CameraSerialNumber'] = 'some number'
camera.exif_tags['EXIF.BodySerialNumber'] = 'some number'
camera.exif_tags['EXIF.SerialNumber'] = 'some number'
for x in range(5):
filename = "P_" + socket.gethostname()[-3:] + "_" + (strftime("%y%m%d_%H%M%S_%s", gmtime())) + ".jpg"
print(scratch_dir + filename)
camera.capture(scratch_dir +filename, bayer=True)
# The images collected with picamera yields a file about 2m small than raspisill
print("Closing Camera")
camera.close()
######### End of picamera ###########
start_post_processing_time = time.time()
imaging_time = str(start_post_processing_time - start_imaging_time)
print ("imaging_time = " + imaging_time)
logfile.write("imaging_time = " + imaging_time + "\n")
photo_count = 0
#post processing
#for filename in os.listdir("/home/pi/gonet3/scratch/"):
for filename in os.listdir(scratch_dir):
if filename.endswith(".jpg"):
sfilename = filename.split("_")
print (scratch_dir + filename)
logfile.write(scratch_dir + filename + "\n")
#logfile.write(scratch_dir + filename + "\n")
# open the the image from pi cam
background = Image.open(scratch_dir + filename).convert("RGB")
# save its exif - does not include raw (bayer) data
exif = background.info['exif']
#print(exif)
# open foreground.jpg and paste it to pi cam image
foreground = Image.open(scratch_dir + "foreground.jpeg")
background.paste(foreground, (0, 0)) #, foreground)
#save the new composite image with pi cam photo's exif
background.save(image_dir + filename, 'JPEG', exif=exif)
# open the composited file for append. then tail the raw file raw data from the original to the end of composited file.
composite_file = open(image_dir + filename, 'a')
subprocess.call(['tail', '-c', '10237440', scratch_dir + filename], stdout=composite_file)
# create thumnail here?
MAX_SIZE = (160, 120)
background.thumbnail(MAX_SIZE)
background.save(thumbs_dir + filename, 'JPEG')
# clean up the scratch directory
os.remove(scratch_dir + filename)
photo_count += 1
##### End of .jpg if
##### End of filename in directory for
print ("photo_count = " + str(photo_count))
logfile.write("photo_count = " + str(photo_count) + "\n")
os.system("(rm -rf /home/pi/Tools/Status/*; touch /home/pi/Tools/Status/Ready) &")
finish_time = time.time()
post_processing_time = str(finish_time - start_post_processing_time)
print ("post_processing_time = " + post_processing_time)
logfile.write("post_processing_time = " + post_processing_time + "\n")
total_run_time = str(finish_time - run_start_time)
print ("total_time = " + total_run_time)
logfile.write("total_time = " + total_run_time + "\n")
print ("perf," + str(run_start_time) + "," + setup_time + "," + gps_aquisition + "," + gps_string_manipulation + "," + create_image_tag + "," + imaging_time + "," + post_processing_time + "," + total_run_time + "," + str(gps_flag) + "," + str(photo_count))
print ("")
logfile.write("perf," + str(run_start_time) + "," + setup_time + "," + gps_aquisition + "," + gps_string_manipulation + "," + create_image_tag + "," + imaging_time + "," + post_processing_time + "," + total_run_time + "," + str(gps_flag) + "," + str(photo_count) + "\n")
logfile.write("\n")
logfile.flush()
logfile.close()
with open('/home/pi/Tools/Camera/gonet.log') as fin, open('/home/pi/Tools/Camera/temp_gonet.log', 'w') as fout:
fout.writelines(deque(fin, 10000))
os.remove("/home/pi/Tools/Camera/gonet.log")
os.rename("/home/pi/Tools/Camera/temp_gonet.log","/home/pi/Tools/Camera/gonet.log")