



# -*- coding: utf-8 -*-
import os
import cv2
#------获取文件下下所有文件路径---
‘‘‘
输入:文件夹的路径
输出:文件列表 - 所有格式
‘‘‘
def file_name(file_dir):
for root, dirs, files in os.walk(file_dir):
#print(root) #当前目录路径
#print(dirs) #当前路径下所有子目录
#print(files) #当前路径下所有非目录子文件
return files
#imgpath="C:/Users/dongdong/Desktop/标定图/img/"
#files=file_name(imgpath)
#for path_i in files:
# print(path_i)
#------获取照片的GPS信息并转化60进制---
‘‘‘
输入:图像路径名称
输出:经纬度
‘‘‘
import json
import requests
import exifread
def GetGps(ImgPath):
with open(ImgPath, ‘rb‘) as f:
exif_dict = exifread.process_file(f)
# 经度
lon_ref = exif_dict["GPS GPSLongitudeRef"].printable
lon = exif_dict["GPS GPSLongitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",")
lon = float(lon[0]) + float(lon[1]) / 60 + float(lon[2]) / float(lon[3]) / 3600
if lon_ref != "E":
lon = lon * (-1)
# 纬度
lat_ref = exif_dict["GPS GPSLatitudeRef"].printable
lat = exif_dict["GPS GPSLatitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",")
lat = float(lat[0]) + float(lat[1]) / 60 + float(lat[2]) / float(lat[3]) / 3600
if lat_ref != "N":
lat = lat * (-1)
#print(‘照片的经纬度:‘, (lat, lon))
‘‘‘
# 调用百度地图api转换经纬度为详细地址
secret_key = ‘MAsVGINLNyTGiM4UulcaeluCekGnAFxj‘ # 百度地图api 需要注册创建应用
baidu_map_api = ‘http://api.map.baidu.com/reverse_geocoding/v3/?ak={}&output=json&coordtype=wgs84ll&location={},{}‘.format(secret_key, lat, lon)
content = requests.get(baidu_map_api).text
gps_address = json.loads(content)
# 结构化的地址
formatted_address = gps_address["result"]["formatted_address"]
# 国家(若需访问境外POI,需申请逆地理编码境外POI服务权限)
country = gps_address["result"]["addressComponent"]["country"]
# 省
province = gps_address["result"]["addressComponent"]["province"]
# 市
city = gps_address["result"]["addressComponent"]["city"]
# 区
district = gps_address["result"]["addressComponent"]["district"]
# 语义化地址描述
sematic_description = gps_address["result"]["sematic_description"]
print(ImgPath,‘照片的经纬度:‘, (lat, lon), formatted_address, gps_address["result"]["business"] ,)
#print(formatted_address) #陕西省西安市长安区Y243(东大路)
#print(gps_address["result"]["business"])#东大
‘‘‘
print(ImgPath,‘照片的经纬度:‘, (lat, lon))
return lat,lon
#------GPS可视化工具---
‘‘‘
输入:
locations 经纬度
output_path GPS可视化结果HTML 文件保存路径
file_name GPS可视化结果HTML 文件保存名字
输出:文件列表 - 所有格式
‘‘‘
##pip3 install folium
import folium #可视化工具
import os
def draw_gps(locations, output_path, file_name):
"""
绘制gps轨迹图
:param locations: list, 需要绘制轨迹的经纬度信息,格式为[[lat1, lon1], [lat2, lon2], ...]
:param output_path: str, 轨迹图保存路径
:param file_name: str, 轨迹图保存文件名
:return: None
"""
m = folium.Map(locations[0], zoom_start=15, attr=‘default‘) #中心区域的确定
folium.PolyLine( # polyline方法为将坐标用线段形式连接起来
locations, # 将坐标点连接起来
weight=3, # 线的大小为3
color=‘orange‘, # 线的颜色为橙色
opacity=0.8 # 线的透明度
).add_to(m) # 将这条线添加到刚才的区域m内
# 起始点,结束点
folium.Marker(locations[0], popup=‘<b>Starting Point</b>‘).add_to(m)
folium.Marker(locations[-1], popup=‘<b>End Point</b>‘).add_to(m)
# len(locations)
‘‘‘
for i in locations:
#folium.Marker(
#i, popup="<i>Click show a test</i>", tooltip="Click me!",icon=folium.Icon(color="red", icon="info-sign")
).add_to(m)
‘‘‘
m.save(os.path.join(output_path, file_name)) # 将结果以HTML形式保存到指定路径
def USE_GPS_Single():
# 1 获取文件夹所有照片
# 2 从图像解析GPS
# 3 调用GPS可视化结果
DirPatnList=["100_0001","100_0002","100_0003","100_0004","100_0005","100_0006","100_0007","100_0008","100_0009"]#文件夹名字
DirPatnList=["100_0009"]
for DirPatn in DirPatnList:
imgpath="F:/dongdong/0my_project/slam/航拍数据/NWPU_9/"+DirPatn+"/"#文件夹完整路径
files=file_name(imgpath)#获取文件夹下所有文件夹名字
locations=[]
for path_i in files:
if path_i[-4:] == ‘.JPG‘:
path_i="./NWPU_9/"+DirPatn+"/"+path_i #文件夹下图像路径
lat,lon=GetGps(path_i)
locations.append([float(lat),float(lon)])
else:
print("非指定文件类型:",path_i)
map_html=DirPatn+".html"
print("地图生成中请等待")
draw_gps(locations,"",map_html)
print(map_html,"地图网页生生结束,请用网页打开查看")
def USE_GPS_More():
# 1 获取文件夹所有照片名字
# 2 从图像解析GPS
# 3 调用GPS可视化结果
#文件夹名字
DirPatnList=["100_0001","100_0002","100_0003","100_0004","100_0005","100_0006","100_0007","100_0008"]#文件夹名字
#DirPatnList=["100_0009"]
locations=[]
for DirPatn in DirPatnList:
imgpath="F:/dongdong/0my_project/slam/航拍数据/NWPU_9/"+DirPatn+"/"#文件夹完整路径
files=file_name(imgpath)#获取文件夹下所有文件夹名字
for path_i in files:
if path_i[-4:] == ‘.JPG‘:
path_i="./NWPU_9/"+DirPatn+"/"+path_i #文件夹下图像路径
lat,lon=GetGps(path_i)
locations.append([float(lat),float(lon)])
else:
print("非指定文件类型:",path_i)
map_html="all_8"+".html"
print("地图生成中请等待")
draw_gps(locations,"",map_html)
print(map_html,"地图网页生生结束,请用网页打开查看")
USE_GPS_More()
原文:https://www.cnblogs.com/gooutlook/p/15253732.html