all video processing samples use camera as default source (and fallback to synth in case of capture error)

This commit is contained in:
Alexander Mordvintsev 2011-08-22 13:36:21 +00:00
parent df49158863
commit fc771363d3
8 changed files with 42 additions and 32 deletions

View File

@ -98,7 +98,7 @@ class App(object):
if __name__ == '__main__':
import sys
try: video_src = sys.argv[1]
except: video_src = video.presets['chess']
except: video_src = 0
print help_message
App(video_src).run()

View File

@ -23,8 +23,8 @@ if __name__ == '__main__':
cv2.createTrackbar('scale', 'hist', hist_scale, 32, set_scale)
try: fn = sys.argv[1]
except: fn = 'synth:bg=../cpp/baboon.jpg:class=chess:noise=0.05'
cam = video.create_capture(fn)
except: fn = 0
cam = video.create_capture(fn, fallback='synth:bg=../cpp/baboon.jpg:class=chess:noise=0.05')
while True:
flag, frame = cam.read()

View File

@ -4,7 +4,7 @@ import sys
if __name__ == '__main__':
try: fn = sys.argv[1]
except: fn = video.presets['chess']
except: fn = 0
def nothing(*arg):
pass

View File

@ -25,7 +25,7 @@ if __name__ == '__main__':
args, video_src = getopt.getopt(sys.argv[1:], '', ['cascade=', 'nested-cascade='])
try: video_src = video_src[0]
except: video_src = 'synth:bg=../cpp/lena.jpg:noise=0.05'
except: video_src = 0
args = dict(args)
cascade_fn = args.get('--cascade', "../../data/haarcascades/haarcascade_frontalface_alt.xml")
nested_fn = args.get('--nested-cascade', "../../data/haarcascades/haarcascade_eye.xml")
@ -33,7 +33,7 @@ if __name__ == '__main__':
cascade = cv2.CascadeClassifier(cascade_fn)
nested = cv2.CascadeClassifier(nested_fn)
cam = create_capture(video_src)
cam = create_capture(video_src, fallback='synth:bg=../cpp/lena.jpg:noise=0.05')
while True:
ret, img = cam.read()

View File

@ -77,7 +77,7 @@ class App:
def main():
import sys
try: video_src = sys.argv[1]
except: video_src = video.presets['chess']
except: video_src = 0
print help_message
App(video_src).run()

View File

@ -12,21 +12,21 @@ def draw_motion_comp(vis, (x, y, w, h), angle, color):
cv2.rectangle(vis, (x, y), (x+w, y+h), (0, 255, 0))
r = min(w/2, h/2)
cx, cy = x+w/2, y+h/2
angle = angle*3.1415926/180
angle = angle*np.pi/180
cv2.circle(vis, (cx, cy), r, color, 3)
cv2.line(vis, (cx, cy), (int(cx+np.cos(angle)*r), int(cy+np.sin(angle)*r)), color, 3)
if __name__ == '__main__':
import sys
try: video_src = sys.argv[1]
except: video_src = 'synth:class=chess:bg=../cpp/lena.jpg:noise=0.01'
except: video_src = 0
cv2.namedWindow('motempl')
visuals = ['input', 'frame_diff', 'motion_hist', 'grad_orient']
cv2.createTrackbar('visual', 'motempl', 2, len(visuals)-1, nothing)
cv2.createTrackbar('threshold', 'motempl', DEFAULT_THRESHOLD, 255, nothing)
cam = video.create_capture(video_src)
cam = video.create_capture(video_src, fallback='synth:class=chess:bg=../cpp/lena.jpg:noise=0.01')
ret, frame = cam.read()
h, w = frame.shape[:2]
prev_frame = frame.copy()

View File

@ -47,7 +47,7 @@ if __name__ == '__main__':
import sys
print help_message
try: fn = sys.argv[1]
except: fn = video.presets['chess']
except: fn = 0
cam = video.create_capture(fn)
ret, prev = cam.read()

View File

@ -39,6 +39,9 @@ class VideoSynthBase(object):
buf = cv2.add(buf, noise, dtype=cv2.CV_8UC3)
return True, buf
def isOpened(self):
return True
class Chess(VideoSynthBase):
def __init__(self, **kw):
super(Chess, self).__init__(**kw)
@ -87,34 +90,41 @@ class Chess(VideoSynthBase):
self.draw_quads(dst, self.black_quads, (10, 10, 10))
classes = dict(chess=Chess)
def create_capture(source):
'''
source: <int> or '<int>' or '<filename>' or 'synth:<params>'
'''
try: source = int(source)
except ValueError: pass
else:
return cv2.VideoCapture(source)
source = str(source).strip()
if source.startswith('synth'):
ss = filter(None, source.split(':'))
params = dict( s.split('=') for s in ss[1:] )
try: Class = classes[params['class']]
except: Class = VideoSynthBase
return Class(**params)
return cv2.VideoCapture(source)
presets = dict(
empty = 'synth:',
lena = 'synth:bg=../cpp/lena.jpg:noise=0.1',
chess = 'synth:class=chess:bg=../cpp/lena.jpg:noise=0.1:size=640x480'
)
def create_capture(source = 0, fallback = presets['chess']):
'''
source: <int> or '<int>' or '<filename>' or 'synth:<params>'
'''
cap = None
try: source = int(source)
except ValueError: pass
else:
cap = cv2.VideoCapture(source)
if cap is None:
source = str(source).strip()
if source.startswith('synth'):
ss = filter(None, source.split(':'))
params = dict( s.split('=') for s in ss[1:] )
try: Class = classes[params['class']]
except: Class = VideoSynthBase
try: cap = Class(**params)
except: pass
if cap is None:
cap = cv2.VideoCapture(source)
if not cap.isOpened():
print 'Warning: unable to open video source: ', source
if fallback is not None:
return create_capture(fallback, None)
return cap
if __name__ == '__main__':
import sys
import getopt
@ -127,7 +137,7 @@ if __name__ == '__main__':
args = dict(args)
shotdir = args.get('--shotdir', '.')
if len(sources) == 0:
sources = [ presets['chess'] ]
sources = [ 0 ]
print 'Press SPACE to save current frame'