123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- from paddlers.deploy import Predictor
- import numpy as np
- import rasterio
- from rasterio.transform import from_bounds
- import os
- # 模型路径
- model_dir = "E:/PaddleRS-1.0.0/inference_model"
- assert os.path.exists(model_dir), "模型路径无效!"
- # 构建Predictor
- predictor = Predictor(model_dir, use_gpu=True)
- # 输入影像路径
- img1_path = "E:/PaddleRS-1.0.0/data/tuban/test/A/4301812024070509180603.tif"
- img2_path = "E:/PaddleRS-1.0.0/data/tuban/test/B/4301812024070509180603.tif"
- assert os.path.exists(img1_path), "前时相影像不存在!"
- assert os.path.exists(img2_path), "后时相影像不存在!"
- # 执行推理
- res = predictor.predict((img1_path, img2_path), warmup_iters=5, repeats=1)
- # 检查结果
- assert 'label_map' in res, "推理结果中缺少label_map键!"
- cm = res['label_map'] # 变化检测结果(numpy 数组)
- # 读取输入影像的地理信息
- with rasterio.open(img1_path) as src:
- # 获取输入影像的地理信息
- profile = src.profile # 包括 CRS、变换信息、元数据等
- transform = src.transform
- crs = src.crs # 坐标参考系
- # 更新输出影像的 profile(元数据)
- profile.update(
- dtype=rasterio.uint8, # 数据类型
- count=1, # 单波段
- compress='lzw', # 压缩方式
- driver='GTiff' # 输出格式为 GeoTIFF
- )
- # 保存变化检测结果为 GeoTIFF
- output_path = "E:/PaddleRS/predict/change_map.tif"
- with rasterio.open(output_path, 'w', **profile) as dst:
- # 写入变化检测结果
- dst.write(np.uint8(cm), 1) # 将数据写入第1个波段
- print(f"变化检测结果已保存为:{output_path}")
|