-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeotag.py
64 lines (44 loc) · 1.5 KB
/
geotag.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
from PIL.ExifTags import TAGS
from PIL import Image
from urllib.request import urlopen
import cv2
import numpy as np
url='http://192.168.0.101:8080/shot.jpg'
def get_exif(filename):
image = Image.open(filename)
image.verify()
return image._getexif()
from PIL.ExifTags import GPSTAGS
def get_geotagging(exif):
if not exif:
raise ValueError("No EXIF metadata found")
geotagging = {}
for (idx, tag) in TAGS.items():
if tag == 'GPSInfo':
if idx not in exif:
raise ValueError("No EXIF geotagging found")
for (key, val) in GPSTAGS.items():
if key in exif[idx]:
geotagging[val] = exif[idx][key]
return geotagging
def get_decimal_from_dms(dms, ref):
degrees = dms[0][0] / dms[0][1]
minutes = dms[1][0] / dms[1][1] / 60.0
seconds = dms[2][0] / dms[2][1] / 3600.0
if ref in ['S', 'W']:
degrees = -degrees
minutes = -minutes
seconds = -seconds
return round(degrees + minutes + seconds, 5)
def get_coordinates(geotags):
lat = get_decimal_from_dms(geotags['GPSLatitude'], geotags['GPSLatitudeRef'])
lon = get_decimal_from_dms(geotags['GPSLongitude'], geotags['GPSLongitudeRef'])
return (lat,lon)
while True:
imageResp=urlopen(url)
imgNp=np.array(bytearray(imageResp.read()),dtype=np.uint8)
frame1=cv2.imdecode(imgNp,-1)
cv2.imwrite('test2.jpg',frame1)
exif = get_exif('test2.jpg')
geotags = get_geotagging(exif)
print(get_coordinates(geotags))