SimpleGVI 是一个简化版的绿视指数 (Green View Index, GVI) 计算工具,使用 Facebook 的 Mask2Former 模型进行语义分割,计算图像中植被区域的占比。
git clone https://github.com/yourusername/SimpleGVI.git
cd SimpleGVI
# 使用 uv(推荐)
uv venv && source .venv/bin/activate
uv pip install -r requirements.txt# 处理单张图像
uv run main.py examples/03_dense_forest.jpg --save_segmentation
# 批量处理文件夹
uv run batch_process.py path/to/images/ --save_segmentation --batch-size 2PowerShell 5.1 不支持 && 作为命令分隔符;需要连续执行多条命令时请使用 ;。
from modules.gvi_calculator import process_image, get_models
# 加载模型
processor, model = get_models()
# 处理图像
gvi, segmentation, image = process_image(
'examples/03_dense_forest.jpg',
is_panoramic=False,
processor=processor,
model=model
)
print(f'绿视指数:{gvi:.4f} ({gvi*100:.1f}%)')uv run streamlit run app.py --server.port 8501- CLI:
results/目录下生成结果文件和分割图 - 批量处理:
gvi_results.csv包含每张图像的tree_GVI、grass_GVI、plant_GVI、flower_GVI、palm_GVI和总GVI - Web UI: 实时显示结果,支持 CSV 导出
- 分割可视化: 输出三段竖向拼接图,依次为显示图、50% 透明分割叠加图、纯分割图
- 普通图和全景图都会先修正 EXIF 方向。
- 全景图会先裁剪底部 20%,用于减少全景地面畸变。
- 分割计算使用短边最大 384px 的图像,降低 Mask2Former 后处理显存占用。
- 批处理只加载一份模型,
--batch-size控制单次送入 GPU/CPU 推理的图像数量。 - 保存分割图时使用有背压的异步写图队列,避免 CPU 可视化阻塞 GPU 推理,同时限制内存积压。
- 可视化底图使用短边最大 1024px 的图像,避免输出图过大,同时保留较清晰的底图。
- 分割标签按 ADE20K 类别连通块生成:连通块太小不标注,连通块越大标签字号越大。
| GVI 范围 | 等级 |
|---|---|
| ≥ 0.30 | 优秀 🟢 |
| ≥ 0.15 | 良好 🟡 |
| < 0.15 | 较低 🔴 |
- torch
- transformers
- pillow
- numpy
- scipy
- pandas
- streamlit
完整依赖见 requirements.txt
# 测试示例图像
uv run main.py examples/03_dense_forest.jpg -s输出:
加载语义分割模型...
处理图像:examples/03_dense_forest.jpg
绿视指数 (GVI): 1.0000
分割结果已保存: results/03_dense_forest_segmentation.png
处理完成!
基于 StreetView-NatureVisibility 项目,使用 Facebook Mask2Former 模型。
MIT License