-
Notifications
You must be signed in to change notification settings - Fork 26
/
sc_logger.py
218 lines (166 loc) · 6.68 KB
/
sc_logger.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/python
import logging
import sys
import os
import cv2
import time
import sc_config
#TODO allow images to be displayed over webserver and possibly print statements
class SmartCameraLogger(object):
#level enumerations: text
#general - logs/prints inforamtion about program activities(starting processes, camera failure, mode change, errors)
GENERAL = 'general'
#aircraft - logs/prints information coming from autopilot
AIRCRAFT = 'aircraft'
#algorithm - logs/prints information returned by image processing algorithm
ALGORITHM = 'algorithm'
#performance - logs/prints performance of the program
PERFORMANCE = 'performance'
#debug - logs/prints temporary messages to be used while developing
DEBUG = 'debug'
#level enumeration: image
#raw - record/display raw video from camera
RAW = 'raw'
#gui - record/display raw video overlayed with a gui
GUI = 'gui'
####Other enumerations made be added as necessary. These are just the defaults
def __init__(self):
#create sub driectory for logs and videos
self.location = sc_config.config.get_string('logging','location','./')
path = self.location + 'logs'
if not os.path.exists(path):
os.makedirs(path)
path = self.location + 'vids'
if not os.path.exists(path):
os.makedirs(path)
#set default startegy name
self.strat_name = 'Smart_Camera'
# get image resolution
self.img_width = sc_config.config.get_integer('camera','width',640)
self.img_height = sc_config.config.get_integer('camera','height',480)
#####TEXT LOGGING######
#levels = 'debug' , 'general' , 'aircraft' , 'algorithm' , ' performance'
#multiple message levels can be selected by concatination strings i.e. 'debug, aircraft'
#what type of messages we print to the terminal
self.print_level = sc_config.config.get_string('logging','print_level','debug, general')
#what type of messages we log to a file
self.log_level = sc_config.config.get_string('logging','log_level','aircraft , algorithm, general')
#####VIDEO RECORDING######
#levels = 'frame' , 'gui'
#multiple message levels can be selected at once by concatination strings i.e. 'frame, gui'
#what type of images we display on the screen
self.display_level = sc_config.config.get_string('logging', 'display_level', 'raw, gui')
#what type of images we record
self.record_level = sc_config.config.get_string('logging', 'record_level', 'raw')
#Note about useful logging practices:
#Inorder to replay a flight through the program log_level must include 'aircraft' and 'algorithm'
#record level must include 'frame'
#all other logging levels are for user diagnostics
#a list of video writers and their tag
self.video_writers = []
#text logger
self.logger = None
#set_name - give the logger a name. Helps when creating and understanding file names
def set_name(self, strat_name):
#define the name of the current strategy
self.strat_name = strat_name
#image - records/displays an image with the provided level
def image(self, level, img):
#display frame
if(self.display_level.find(level) != -1):
#show image
cv2.imshow(level, img)
#end program with esc key
k = cv2.waitKey(1) & 0xFF
if k == 27:
cv2.destroyAllWindows()
self.text(self.GENERAL, "User terminated Program")
sys.exit(0)
#record frame
if(self.record_level.find(level) != -1):
writer = self.get_video_writer(level)
writer.write(img)
#text - logs/prints a message with the provided level
def text(self, level, msg):
#print text
if(self.print_level.find(level) != -1):
print msg
#log text
if(self.log_level.find(level) != -1):
#open a logger if necessary
if(self.logger is None):
self.open_text_logger()
#log text
self.logger.info(msg , extra={'type': level})
#open_video_writer- opens a video writer with a filename that starts the tag. The rest of the file name is date-time
def open_video_writer(self, tag):
#define filename
path = self.location + 'vids/{0}-{1}-%Y-%m-%d-%H-%M.avi'
filename = time.strftime(path.format(self.strat_name, tag))
# Define the codec and create VideoWriter object
# Note: setting ex to -1 will display pop-up requesting user choose the encoder
# Frame rate is hard coded in despite actaul recording rate: 15 fps
ex = int(cv2.cv.CV_FOURCC('M','J','P','G'))
video_writer = cv2.VideoWriter(filename, ex, 15, (self.img_width,self.img_height))
#add the video writer to a list of video writers
self.video_writers.append((tag,video_writer))
#return the writer
return video_writer
#get_video_writer- retreives a videowriter assocaited with a certain tag
def get_video_writer(self,tag):
#look for video writer
for writer in self.video_writers:
if(writer[0] == tag):
return writer[1]
#if it doesn't exist, open one
return self.open_video_writer(tag)
#open_text_logger - create an instance of 'logging' and
def open_text_logger(self):
'''
#configure logger
#format of log line: time - log level - message
FORMAT = '%(asctime)s - %(type)s - %(message)s'
#define filename
path = self.location + 'logs/{0}-%Y-%m-%d-%H-%M.log'
filename = time.strftime(path.format(self.strat_name))
logging.basicConfig(format=FORMAT, level=logging.INFO, filename= filename)
#create logger
self.logger = logging.getLogger()
'''
#create logger
self.logger = logging.getLogger('sc_logger')
self.logger.setLevel(logging.INFO)
# add a file handler
#define filename
path = self.location + 'logs/{0}-%Y-%m-%d-%H-%M.log'
filename = time.strftime(path.format(self.strat_name))
fh = logging.FileHandler(filename)
fh.setLevel(logging.INFO)
# create a formatter and set the formatter for the handler.
frmt = logging.Formatter('%(asctime)s - %(type)s - %(message)s')
fh.setFormatter(frmt)
# add the Handler to the logger
self.logger.addHandler(fh)
self.logger.propogate = False
#create global logger
sc_logger = SmartCameraLogger()
if __name__ == '__main__':
sc_logger.set_name('test')
#test text logger
sc_logger.text(sc_logger.DEBUG,"This is should appear in the terminal")
sc_logger.text(sc_logger.AIRCRAFT, "This should appear in the file")
sc_logger.text(sc_logger.GENERAL, "This should appear in both")
sc_logger.text(sc_logger.PERFORMANCE, "This should not appear at all")
#test video logger
#create images
import numpy as np
green_image = np.zeros((sc_logger.img_height,sc_logger.img_width,3), np.uint8)
green_image[:] = (0,255,0)
blue_image = np.zeros((sc_logger.img_height,sc_logger.img_width,3), np.uint8)
blue_image[:] = (255,0,0)
for x in range(0, 45):
time.sleep(66.6/1000)
#Display a green and blue image for 3 seconds
#Save a 3 second video of the blue image
sc_logger.image(sc_logger.RAW, blue_image)
sc_logger.image(sc_logger.GUI, green_image)