-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluate_image_classification.py
73 lines (58 loc) · 2 KB
/
Evaluate_image_classification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import sys
from os.path import isfile, join
from osgeo import ogr, osr
from zipfile import ZipFile
import subprocess
def main():
# input land cover image
land_cover = sys.argv[1]
# input shapefile with validation samples
val_file = sys.argv[2]
# field name in shapefile that contains the label
field = sys.argv[3]
# output report
report = sys.argv[4]
# output confusion matrix
conf_matrix = sys.argv[5]
# prepare input shapefile name
val_shp = val_file[:-4]+'.shp'
# unzip compressed files
unzip_shapefile(val_file)
# validate land cover classification
evaluate_classification(land_cover, val_shp, field, report, conf_matrix)
# clean folders from shapefile-related files other than zip files
clean_folder(val_file)
def unzip_shapefile(in_file):
with ZipFile(in_file, 'r') as zipObj:
zipObj.extractall(os.path.dirname(in_file))
# function that removes all shapefile related files other than the zip file
def clean_folder(zip_file):
# determine folder
folder = os.path.dirname(zip_file)
# pattern
remove_pattern = os.path.basename(zip_file)[:-4]
# list all files in folder
all_files = [f for f in os.listdir(folder) if isfile(join(folder, f))]
# remove the files
for item in all_files:
if remove_pattern in item and '.zip' not in item:
os.remove(join(folder, item))
def evaluate_classification(land_cover, val_shp, field, report, conf_matrix):
# prepare the system call
otb_call = []
otb_call.append('otbcli_ComputeConfusionMatrix')
otb_call.append('-in')
otb_call.append(land_cover)
otb_call.append('-ref')
otb_call.append('vector')
otb_call.append('-ref.vector.in')
otb_call.append(val_shp)
otb_call.append('-ref.vector.field')
otb_call.append(field)
otb_call.append('-out')
otb_call.append(conf_matrix)
returned_string = subprocess.check_output(otb_call)
with open(report, "wb") as f:
f.write(returned_string)
main()