geotag_images_ulog.py
7.7 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
#!/usr/bin/env python
#######################################################################################
#
# DeltaTag: enhanced geo-referencing survey images
# @author: Sander Smeets (sander@droneslab.com)
# Copyright (c) 2018 Vertical Technologies
#
# DeltaTag provides an alternate method to geo-referencing images from survey missions.
# It uses the delta in seconds between the images and the delta between the triggers to
# match geo information with pictures, allowing for missing images.
#
# It uses the last image and last trigger event as a calibration point as errors are
# more likely to arrise in the first images (test triggers, booting cameras, etc)
#
# Note: DeltaTag does not make copies, it writes the Exif information directly to the images
#
# Install: pip install pyulog piexif Pillow numpy
# Run: python geotag_images_ulog.py [logfile] [image dir] (optional offset)
# eg: python geotag_images_ulog.py mylog.ulg ./images
#
# Parameters
# logfile: a ulog formatted logfile containing camera_capture events (survey missions)
# image_dir: the directory where the images are located
# offset (optional): skip [offset] triggers to reference the last image
# 0 means the last trigger event equals last image (default)
# 1 means the second last trigger event equals last image
# ...etc
#
# Parts included from https://gist.github.com/c060604/8a51f8999be12fc2be498e9ca56adc72
# Parts included from https://github.com/PX4/flight_review/
from __future__ import print_function
import os, sys, time, datetime, piexif
from pyulog import *
from pyulog.px4 import *
from PIL import Image
from fractions import Fraction
if(len(sys.argv)) < 3:
print("Usage: python geotag_images_ulog.py [logfile] [image dir]")
print("Example: python geotag_images_ulog.py mylog.ulg ./images")
print(len(sys.argv))
sys.exit()
logfile = sys.argv[1]
imageDir = sys.argv[2]
triggerOffset = 0
if(len(sys.argv) > 3):
triggerOffset = int(sys.argv[3])
def to_deg(value, loc):
"""convert decimal coordinates into degrees, munutes and seconds tuple
Keyword arguments: value is float gps-value, loc is direction list ["S", "N"] or ["W", "E"]
return: tuple like (25, 13, 48.343 ,'N')
"""
if value < 0:
loc_value = loc[0]
elif value > 0:
loc_value = loc[1]
else:
loc_value = ""
abs_value = abs(value)
deg = int(abs_value)
t1 = (abs_value-deg)*60
min = int(t1)
sec = round((t1 - min)* 60, 5)
return (deg, min, sec, loc_value)
def change_to_rational(number):
"""convert a number to rantional
Keyword arguments: number
return: tuple like (1, 2), (numerator, denominator)
"""
f = Fraction(str(number))
return (f.numerator, f.denominator)
class ULogException(Exception):
"""
Exception to indicate an ULog parsing error. It is most likely a corrupt log
file, but could also be a bug in the parser.
"""
pass
def load_ulog_file(file_name):
""" load an ULog file
:return: ULog object
"""
# The reason to put this method into helper is that the main module gets
# (re)loaded on each page request. Thus the caching would not work there.
# load only the messages we really need
msg_filter = ['camera_capture']
try:
ulog = ULog(file_name, msg_filter)
except FileNotFoundError:
print("Error: file %s not found" % file_name)
raise
# catch all other exceptions and turn them into an ULogException
except Exception as error:
traceback.print_exception(*sys.exc_info())
raise ULogException()
# filter messages with timestamp = 0 (these are invalid).
# The better way is not to publish such messages in the first place, and fix
# the code instead (it goes against the monotonicity requirement of ulog).
# So we display the values such that the problem becomes visible.
# for d in ulog.data_list:
# t = d.data['timestamp']
# non_zero_indices = t != 0
# if not np.all(non_zero_indices):
# d.data = np.compress(non_zero_indices, d.data, axis=0)
return ulog
class ULogException(Exception):
"""
Exception to indicate an ULog parsing error. It is most likely a corrupt log
file, but could also be a bug in the parser.
"""
pass
def load_ulog_file(file_name):
""" load an ULog file
:return: ULog object
"""
# The reason to put this method into helper is that the main module gets
# (re)loaded on each page request. Thus the caching would not work there.
# load only the messages we really need
msg_filter = ['camera_capture']
try:
ulog = ULog(file_name)
except FileNotFoundError:
print("Error: file %s not found" % file_name)
raise
# catch all other exceptions and turn them into an ULogException
except Exception as error:
traceback.print_exception(*sys.exc_info())
raise ULogException()
# filter messages with timestamp = 0 (these are invalid).
# The better way is not to publish such messages in the first place, and fix
# the code instead (it goes against the monotonicity requirement of ulog).
# So we display the values such that the problem becomes visible.
# for d in ulog.data_list:
# t = d.data['timestamp']
# non_zero_indices = t != 0
# if not np.all(non_zero_indices):
# d.data = np.compress(non_zero_indices, d.data, axis=0)
return ulog
ulog = load_ulog_file(logfile)
camera_capture = ulog.get_dataset('camera_capture')
count = len(camera_capture.data['timestamp_utc'])
init = round(camera_capture.data['timestamp_utc'][count-1-triggerOffset] / 1000000)
offsets = {}
for i in range(0, count):
test = round(camera_capture.data['timestamp_utc'][i] / 1000000)
offset = init - test
offsets[offset] = i
files = os.listdir(imageDir)
os.chdir(imageDir)
#files.sort(key=lambda x: os.path.getctime(x))
files.sort()
first = 0
for f in reversed(files):
img = Image.open(f)
exif_dict = piexif.load(img.info['exif'])
timestring = exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal]
timestamp = time.mktime(datetime.datetime.strptime(timestring, "%Y:%m:%d %H:%M:%S").timetuple())
if first == 0:
first = timestamp
print("Calibrating on",f,"as last image on",timestring)
print("")
print("[filename] [offset] [trigger seq] [lat] [lng] [alt]")
offset = first - timestamp
print((f), end=' ')
print((offset), end=' ')
if not offset in offsets:
offset += 1
if not offset in offsets:
offset += 1
if offset in offsets:
print((offsets[offset]), end=' ')
print((camera_capture.data['lat'][offsets[offset]]), end=' ')
print((camera_capture.data['lon'][offsets[offset]]), end=' ')
print(camera_capture.data['alt'][offsets[offset]])
lat = camera_capture.data['lat'][offsets[offset]]
lng = camera_capture.data['lon'][offsets[offset]]
altitude = camera_capture.data['alt'][offsets[offset]]
lat_deg = to_deg(lat, ["S", "N"])
lng_deg = to_deg(lng, ["W", "E"])
exiv_lat = (change_to_rational(lat_deg[0]), change_to_rational(lat_deg[1]), change_to_rational(lat_deg[2]))
exiv_lng = (change_to_rational(lng_deg[0]), change_to_rational(lng_deg[1]), change_to_rational(lng_deg[2]))
gps_ifd = {
piexif.GPSIFD.GPSVersionID: (2, 0, 0, 0),
piexif.GPSIFD.GPSAltitudeRef: 0,
piexif.GPSIFD.GPSAltitude: change_to_rational(round(altitude)),
piexif.GPSIFD.GPSLatitudeRef: lat_deg[3],
piexif.GPSIFD.GPSLatitude: exiv_lat,
piexif.GPSIFD.GPSLongitudeRef: lng_deg[3],
piexif.GPSIFD.GPSLongitude: exiv_lng,
}
exif_dict["GPS"] = gps_ifd
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, f)
else:
print("Could not georeference")
print("Done")