File: pytest_classify.py

package info (click to toggle)
jeolib-jiplib 1.1.6%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,028 kB
  • sloc: cpp: 40,743; python: 2,784; sh: 49; makefile: 24; ansic: 5
file content (56 lines) | stat: -rw-r--r-- 2,267 bytes parent folder | download
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
###############################################################################
# pytest_classify.py: classify
# Author(s): Pieter.Kempeneers@ec.europa.eu
# Copyright (c) 2016-2019 European Union (Joint Research Centre)
# License EUPLv1.2
# 
# This file is part of jiplib
###############################################################################

# History
# 2017/10/24 - Created by Pieter Kempeneers (pieter.kempeneers@ec.europa.eu)
# Change log


import argparse
import os
import math
import jiplib as jl

parser=argparse.ArgumentParser()
parser.add_argument("-input","--input",help="Path of the input raster dataset",dest="input",required=True,type=str)
parser.add_argument("-vector","--vector",help="Path of the sample vector dataset with labels",dest="vector",required=True,type=str)
parser.add_argument("-model","--model",help="Path of the model output filename used for training",dest="model",required=True,type=str)
parser.add_argument("-output","--output",help="Path of the classification output raster dataset",dest="output",required=True,type=str)
parser.add_argument("-classifier","--classifier",help="classifier (svm, ann)",dest="classifier",required=False,type=str,default="svm")
args = parser.parse_args()

try:
    print("createJim")
    jim=jl.createJim(args.input)
    print("createVector")
    sample=jl.createVector();
    print("open vector",args.vector)
    sample.open(args.vector)
    print("extractOgr")
    training=jim.extractOgr(sample,{'output':'training','oformat':'Memory','copy':'label'})
    if args.classifier == 'svm':
        #SVM classification
        print("training")
        training.train({'method':'svm','label':'label','model':args.model})
        print("classification")
        jim_classify=jim.classify({'method':'svm','model':args.model})
        jim_classify.write({'filename':args.output})
        jim_classify.close()
    else:
        #ANN classification
        training.train({'method':'ann','label':'label','model':args.model})
        jim_classify=jim.classify({'method':'ann','model':args.model})
        jim_classify.write({'filename':args.output})
        jim_classify.close()
    sample.close()
    training.close()
    jim.close()
    print("Success: classify")
except:
    print("Failed: classify")