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(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']
- with rasterio.open(img1_path) as src:
-
- profile = src.profile
- transform = src.transform
- crs = src.crs
- profile.update(
- dtype=rasterio.uint8,
- count=1,
- compress='lzw',
- driver='GTiff'
- )
- output_path = "E:/PaddleRS/predict/change_map.tif"
- with rasterio.open(output_path, 'w', **profile) as dst:
-
- dst.write(np.uint8(cm), 1)
- print(f"变化检测结果已保存为:{output_path}")
|