/
displaycam.py
executable file
·464 lines (378 loc) · 19.2 KB
/
displaycam.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# Captures and displays video images
# Dependencies: PyGame >= 1.9
from __future__ import print_function, division, absolute_import
###################################################################
# TODO:
# - display/hide grid with calib raster, shortcut 'g'
# - make more OO IN PROGRESS
# - split screen to display last/loaded still image side by side or overlayed with video
# - distance measuring tool
# - text input & output in PyGame
# - make lines with nice whiskers
# - save & load calibration (only values?)
# - move video offsets with arrow keys
# - make it possible to use different video & screenshot resolutions
# This will be difficult, because the UVC device driver on Linux does not support
# still image capture :-( (atm. see http://www.ideasonboard.org/uvc/ for current info)
# (screenshot always in max cam resolution?)
# current behaviour: screenshot res = vid res
# - 'v' to start/stop saving video. What format? H264 is good, but does a Python lib exists? Seems: no (py264 is dead)
# - DEBUG: Behaviour on draw calib line is not 100% consistent. Sometimes more than one calibration
# is invoked, sometimes it does not recognize mouse button up.
# - DEBUG: moving video when cam res is bigger than screen res does not redraw properly
# - DEBUG: erasing of grid does not work! (lines 140 & 259)
###################################################################
__author__ = 'Michael Harig <floss@michaelharig.de>'
__version__ = '0.2'
__license__ = '''GPL v3'''
import os.path as op
import argparse
import sys
import math
from collections import namedtuple
from operator import add, sub
# Python2 & 3 compatibility
try: import configparser
except NameError: import ConfigParser as configparser
# Python2 & 3 compatibility
try: input = raw_input
except NameError: pass
# PyGame >= 1.9 must be installed!
import pygame
import pygame.camera as camera
from pygame.locals import HWSURFACE,DOUBLEBUF,RESIZABLE
# define the mouse "buttons"
( LEFT,
MIDDLE,
RIGHT,
WHEEL_UP,
WHEEL_DOWN) = range(1,6)
# just make args.device global
DEVICE = ''
INIFILE = '.dc'
# objects that are drawn to foreground or other surface, like lines and text
PGObject = namedtuple('PGobject', ('drawFunction', 'surface', 'parameters', 'visible'))
# Calibration values
CalibValue = namedtuple('CalibValue', ('pixels', 'worlddim'))
# Measured values
class MeasuredValue(object):
def __init__(self, _pixels, _result):
self.pixels = _pixels
self.result = _result
### main PyGame loop
class PGLoop(object):
[DRAGGING, DRAWCALIB, DRAWMEASURE, NONE] = range(4)
def __init__(self, _args, _cfg):
self.args = _args
self.cfg = _cfg
self.state = PGLoop.NONE
self.measuredValues = [] # list of namedtuples
self.calibValues = [] # list of namedtuples
self.WORLDSCALE = _cfg.getfloat('MEASURING', 'worldscale')
# assets
self.calibLines = []
self.measureLines = []
self.grid = []
def __call__(self):
# Flags and more
DRAWCALIB_FLAG = False
DRAWMEASURE_FLAG = False
DRAWGRID_FLAG = False
XOFFSET = 0
YOFFSET = 0
DONE = False
# counter for saved images
IMGCOUNTER = 1
# video image
snapshot = pygame.surface.Surface((self.args.screen.get_width(), self.args.screen.get_height()), 0, self.args.screen)
# lines & text
foreground = pygame.surface.Surface((self.args.screen.get_width(), self.args.screen.get_height()))
foreground.convert() # for faster blitting
foreground = foreground.convert_alpha() # faster blitting with transparent color
while not DONE:
### event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
DONE = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
DONE = True
elif event.key == pygame.K_LCTRL or event.key == pygame.K_RCTRL:
DRAWCALIB_FLAG = True
elif event.key == pygame.K_LSHIFT or event.key == pygame.K_RSHIFT:
DRAWMEASURE_FLAG = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LCTRL or event.key == pygame.K_RCTRL:
DRAWCALIB_FLAG = False
elif event.key == pygame.K_LSHIFT or event.key == pygame.K_RSHIFT:
DRAWMEASURE_FLAG = False
elif event.key == pygame.K_DELETE:
if not DRAWCALIB_FLAG:
self.deleteMeasurements()
else:
self.deleteCalibration()
elif event.key == pygame.K_g:
if not DRAWGRID_FLAG:
self.grid = self.makeGrid(foreground, 1.0/self.WORLDSCALE)
else:
self.grid = []
DRAWGRID_FLAG = not DRAWGRID_FLAG
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == RIGHT: # right mouse button click
img = self.args.cam.get_image()
img = pygame.transform.flip(img, self.args.flipHorizontal, self.args.flipVertical)
snapName = 'snapshot_' + str(IMGCOUNTER) + '.png'
while op.exists(snapName):
IMGCOUNTER += 1
snapName = 'snapshot_' + str(IMGCOUNTER) + '.png'
# draw the lines if requested
if DRAWCALIB_FLAG or DRAWMEASURE_FLAG:
for o in self.calibLines:
o.drawFunction(img, *o.parameters)
for o in self.measureLines:
o.drawFunction(img, *o.parameters)
pygame.image.save(img, snapName)
print('Saved snapshot as', snapName)
IMGCOUNTER += 1
elif event.button == LEFT:
if self.state == PGLoop.DRAGGING:
self.state = PGLoop.NONE
elif self.state == PGLoop.DRAWCALIB:
pos = vectorSub(pygame.mouse.get_pos(), (XOFFSET, YOFFSET))
distance = math.sqrt( (pos[1]-lineOrig[1])**2 + (pos[0]-lineOrig[0])**2 )
print('pixels: ', distance)
worlddim = None
while worlddim == None:
d = input('Please enter distance in real world: ')
try:
worlddim = float(d)
except ValueError:
print('You MUST enter a valid floating point number!')
self.calibValues.append(CalibValue(distance, worlddim))
self.updateCalibration()
self.state = PGLoop.NONE
# add line to foreground 'til death
line = PGObject(pygame.draw.line, foreground, ((255, 0, 0), lineOrig, pos, 1), True)
self.calibLines.append(line)
elif self.state == PGLoop.DRAWMEASURE:
pos = vectorSub(pygame.mouse.get_pos(), (XOFFSET, YOFFSET))
distance = math.sqrt( (pos[1]-lineOrig[1])**2 + (pos[0]-lineOrig[0])**2 )
self.measuredValues.append(MeasuredValue(distance, distance * self.WORLDSCALE))
print('Measured distance: ', distance * self.WORLDSCALE, ' your unit; average: ',
self.getMeasuredValuesAverage())
self.state = PGLoop.NONE
# add line to foreground 'til death
line = PGObject(pygame.draw.line, foreground, ((0, 0, 255), lineOrig, pos, 1), True)
self.measureLines.append(line)
elif not self.state == PGLoop.DRAGGING and event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
pygame.mouse.get_rel() # init the relative mouse movement
if DRAWCALIB_FLAG:
lineOrig = vectorSub(pygame.mouse.get_pos(), (XOFFSET, YOFFSET))
self.state = PGLoop.DRAWCALIB
elif DRAWMEASURE_FLAG:
lineOrig = vectorSub(pygame.mouse.get_pos(), (XOFFSET, YOFFSET))
self.state = PGLoop.DRAWMEASURE
elif not self.args.scale:
self.state = PGLoop.DRAGGING
elif event.type == pygame.MOUSEMOTION:
if self.state == PGLoop.DRAGGING:
XOFFSET, YOFFSET = vectorAdd((XOFFSET, YOFFSET), pygame.mouse.get_rel())
elif self.state == PGLoop.DRAWCALIB:
# temporary line
foreground.fill((0,0,0,0)) # erase foreground & make it transparent
pygame.draw.line(foreground, (255, 0, 0), lineOrig, vectorSub(pygame.mouse.get_pos(), (XOFFSET, YOFFSET)), 1)
elif self.state == PGLoop.DRAWMEASURE:
# temporary line
foreground.fill((0,0,0,0)) # erase foreground & make it transparent
pygame.draw.line(foreground, (0, 0, 255), lineOrig, vectorSub(pygame.mouse.get_pos(), (XOFFSET, YOFFSET)), 1)
elif event.type == pygame.VIDEORESIZE:
self.args.screen = pygame.display.set_mode(event.dict['size'], HWSURFACE|DOUBLEBUF|RESIZABLE)
self.args.screen.convert()
foreground = pygame.surface.Surface((self.args.screen.get_width(), self.args.screen.get_height()), pygame.SRCALPHA)
foreground.convert()
foreground = foreground.convert_alpha() # faster blitting with transparent color
# calibration is no longer valid:
self.deleteCalibration()
self.grid = []
### video diplay
# to get fastest possible framerate & no flicker (near) all updating should be
# made here
if self.args.cam.query_image():
img = self.args.cam.get_image()
if self.args.scale:
snapshot = pygame.transform.scale(img, (self.args.screen.get_width(), self.args.screen.get_height()))
snapshot = pygame.transform.flip(snapshot, self.args.flipHorizontal, self.args.flipVertical)
snapshot.blit(foreground, (0, 0), special_flags=(pygame.BLEND_RGBA_ADD))
uRect = self.args.screen.blit(snapshot, (0, 0))
else:
img = pygame.transform.flip(img, self.args.flipHorizontal, self.args.flipVertical)
img.blit(foreground, (0, 0), special_flags=(pygame.BLEND_RGBA_ADD))
uRect = self.args.screen.blit(img, (XOFFSET,YOFFSET))
pygame.display.update(uRect)
# draw foreground with all objects, as fast as camera framerate
if self.state != PGLoop.DRAWCALIB and self.state != PGLoop.DRAWMEASURE:
erased = []
# TODO: use itertools
for o in self.calibLines:
if o.surface not in erased:
o.surface.fill((0,0,0,0)) # erase surface & make it transparent
erased.append(o.surface)
o.drawFunction(o.surface, *o.parameters)
for o in self.measureLines:
if o.surface not in erased:
o.surface.fill((0,0,0,0)) # erase surface & make it transparent
erased.append(o.surface)
o.drawFunction(o.surface, *o.parameters)
for o in self.grid:
if o.surface not in erased:
o.surface.fill((0,0,0,0))
erased.append(o.surface)
o.drawFunction(o.surface, *o.parameters)
# does not work:
#pygame.display.set_caption(DEVICE + 'with frame rate: {:0.2f} fps'.format(_clock.get_fps()))
pygame.quit()
sys.exit()
def getMeasuredValuesAverage(self):
if len(self.measuredValues) > 0:
sum = 0
for m in self.measuredValues:
sum += m.result
return sum / len(self.measuredValues)
else:
return 0.0
def updateMeasuredValues(self):
'''Iterates over measured distances and calculates arithmetic mean.'''
sum = 0
for m in self.measuredValues:
m.result = m.pixels * self.WORLDSCALE
sum += m.result
print('New distance: ', m.result)
if len(self.measuredValues) > 0:
print('New average distance:', sum / len(self.measuredValues))
def updateCalibration(self):
'''Iterates over calibration levels and calculates arithmetic mean, then updates measured distances.'''
sum = 0
for c in self.calibValues:
if c.pixels > 0:
sum += c.worlddim / c.pixels
if sum > 0:
self.WORLDSCALE = sum / len(self.calibValues)
else:
self.WORLDSCALE = 1.0
print('New scaling: ', self.WORLDSCALE, ' your unit/pixels')
self.cfg.set('MEASURING', 'worldscale', self.WORLDSCALE)
with open(INIFILE, 'wt') as fp:
self.cfg.write(fp)
self.updateMeasuredValues()
def deleteMeasurements(self):
self.measuredValues = []
self.measureLines = []
def deleteCalibration(self):
self.measuredValues = []
self.calibValues = []
self.WORLDSCALE = 1.0
self.measureLines = []
self.calibLines = []
self.grid = []
def makeGrid(self, _surface, _distance):
grid = []
width = self.args.screen.get_width()
height = self.args.screen.get_height()
# X axis
pos = 0
for lineOrigX in range(1, int(height/_distance) + 3):
curX = int(lineOrigX/self.WORLDSCALE)
o = PGObject(pygame.draw.line, _surface, ((128, 128, 128), (curX, 0), (curX, height), 1), True)
grid.append(o)
# Y axis
pos = 0
for lineOrigY in range(1, int(width/_distance)):
curY = int(lineOrigY/self.WORLDSCALE)
o = PGObject(pygame.draw.line, _surface, ((128, 128, 128), (0, curY), (width, curY), 1), True)
grid.append(o)
return grid
def vectorAdd(_a, _b):
return tuple(map(add, _a, _b))
def vectorSub(_a, _b):
return tuple(map(sub, _a, _b))
def printVideoDevices():
'''Prints a list of available video devices'''
camera.init()
print(camera.list_cameras())
def initCam(_camera='/dev/video0', _res=(640, 480)):
'''Wants the name of the wanted camera device. Returns the device object and the resolution (w,h)'''
camera.init()
cam = camera.Camera(_camera, _res)
if cam is None:
raise Exception('Cannot connect to camera. Maybe in use by other program?')
try:
cam.start()
except:
raise Exception('Cannot connect to camera. Maybe in use by other program?')
return cam
def initPygame(_windowSize=(640, 480)):
pygame.init()
# get display resolution and adjust screen size
vidinfo = pygame.display.Info()
w = min(vidinfo.current_w, _windowSize[0])
h = min(vidinfo.current_h, _windowSize[1])
screen = pygame.display.set_mode((w, h), HWSURFACE|DOUBLEBUF|RESIZABLE)
clock = pygame.time.Clock()
return screen, clock
def makeParser():
argparser = argparse.ArgumentParser(description='''Display camera video with pygame (>= 1.9).
Left mouse button + mouse motion drags video.
Right mouse button takes screenshot, CTRL + right mouse button saves screenshot with drawn lines.
CTRL + left mouse button + mouse motion draws calibration line.
SHIFT + left mouse button + mouse motion draws measuring line.
DEL deletes measured values.
CTRL + DEL deletes calibration (including measured values).
g displays/hides grid.
ESC exits.
Please start this program from command line!''', add_help=False)
argparser.add_argument('-l', '--list', action='store_true', help='Print avaiable video devices')
argparser.add_argument('-s', '--scale', action='store_true', help='Scale video, otherwise it is displayed 1:1 and may be dragged with the mouse')
argparser.add_argument('-m', '--flipHorizontal', action='store_true', help='Flip video horizontal')
argparser.add_argument('-v', '--flipVertical', action='store_true', help='Flip video vertical')
argparser.add_argument('-d', '--device', nargs = '?', default='/dev/video0', help='Name of the camera device')
argparser.add_argument('-w', '--width', nargs = '?', type=int, default=640, help='Width of view')
argparser.add_argument('-h', '--height', nargs = '?', type=int, default=480, help='Height of view')
argparser.add_argument('-?', '--help', action='store_true', help='Print usage information')
return argparser
def main(_args):
global DEVICE
argparser = makeParser()
args = argparser.parse_args(_args)
if args.list:
printVideoDevices()
exit()
elif args.help:
argparser.print_help()
exit()
cam = initCam(args.device, (args.width, args.height))
fps = 30
screen, clock = initPygame(cam.get_size())
print('Using camera', args.device)
print('Using camera resolution', cam.get_size())
print('Using initial screen size (%d, %d)'%(screen.get_width(), screen.get_height()))
if args.scale:
print('Scaling video to screen size')
DEVICE = args.device
pygame.display.set_caption(args.device)
args.cam = cam
args.fps = fps
args.screen = screen
args.clock = clock
cfg = configparser.RawConfigParser()
cfg.read(INIFILE)
if not cfg.has_section('MEASURING'):
cfg.add_section('MEASURING')
if not cfg.has_option('MEASURING', 'worldscale'):
cfg.set('MEASURING', 'worldscale', 1.0)
print('current scale:', cfg.getfloat('MEASURING', 'worldscale'))
# TODO: handle options for screen resolution, camera resolution, ...
loop = PGLoop(args, cfg)
loop()
pygame.quit()
if __name__ == '__main__':
main(sys.argv[1:])