forked from csherwood-usgs/IPython_notebook_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exif2track.py
343 lines (282 loc) · 10.2 KB
/
exif2track.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
# coding: utf-8
# In[ ]:
# exif2track - script to plot photo locations and tabulate image info
# This is an ipython script that can be run in a Jupyter notebook
# after loading with the magic command %load exif2track.py
"""
Unless otherwise noted, the software and content on this site is
in the public domain because it contains materials developed by
the United States Geological Survey, an agency of the United States
Department of Interior. For more information, see the official USGS
copyright policy at:
http://www.usgs.gov/visual-id/credit_usgs.html#copyright
This software and content is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Any
dependent libraries included here are distributed under open source
(or open source-like) licenses/agreements. Appropriate license agreements
are included with each library.
"""
# 15-Jul-2016
# Includes code from Eddie Obropta, Raptor Maps, Inc.
import __future__
import sys
# argument parsing
import argparse
# glob for files
import glob
# file path tools
import os
# os.path import join, basename, splitdrive, split
# regular expression
import re
# math library
import math
# Numpy
import numpy as np
# GDAL Python library
from osgeo import osr
# EXIF Reader
import exifread
# Image library
from PIL import Image
import matplotlib.pyplot as plt
get_ipython().magic(u'matplotlib inline')
src_crs = 'EPSG:4326'
dst_crs = 'EPSG:26919'
# source coordinate system
srs_cs = osr.SpatialReference()
srs_cs.SetFromUserInput(src_crs)
# destination coordinate system
dst_cs = osr.SpatialReference()
dst_cs.SetFromUserInput(dst_crs)
# osr image transformation object
transform = osr.CoordinateTransformation(srs_cs, dst_cs)
# print coordinate system information
print " >> SOURCE COORDINATE REFERENCE SYSTEM: "
print srs_cs
print " >> DESTINATION COORDINATE REFERENCE SYSTEM: "
print dst_cs
def averageRGB(fpath):
"""
Return the average RBG color of an image
"""
im = Image.open(fpath)
h=np.array( im.histogram(), dtype='float64')
h=np.reshape(h,(3,256))
rgb = np.zeros((1,3), dtype='float64')
wts = np.arange(0,254, dtype='float64')
for i in range(0,3):
rgb[0,i] = sum(h[i,0:254]*wts)/sum(h[i,0:254])
return rgb
def getGPSFiles(filepath):
"""
Return a list of files with GPS data
Includes code from Eddie Obropta, Raptor Maps, Inc.
"""
files_gps = []
files = sorted(glob.glob(filepath))
n_images = len(files)
print("Found {0} files.\n".format(n_images))
n_images_GPS = 0
for file in files:
fs = open(file,'rb')
# intialize arrays
lat = []
lon = []
latref = []
lonref = []
alt = []
time = []
# attempt to read files
try:
tags = exifread.process_file(fs)
except:
print "No EXIF Data"
try:
try:
lon = tags['GPS GPSLongitude']
lat = tags['GPS GPSLatitude']
except:
print 'failed latlon'
try:
latref = tags['GPS GPSLatitudeRef']
lonref = tags['GPS GPSLongitudeRef']
except:
print 'failed latref or lonref'
files_gps.append(file)
n_images_GPS += 1
except KeyError:
print "No GPS data for this image: ",file
try:
alt = tags['GPS GPSAltitude']
except:
print "No altitude data for this image: ",file
# Get time information primarily from GPSTimeStamp
try:
time = tags['GPS GPSTimeStamp']
except KeyError:
time = tags['EXIF DateTimeOriginal']
print "No GPS Time Stamp for this image: ", file
# raise warning if no images have GPS data
if n_images_GPS == 0:
raise UserWarning('No images with GPS')
else:
print "Images with GPS: %d out of %d - %.2f%%" % (n_images_GPS, n_images, 100*n_images_GPS/n_images)
return files_gps
def transformCoordinateSystem(files):
"""
Transform Coordinate System
Read EXIF and GPS information from a list of pathnames
and return an np.array with UTM Zone 19 North coordinates.
Includes code from Eddie Obropta, Raptor Maps, Inc.
"""
nfiles = len(files)
print 'nfiles=',nfiles
# initialize arrays
fp_names = []
fp = np.ones((15,nfiles),dtype=float)
fp = np.nan*fp
# loop over files, check for GPS data
k = 0
for file in files:
fs = open(file,'rb')
tags = exifread.process_file(fs)
lon = tags['GPS GPSLongitude']
lat = tags['GPS GPSLatitude']
latref = tags['GPS GPSLatitudeRef']
lonref = tags['GPS GPSLongitudeRef']
try:
alt = tags['GPS GPSAltitude']
# alt_num = np.float(eval(str(alt))) # this does integer math on strings like 1284/2
alt_num = eval(compile(str(alt),'<string>','eval', __future__.division.compiler_flag))
# print alt, alt_num
except:
alt_num = -99.9
try:
time = tags['GPS GPSTimeStamp']
date = tags['GPS GPSDate']
timenum = np.nan
datenum = np.nan
# convert time tag
try:
times=(str(time))
exec 'timesa = np.array('+times+')'
timenum = timesa.astype(np.float)
except:
print('Could not convert time tag in ',file)
dates = str(date).split(':')
datesa = np.array(dates,dtype='|S4')
datenum = datesa.astype(np.float)
dta = np.concatenate((datenum,timenum),axis=0)
except:
time = tags['EXIF DateTimeOriginal']
time_split = str(time).split()
datenum = time_split[0].split(':')
time = time_split[1].split(':')
timenum = [float(i) for i in time]
dta = np.concatenate((datenum,timenum),axis=0)
print('Using EXIF time stamp in ',file)
# get average color
rbg = np.zeros((1,3))
try:
rgb = averageRGB(file)
except:
print('No color from ',file)
# convert time to seconds (Note: this does not work across midnight, and it is UTC, so it will cause problems eventually)
time_dec = (timenum[0]+timenum[1]/60.0 + timenum[2]/3600.0)*3600 # seconds
## Latitude
# use regular expression to get value between brackets
y_regx = re.search('\[(.*?)\]', str(lat))
y_regx = y_regx.group(1)
y_regx = y_regx.split(', ')
# properly convert values to floats
y = []
y.append(eval(compile(y_regx[0],'<string>','eval', __future__.division.compiler_flag)))
y.append(eval(compile(y_regx[1],'<string>','eval', __future__.division.compiler_flag)))
y.append(eval(compile(y_regx[2],'<string>','eval', __future__.division.compiler_flag)))
## Longitude
x_regx = re.search('\[(.*?)\]', str(lon))
x_regx = x_regx.group(1)
x_regx = x_regx.split(', ')
x = []
x.append(eval(compile(x_regx[0],'<string>','eval', __future__.division.compiler_flag)))
x.append(eval(compile(x_regx[1],'<string>','eval', __future__.division.compiler_flag)))
x.append(eval(compile(x_regx[2],'<string>','eval', __future__.division.compiler_flag)))
# convert from latitude longitude minutes seconds to decimal
ddx = (np.sign(x[0]))*(math.fabs(x[0]) + (x[1]/60.0) + (x[2]/3600.0))
ddy = (np.sign(y[0]))*(math.fabs(y[0]) + (y[1]/60.0) + (y[2]/3600.0))
# handle longitude west values to negative
if str(lonref) == 'W':
ddx = -1.0*ddx
# handle latitude south values to negative
if str(latref) == 'S':
ddy = -1.0*ddy
# get the coordinates in lat long
# TransformPoint(X,Y) i.e. -> (long, lat)
u = transform.TransformPoint(ddx, ddy)
# append file names to array
drive, path_and_file = os.path.splitdrive(file)
path, fn = os.path.split(path_and_file)
fp_names.append(fn)
r = rgb[0,0]/255.
g = rgb[0,1]/255.
b = rgb[0,2]/255.
# fill data array
fp[:,k]=[dta[0],dta[1],dta[2],dta[3],dta[4],dta[5],ddx,ddy,u[0],u[1],alt_num,time_dec,r,g,b]
# close connection to file
fs.close()
k = k+1
return fp_names,fp
# input arguments
# image folder
#imdir='flight_1_rgb_1'
#imdir='flight_1_rgb_2'
#imdir='flight_2_rgb_1'
imdir='flight_2_nir_1'
#imdir='testdir'
# path to image folder
#tdir = '/home/csherwood/crs/proj/2016_CACO/2016-3-1_raptormaps_cape_cod/'
tdir = 'd:/crs/proj/2016_CACO/2016-03-01_CACO_images/'
filetype = 'JPG'
# construct filepath
filepath = tdir+imdir+'/*.JPG'
print "Reading image files from ",filepath
files = getGPSFiles(filepath)
print "Transforming coordinate system"
fpnames,fp = transformCoordinateSystem(files)
# write formatted list of images
csvpath = tdir+imdir+'.csv'
fid = open(csvpath,'w')
for i in np.arange(fp.shape[1]):
fid.write("{0:s},{1:4.0f},{2:02.0f},{3:02.0f},{4:02.0f},{5:02.0f},{6:06.3f},{7:11.6f},{8:10.6f},{9: 11.3f},{10: 11.3f},{11:6.1f}\n" .format(fpnames[i],fp[0,i],fp[1,i],fp[2,i],fp[3,i],fp[4,i],fp[5,i],fp[6,i],fp[7,i],fp[8,i],fp[9,i],fp[10,i]))
fid.close()
# calculate ground speed
eps = np.finfo(float).eps
dx=np.diff(fp)
dist = np.sqrt( np.sum(dx[8:10,:]**2,axis = 0) )
dt = dx[11,::]
#print dx[:,]
print 'Mean delta xyz (m):',np.mean(dist)
print 'Mean delta t (s):',np.mean(dt)
speed = dist/(dt+eps)
print('Ground speed: Mean {0}, Max: {1}, Min: {2} m/s\n'.format(np.mean(speed), np.max(speed), np.min(speed)))
# plot UTM x,y with average image color
rgb = fp[12:15,:]
ss = np.ones_like(fp[1,:])*20.
fig,ax = plt.subplots(figsize=(8,20))
for i in range(len(fp[0,:])):
col = rgb[:,i]
try:
ax.scatter(fp[8,i],fp[9,i],s=60,c=rgb[:,i],edgecolors='none')
except:
print(col)
plt.xlabel('Easting (m)')
plt.ylabel('Northing (m)')
plt.title(imdir)
plt.xlim(420600,422200)
plt.ylim(4630000,4634000)
plt.gca().set_aspect('equal', adjustable='box')
plt.draw()
plt.savefig(tdir+imdir+'track.png')