-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclassify.py
151 lines (119 loc) · 3.89 KB
/
classify.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
###############################################################################
# classify.py
#
# sheneman@uidaho.edu
#
# Loads a saved and trained scikit-learn machine learning model from
# disk and uses that model to classify all raw images within the
# specified directory.
#
# Usage:
# python classify.py [ --help | --verbose | --config=<YAML config file> ]
#
###############################################################################
import os
import sys
import getopt
import yaml
import pickle
import numpy
import cv2
from os import listdir
from os.path import isfile, join
from PIL import Image
import preprocess
numpy.set_printoptions(threshold=sys.maxsize)
###############################################################################
#
# HANDLE Command line arguments
#
#
def usage():
print("python classify.py [ --help | --verbose | --config=<YAML config filename> ] ")
try:
opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "config="])
except getopt.GetoptError as err:
print(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
configfile = None
verbose = False
for o, a in opts:
if o == "-v":
verbose = True
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-c", "--config"):
configfile = a
else:
assert False, "unhandled option"
if(configfile == None):
configfile="classify.yaml"
###############################################################################
###############################################################################
#
# Format and Example config YAML file:
#
# FORMAT:
# -------
# inputlist: <path to a text file that names which files to classify>
# rawdir: <path to raw images>
# model: <path to the machine learning model to use for classification>
# outputdir: <path to output folder>
#
# EXAMPLE:
# --------
# inputlist: "./input_filenames.txt"
# rawdir: "../images/raw"
# model: "./models/rf.model"
# outputdir: "./output_directory"
#
###############################################################################
cf = open(configfile, "r")
config = yaml.load(cf, Loader=yaml.FullLoader)
print("YAML CONFIG:")
for c in config:
print(" [%s]:\"%s\"" %(c,config[c]))
print("\n")
cf.close()
#
# get feature labels
#
flabels = preprocess.feature_labels()
nfeatures = preprocess.feature_count(flabels)
print("Number of Feature Labels: %d" %nfeatures)
# get the list of raw files to classify
filenames = [line.rstrip('\n') for line in open(config["inputlist"])]
# Load the Trained Classifier Model
print("Loading Trained Classifier Model = %s" %config["model"])
classifier = pickle.load(open(config["model"],'rb'))
classifier.verbose = False
print("Trained Classifier Model Loaded!")
##############################################################################
# Load all of the raw images from the specified input directory and perform
# image segmentation (pixel classification) using the loaded machine learning
# model.
#
# For each image, extract all features for every pixel in the raw image and
# pass the 2D feature array to the model for classification.
for filename in filenames:
output = config["outputdir"] + "/" + filename
rawpath = config["rawdir"] + "/" + filename
print("Loading: %s" %rawpath)
raw_img = Image.open(rawpath)
numcols,numrows = raw_img.size
raw_cv2 = numpy.array(raw_img)
raw_img.close()
# extract feature array for loaded image
data = preprocess.image_preprocess(filename, nfeatures, raw_cv2)
print("Image Size: numcols=%d x numrows=%d" %(numcols,numrows))
print("Num Features: %d" %nfeatures)
# classify our input pixels and their feature vector
Y_pred = classifier.predict(data)
predicted_array = numpy.reshape(Y_pred,(numrows,numcols),order='F')
predicted_array = cv2.normalize(predicted_array,None,0,255,cv2.NORM_MINMAX,cv2.CV_8U)
predicted_image = Image.fromarray(predicted_array)
predicted_image.save(output, "TIFF")
predicted_image.close()
exit(0)