-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistImages.py
More file actions
325 lines (303 loc) · 11.9 KB
/
listImages.py
File metadata and controls
325 lines (303 loc) · 11.9 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import argparse
import cv2
from datetime import date, datetime, timedelta
import os
import re
import events_from_log
from cat_detector import CatDetector
from explorer import imageExplorer
def list_dates(imagedir):
p = re.compile('(\d+)-(\d+)-(\d+).jpg')
dates = []
for file in os.listdir(imagedir):
m = re.match(p, file)
if m:
date_and_time = m.group(2)
dt = datetime.strptime(date_and_time, '%Y%m%d%H%M%S')
dates.append(date(dt.year, dt.month, dt.day))
return dates
def list_events_for_date(imagedir, date_to_show):
datestr = date_to_show.strftime('%Y%m%d')
p = re.compile('(\d+)-' + datestr + '(\d+)-(\d+).jpg')
s = re.compile('(\d+)-' + datestr + '(\d+)-snapshot.jpg')
events = {}
snapshots = {}
for fname in os.listdir(imagedir):
m = re.match(p, fname)
if m:
event_id = m.group(1)
timeofday = m.group(2)
idx = m.group(3)
if event_id not in events:
events[event_id] = []
events[event_id].append({'timeofday': timeofday,
'id': event_id,
'datetime': datestr + timeofday,
'idx': idx,
'filename': fname})
else:
m2 = re.match(s, fname)
if m2:
event_id = m2.group(1)
timeofday = m2.group(2)
houroftheday = timeofday[0:2]
snapshots[houroftheday] = {
'id': event_id,
'timeofday': timeofday,
'filename': fname}
for k, v in events.items():
v.sort(key=events_from_log.eventkey)
return (events, snapshots)
def get_next_date_after(imagedir, curdate):
try_this = curdate + timedelta(days=1)
print('trying date %s' % try_this.strftime('%Y-%m-%d'))
for i in range(30):
(ev, snapshots) = list_events_for_date(imagedir, try_this)
if ev:
return (try_this, ev, snapshots)
else:
try_this = try_this + timedelta(days=1)
print('no events for date %s' % try_this.strftime('%Y-%m-%d'))
print('no events found for 30 days starting from %s' % curdate.strftime('%Y-%m-%d'))
return (None, None, None)
def list_images(imagedir, date_to_show):
(events, snapshots) = list_events_for_date(imagedir, date_to_show)
if not events:
print('No events for %s' % date_to_show.strftime('%Y-%m-%d'))
print('There are %d events: ' % len(events))
keys = list(events.keys())
keys.sort()
for k in keys:
print(k)
x = events[k][0]
print('first img %s at %s' % (x['filename'], x['timeofday']))
def show_single_image(filename, detector):
img = cv2.imread(filename)
if img is None:
print('failed to read image at %s' % filename)
return
cv2.imshow(filename, img)
pressed = cv2.waitKey(0)
if pressed == 112: # 'p'
if detector is None:
print('you need a cat detector for this')
else:
retval = detector.evaluate_motion_and_image(None, img)
print('detector says: %s' % retval)
elif pressed == 120: # 'x'
xp = imageExplorer(filename, detector, labelfile)
xp.exploreImage(img, None, None, None)
cv2.destroyAllWindows()
def loop_images_no_log(imagedir, date_to_start, labelfile, detector):
if date_to_start is None:
dates = list_dates(imagedir)
if not dates:
print('no events in %s' % imagedir)
return
curdate = dates[0]
else:
curdate = date_to_start
(ev, snapshots) = list_events_for_date(imagedir, curdate)
while(True):
for idx in ev.keys():
cont = show_images_no_log(imagedir, curdate, idx, labelfile, detector)
if not cont:
break
(curdate, ev, snapshots) = get_next_date_after(imagedir, curdate)
if ev:
print('%d images found for %s' % (len(ev.keys()), curdate.strftime('%Y-%m-%d')))
cont = input("Continue with next day? ")
if (cont == 'n'):
return
else:
return
def show_images_no_log(imagedir, date_to_show, idx, labelfile, detector):
(ev, snapshots) = list_events_for_date(imagedir, date_to_show)
if idx not in ev:
print('no event %s for date %s' % (idx, date_to_show))
return True
windowname = '%s' % date_to_show
i = 0
filename = imagedir + ev[idx][i]['filename']
img = cv2.imread(filename)
if img is None:
print('failed to read image at %s' % filename)
return True
cv2.imshow(windowname, img)
print('showing image %d of %d' % (i, len(ev[idx])))
while(1):
pressed = cv2.waitKey(0)
print(pressed)
if pressed == 83: # cursor right
if i+1 >= len(ev[idx]):
print('at last image')
continue
else:
i += 1
elif pressed == 81: # cursor left
if i == 0:
print('at first image')
continue
else:
i -= 1
elif pressed == 108: # 'l'
if not detector:
print('you need a detector for this')
continue
print('cat (a)rriving, (l)eaving, (n)o cat, (w)ait for next image?')
label = cv2.waitKey(0)
if label == 97:
descstr = 'cat arriving'
elif label == 108:
descstr = 'cat leaving'
elif label == 110:
descstr = 'no cat'
elif label == 119:
descstr = 'not sure what exactly'
else: descstr = 'an unknown object'
coordstr = 'unknown'
coords = detector.get_coords(img)
if coords:
print(coords)
coordstr = ','.join(str(x.astype(int)) for x in coords)
print('image %s shows %s at %s' % (filename, descstr, coordstr))
labelfile.write('%s,%s,%s\n' % (filename, descstr, coordstr))
elif pressed == 112: # 'p'
if detector is None:
print('you need a cat detector for this')
else:
retval = detector.evaluate_motion_and_image(None, img)
print('detector says: %s' % retval)
elif pressed == 120: # 'x'
xp = imageExplorer(ev[idx][i]['filename'], detector, labelfile)
snapkey = ev[idx][i]['timeofday'][0:2]
if snapkey in snapshots:
snap = cv2.imread(imagedir + snapshots[snapkey]['filename'])
xp.exploreImage(img, snap, None, None)
else:
xp.exploreImage(img, None, None, None)
elif pressed == 116: # 't'
return False
else:
break
print('showing image %d of %d' % (i, len(ev[idx])))
filename = imagedir + ev[idx][i]['filename']
img = cv2.imread(filename)
cv2.imshow(windowname, img)
cv2.destroyAllWindows()
return True
def show_image(imagedir, events, snapshots, idx, detector, labelfile):
if idx not in events:
print('no event %s in list' % idx)
return
i = 0
img = cv2.imread(imagedir + events[idx][i]['filename'])
if img is None:
print('failed to find image at %s' % imagedir + events[idx][i]['filename'], flush=True)
ev = events[idx][i]
motion = (ev['changedpixels'], ev['width'], ev['height'], ev['x'], ev['y'])
previous_motion = None
print('motion for this image: %s' % (motion,))
windowname = idx
cv2.imshow(windowname, img)
snapkey = ev['datetime'][0:10]
snap = None
if snapkey in snapshots:
snap = cv2.imread(imagedir + snapshots[snapkey]['filename']+'-snapshot.jpg')
elif os.path.exists(imagedir + 'lastsnap.jpg'):
snap = cv2.imread(imagedir + 'lastsnap.jpg')
if snap is not None:
cv2.imshow('snapshot', snap)
while(1):
pressed = cv2.waitKey(0)
if pressed == 83: # cursor right
if i+1 >= len(events[idx]):
print('at last image')
continue
else:
i += 1
ev = events[idx][i]
previous_motion = motion
if 'changedpixels' in ev:
motion = (ev['changedpixels'], ev['width'], ev['height'], ev['x'], ev['y'])
print('motion for this image: %s' % (motion,))
else:
motion = None
print('missing motion for this image')
elif pressed == 81: # cursor left
if i == 0:
print('at first image')
continue
else:
i -= 1
ev = events[idx][i]
previous_motion = motion
if 'changedpixels' in ev:
motion = (ev['changedpixels'], ev['width'], ev['height'], ev['x'], ev['y'])
print('motion for this image: %s' % (motion,))
else:
motion = None
print('missing motion for this image')
elif pressed == 112: # 'p'
if detector is None:
print('you need a cat detector for this')
else:
retval = detector.evaluate_motion_and_image(motion, img)
print('detector says: %s' % retval)
elif pressed == 120: # 'x'
xp = imageExplorer(ev['filename'], detector, labelfile)
xp.exploreImage(img, snap, motion, previous_motion)
else:
break
print('showing image %d of %d' % (i, len(events[idx])))
img = cv2.imread(imagedir + ev['filename'])
cv2.imshow(windowname, img)
cv2.destroyAllWindows()
if __name__ == "__main__":
parser = argparse.ArgumentParser('view camera images')
parser.add_argument('--date', default=None, help='The day to show images for')
parser.add_argument('--idx', default=None, help='The event to show images for')
parser.add_argument('--images', default='./images/', help='Where your image files are')
parser.add_argument('--picture', default=None, help='Single picture filename')
parser.add_argument('--labelfile', default='/tmp/catlabels.csv', help='Where to write labels')
parser.add_argument('--daemonlog', default=None, help='File with cat flap daemon log messages')
parser.add_argument('--modelfile', default=None, help='File to load model from')
parser.add_argument('--loop', default=None, help='loop over all images in dir')
args = parser.parse_args()
date_to_show = None
if not args.date and args.daemonlog is not None:
with open(args.daemonlog) as daemonfile:
daemon_events = events_from_log.daemonlog_datelist(daemonfile)
print(daemon_events)
exit()
elif args.date == 'today':
date_to_show = date.today()
elif args.date is not None:
date_to_show = datetime.strptime(args.date, "%Y-%m-%d").date()
daemon_events = None
if args.daemonlog is not None and date_to_show is not None:
with open(args.daemonlog) as daemonfile:
daemon_events, snapshots = events_from_log.daemonlog_events_for_date(daemonfile, date_to_show)
if args.idx is None and args.picture is None and args.loop is None:
if daemon_events is not None:
for event_id, events in sorted(daemon_events.items()):
print('event_id: %s started at %s' % (event_id, events[0]['motiontime']))
elif date_to_show is not None:
list_images(args.images, date_to_show)
exit()
if not os.path.exists(args.labelfile):
with open(args.labelfile, 'w') as labelfile:
labelfile.write('filename,action,left,right,top,bottom\n')
cat_detector = None
if args.modelfile is not None:
CatDetector.setupCatDetector(args.modelfile, None)
cat_detector = CatDetector.makeCatDetector()
with open(args.labelfile, 'a') as labelfile:
if args.picture is not None:
show_single_image(args.picture, cat_detector)
elif daemon_events is not None:
show_image(args.images, daemon_events, snapshots, args.idx, cat_detector, labelfile)
elif args.loop is not None:
loop_images_no_log(args.images, date_to_show, labelfile, cat_detector)
else:
show_images_no_log(args.images, date_to_show, args.idx, None, labelfile, cat_detector)