I'm using a pytorch machine learning framework making predictions on raster images. Single images can be predicted using the following line in cmd:
rastervision predict --vector-label-uri /path/output_json /path/model-bundle.zip /path/rasterfile_to_make_prediction_on.tif /path/output_tif
My idea is to write a python code using a loop/iterate a directory with input files and call it from cmd. The results should be json and tiff files.The "model bundle.zip" is static but the "rasterfile_to_make_prediction_on.tif", "output_json" and "output_tif" should have namnes according to input files in directory.
I wonder how I should apply the iteration of files.
CodePudding user response:
Spawn a child command with check_call().
from subprocess import check_call
from pathlib import Path
def get_cmd(file: Path):
json = file.with_suffix('.json')
tiff = file.with_suffix('.tiff')
return ("rastervision predict --vector-label-uri"
f" {json} /path/model-bundle.zip {file} {tiff}")
input_dir = Path("/some/where")
for file in input_dir.glob("*.tif"):
check_call(get_cmd(file), shell=True)