-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·166 lines (123 loc) · 4.72 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from threading import Thread
import logging
import argparse
from lib import imagemerge, analysis_strategy
class Application:
def __init__(self, args=None):
self.parser = self.init_parser()
self.register_cli_arguments()
self.args = self.parser.parse_args(args)
self.logger = self.init_logger()
if not self.args.output_image:
self.args.output_image = 'tmp/foo.tiff'
else:
self.args.output_image = self.args.output_image[0]
def dispatch(self):
"""This is the place where the app does something
depending on its cli arguments.
Add functionality here (please oneliners to keep things tidy)"""
if self.args.debug:
global debug
debug = True
self.log_arguments()
if not self.args.nogui:
self.show_gui()
if self.args.merge_images:
self.merge_in_new_thread()
if self.args.lawn_grass:
self.count_lg_in_new_thread()
if self.args.dry_grass:
self.count_dg_in_new_thread()
if self.args.ndvi:
self.ndvi_in_new_thread()
if self.args.histogram:
self.histogram_in_new_thread()
return 0
def merge_in_new_thread(self):
img = self.args.merge_images
Thread(target = lambda: imagemerge.merge_to_file(\
img[0], img[1], img[2], self.args.output_image)).start()
def count_lg_in_new_thread(self):
Thread(target = lambda: analysis_strategy.count_lawn_grass(\
self.args.lawn_grass[0])).start()
def count_dg_in_new_thread(self):
Thread(target = lambda: analysis_strategy.count_dry_grass(\
self.args.dry_grass[0])).start()
def ndvi_in_new_thread(self):
bands = self.args.ndvi
Thread(target=lambda: \
analysis_strategy.ndvi_to_file(bands[0], bands[1],
self.args.output_image)).start()
def histogram_in_new_thread(self):
def compute():
print analysis_strategy.histogram(self.args.histogram[0],
int(self.args.histogram[1]))
Thread(target=compute).start()
def init_parser(self):
return argparse.ArgumentParser(prog='Vegetation Analyzer',
description='Does stuff.', epilog='Let us do the work.')
def register_cli_arguments(self):
self.parser.add_argument('-c','--commandline', action='store_true',
dest='nogui',
help="don't use a graphical user interface")
self.parser.add_argument('-d','--debug', action='store_true',
help="print debug information to stdout")
self.parser.add_argument('-o','--output', nargs=1,
type=argparse.FileType('w'),
default=sys.stdout,
help='write results to FILE (default is stdout)')
self.parser.add_argument('-oimg', '--output-image', nargs=1,
default=None, metavar=('outputimage.tif'),
help="Write resulting image into a file", dest='output_image')
self.parser.add_argument('-mi', '--merge-images', nargs=3,
default=None, metavar=('image1.tif'),
help="Merge images in rgb order", dest='merge_images')
self.parser.add_argument('-lg', '--lawn-grass', nargs=1,
default=None, metavar=('image1.tif'),
help="Count pixels which are lawn grass", dest='lawn_grass')
self.parser.add_argument('-dg', '--dry-grass', nargs=1,
default=None, metavar=('image1.tif'),
help="Count pixels which are dry grass", dest='dry_grass')
self.parser.add_argument('-ndvi', nargs=2,
default=None, metavar=('bandx.tif'),
help="Writes an image with the normalized difference vegetation index\
(band4-band3)/(band4+band3) into output image given with -oimg", dest='ndvi')
self.parser.add_argument('-hist', '--histogram', nargs=2,
default=None,
help="Creates histogram for each pixel value in (0, 255) with n bins",
dest='histogram')
self.parser.add_argument('--version', action='version',
version='%(prog)s 0.0.1')
def init_logger(self):
logger = logging.getLogger('vegetation')
if self.args.debug:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(relativeCreated)s %(name)s %(levelname)s: %(message)s")
stderrHandler = logging.StreamHandler(sys.stderr)
stderrHandler.setFormatter(formatter)
logger.addHandler(stderrHandler)
return logger
def show_gui(self):
from PyQt4 import QtCore, QtGui
from gui.mainwindow import MainWindow
QtCore.QCoreApplication.setOrganizationName('Rhoket Scientists');
QtCore.QCoreApplication.setOrganizationDomain('');
QtCore.QCoreApplication.setApplicationName('Vegetation Analyzer');
app = QtGui.QApplication(sys.argv)
window = MainWindow(args = self.args)
#window.setWindowState(QtCore.Qt.WindowMaximized)
window.show()
window.raise_()
return app.exec_()
def log_arguments(self):
self.logger.debug("=== DEBUG MODE ===")
self.logger.debug("command line arguments:")
for k in vars(self.args):
self.logger.debug('{0}: {1}'.format(k,vars(self.args)[k]))
if __name__ == '__main__':
Application().dispatch()