首页 > 解决方案 > 如何在 tfrecord 中使用 python API 从谷歌地球引擎下载哨兵图像

问题描述

在尝试下载特定位置的哨兵图像时,tif 文件默认在驱动器中生成,但 openCV 或 PIL.Image() 无法读取它。下面是相同的代码。如果我使用文件格式作为 tfrecord. 驱动器中没有下载图像。

starting_time = '2018-12-15' 
delta = 15  
L = -96.98  
B = 28.78  
R = -97.02  
T = 28.74

cordinates = [L,B,R,T] 
my_scale = 30 
fname = 'sinton_texas_30'


llx = cordinates[0] 
lly = cordinates[1] 
urx = cordinates[2] 
ury = cordinates[3]

geometry = [[llx,lly], [llx,ury], [urx,ury], [urx,lly]]

tstart = datetime.datetime.strptime(starting_time, '%Y-%m-%d') tend =
tstart+datetime.timedelta(days=delta)
collSent = ee.ImageCollection('COPERNICUS/S2').filterDate(str(tstart).split('')[0], str(tend).split(' ')[0]).filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20)).map(mask2clouds)


medianSent = ee.Image(collSent.reduce(ee.Reducer.median())) cropLand = ee.ImageCollection('USDA/NASS/CDL').filterDate('2017-01-01','2017-12-31').first() 
task_config = {
 'scale': my_scale,
 'region': geometry,
 'fileFormat':'TFRecord'
   }

f1 = medianSent.select(['B1_median','B2_median','B3_median'])


taskSent = ee.batch.Export.image(f1,fname+"_Sent",task_config)
taskSent.start()

我希望输出在 python 中是可读的,所以我可以转换为 numpy。如果文件格式为“tfrecord”,我希望该文件能够下载到我的驱动器中。

标签: python-3.xtifftfrecordgoogle-earth-enginesentinel2

解决方案


我认为你应该考虑以下几点:

文件格式

如果你想用 PIL 或 OpenCV 打开你的文件,而不是用 TensorFlow,你宁愿使用 GeoTIFF。尝试使用这种格式,看看情况是否有所改善。

保存到驱动器

通常保存到您的云端硬盘是默认行为。但是,您可以尝试强制写入驱动器:

ee.batch.Export.image.toDrive(image=f1, ...)

您可以进一步尝试设置一个文件夹,将图像发送到:

ee.batch.Export.image.toDrive(image=f1, folder='foo', ...)

此外,导出数据帮助页面和本教程是进一步研究的良好起点。


推荐阅读