From fc771363d33bc5d68d2649a723c583a5c43a740c Mon Sep 17 00:00:00 2001 From: Alexander Mordvintsev Date: Mon, 22 Aug 2011 13:36:21 +0000 Subject: [PATCH] all video processing samples use camera as default source (and fallback to synth in case of capture error) --- samples/python2/camshift.py | 2 +- samples/python2/color_histogram.py | 4 +-- samples/python2/edge.py | 2 +- samples/python2/facedetect.py | 4 +-- samples/python2/lk_track.py | 2 +- samples/python2/motempl.py | 6 ++-- samples/python2/opt_flow.py | 2 +- samples/python2/video.py | 52 ++++++++++++++++++------------ 8 files changed, 42 insertions(+), 32 deletions(-) diff --git a/samples/python2/camshift.py b/samples/python2/camshift.py index 79b4c29510..1b4a678bd5 100644 --- a/samples/python2/camshift.py +++ b/samples/python2/camshift.py @@ -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() diff --git a/samples/python2/color_histogram.py b/samples/python2/color_histogram.py index 7038dc6f8c..f2deb84965 100644 --- a/samples/python2/color_histogram.py +++ b/samples/python2/color_histogram.py @@ -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() diff --git a/samples/python2/edge.py b/samples/python2/edge.py index cd5d216794..4fa43d83f3 100644 --- a/samples/python2/edge.py +++ b/samples/python2/edge.py @@ -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 diff --git a/samples/python2/facedetect.py b/samples/python2/facedetect.py index 5fe9951295..20bce3da14 100644 --- a/samples/python2/facedetect.py +++ b/samples/python2/facedetect.py @@ -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() diff --git a/samples/python2/lk_track.py b/samples/python2/lk_track.py index 8a157e1787..10e396150b 100644 --- a/samples/python2/lk_track.py +++ b/samples/python2/lk_track.py @@ -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() diff --git a/samples/python2/motempl.py b/samples/python2/motempl.py index a333fcd104..882649fc78 100644 --- a/samples/python2/motempl.py +++ b/samples/python2/motempl.py @@ -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() diff --git a/samples/python2/opt_flow.py b/samples/python2/opt_flow.py index 03cdb3f1cb..a22adb2b01 100644 --- a/samples/python2/opt_flow.py +++ b/samples/python2/opt_flow.py @@ -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() diff --git a/samples/python2/video.py b/samples/python2/video.py index d18ee82d07..80e1282b79 100644 --- a/samples/python2/video.py +++ b/samples/python2/video.py @@ -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: or '' or '' or 'synth:' - ''' - 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: or '' or '' or 'synth:' + ''' + 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'