def ConvertToBase64(src_filepath):
with open(src_filepath, 'rb') as imageFileAsBinary:
fileContent = imageFileAsBinary.read()
b64_encoded_img = base64.b64encode(fileContent)
def ServerLocalize(url, token, mapId, imagePath):
complete_url = url + '/localizeb64'
"fx": 727.87, # hard-coded camera intrinsics for the test image
"b64": str(ConvertToBase64(imagePath), 'utf-8'),
"mapIds": [{"id": mapId}]
json_data = json.dumps(data)
r = requests.post(complete_url, data=json_data)
def ServerECEF(url, token, mapId):
complete_url = url + '/ecef'
json_data = json.dumps(data)
r = requests.post(complete_url, data=json_data)
def EcefToWGS84(ecefPos):
e = np.double(8.1819190842622e-2)
x = np.double(ecefPos[0])
y = np.double(ecefPos[1])
z = np.double(ecefPos[2])
b = np.sqrt(asq * (1.0 - esq))
ep = np.sqrt((asq - bsq) / bsq)
p = np.sqrt(np.power(x, 2) + np.power(y, 2))
th = np.arctan2(a * z, b * p)
lat = np.arctan2((z + np.power(ep, 2) * b * np.power(np.sin(th), 3)), (p - esq * a * np.power(np.cos(th), 3)))
n = a / (np.sqrt(1.0 - esq * np.power(np.sin(lat), 2)))
alt = p / np.cos(lat) - n
return np.array([lat, lon, alt])
def MapPosToEcef(mapPos, m2e):
mp = np.array([ -mapPos[1], -mapPos[0], mapPos[2], 1.0 ])
m = np.array([ [S*m2e[3], S*m2e[6], S*m2e[9], 0.0],
[S*m2e[4], S*m2e[7], S*m2e[10], 0.0],
[S*m2e[5], S*m2e[8], S*m2e[11], 0.0],
[ m2e[0], m2e[1], m2e[2], 1.0] ])
def Localize(url, token, mapId, imagePath):
localizationResult = json.loads(ServerLocalize(url, token, mapId, imagePath))
mapPos = np.array([localizationResult['px'], localizationResult['py'], localizationResult['pz'], 1.0])
ecefResult = json.loads(ServerECEF(url, token, mapId))
mapToEcef = np.array(ecefResult['ecef'])
ecefPos = MapPosToEcef(mapPos, mapToEcef)
wgs84 = EcefToWGS84(ecefPos)
print('lat: ' + str(wgs84[0].astype(float)),
'long: ' + str(wgs84[1].astype(float)),
'alt: ' + str(wgs84[2].astype(float)))
# Open location in Google Maps
webbrowser.open_new_tab(f'https://www.google.com/maps/search/{str(wgs84[0])},{str(wgs84[1])}')
Localize('https://api.immersal.com', '', 4054, 'test.png')