Commit fc771363 authored by Alexander Mordvintsev's avatar Alexander Mordvintsev

all video processing samples use camera as default source (and fallback to synth…

all video processing samples use camera as default source (and fallback to synth in case of capture error)
parent df491588
...@@ -98,7 +98,7 @@ class App(object): ...@@ -98,7 +98,7 @@ class App(object):
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
try: video_src = sys.argv[1] try: video_src = sys.argv[1]
except: video_src = video.presets['chess'] except: video_src = 0
print help_message print help_message
App(video_src).run() App(video_src).run()
...@@ -23,8 +23,8 @@ if __name__ == '__main__': ...@@ -23,8 +23,8 @@ if __name__ == '__main__':
cv2.createTrackbar('scale', 'hist', hist_scale, 32, set_scale) cv2.createTrackbar('scale', 'hist', hist_scale, 32, set_scale)
try: fn = sys.argv[1] try: fn = sys.argv[1]
except: fn = 'synth:bg=../cpp/baboon.jpg:class=chess:noise=0.05' except: fn = 0
cam = video.create_capture(fn) cam = video.create_capture(fn, fallback='synth:bg=../cpp/baboon.jpg:class=chess:noise=0.05')
while True: while True:
flag, frame = cam.read() flag, frame = cam.read()
......
...@@ -4,7 +4,7 @@ import sys ...@@ -4,7 +4,7 @@ import sys
if __name__ == '__main__': if __name__ == '__main__':
try: fn = sys.argv[1] try: fn = sys.argv[1]
except: fn = video.presets['chess'] except: fn = 0
def nothing(*arg): def nothing(*arg):
pass pass
......
...@@ -25,7 +25,7 @@ if __name__ == '__main__': ...@@ -25,7 +25,7 @@ if __name__ == '__main__':
args, video_src = getopt.getopt(sys.argv[1:], '', ['cascade=', 'nested-cascade=']) args, video_src = getopt.getopt(sys.argv[1:], '', ['cascade=', 'nested-cascade='])
try: video_src = video_src[0] try: video_src = video_src[0]
except: video_src = 'synth:bg=../cpp/lena.jpg:noise=0.05' except: video_src = 0
args = dict(args) args = dict(args)
cascade_fn = args.get('--cascade', "../../data/haarcascades/haarcascade_frontalface_alt.xml") cascade_fn = args.get('--cascade', "../../data/haarcascades/haarcascade_frontalface_alt.xml")
nested_fn = args.get('--nested-cascade', "../../data/haarcascades/haarcascade_eye.xml") nested_fn = args.get('--nested-cascade', "../../data/haarcascades/haarcascade_eye.xml")
...@@ -33,7 +33,7 @@ if __name__ == '__main__': ...@@ -33,7 +33,7 @@ if __name__ == '__main__':
cascade = cv2.CascadeClassifier(cascade_fn) cascade = cv2.CascadeClassifier(cascade_fn)
nested = cv2.CascadeClassifier(nested_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: while True:
ret, img = cam.read() ret, img = cam.read()
......
...@@ -77,7 +77,7 @@ class App: ...@@ -77,7 +77,7 @@ class App:
def main(): def main():
import sys import sys
try: video_src = sys.argv[1] try: video_src = sys.argv[1]
except: video_src = video.presets['chess'] except: video_src = 0
print help_message print help_message
App(video_src).run() App(video_src).run()
......
...@@ -12,21 +12,21 @@ def draw_motion_comp(vis, (x, y, w, h), angle, color): ...@@ -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)) cv2.rectangle(vis, (x, y), (x+w, y+h), (0, 255, 0))
r = min(w/2, h/2) r = min(w/2, h/2)
cx, cy = x+w/2, y+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.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) cv2.line(vis, (cx, cy), (int(cx+np.cos(angle)*r), int(cy+np.sin(angle)*r)), color, 3)
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
try: video_src = sys.argv[1] 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') cv2.namedWindow('motempl')
visuals = ['input', 'frame_diff', 'motion_hist', 'grad_orient'] visuals = ['input', 'frame_diff', 'motion_hist', 'grad_orient']
cv2.createTrackbar('visual', 'motempl', 2, len(visuals)-1, nothing) cv2.createTrackbar('visual', 'motempl', 2, len(visuals)-1, nothing)
cv2.createTrackbar('threshold', 'motempl', DEFAULT_THRESHOLD, 255, 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() ret, frame = cam.read()
h, w = frame.shape[:2] h, w = frame.shape[:2]
prev_frame = frame.copy() prev_frame = frame.copy()
......
...@@ -47,7 +47,7 @@ if __name__ == '__main__': ...@@ -47,7 +47,7 @@ if __name__ == '__main__':
import sys import sys
print help_message print help_message
try: fn = sys.argv[1] try: fn = sys.argv[1]
except: fn = video.presets['chess'] except: fn = 0
cam = video.create_capture(fn) cam = video.create_capture(fn)
ret, prev = cam.read() ret, prev = cam.read()
......
...@@ -39,6 +39,9 @@ class VideoSynthBase(object): ...@@ -39,6 +39,9 @@ class VideoSynthBase(object):
buf = cv2.add(buf, noise, dtype=cv2.CV_8UC3) buf = cv2.add(buf, noise, dtype=cv2.CV_8UC3)
return True, buf return True, buf
def isOpened(self):
return True
class Chess(VideoSynthBase): class Chess(VideoSynthBase):
def __init__(self, **kw): def __init__(self, **kw):
super(Chess, self).__init__(**kw) super(Chess, self).__init__(**kw)
...@@ -87,33 +90,40 @@ class Chess(VideoSynthBase): ...@@ -87,33 +90,40 @@ class Chess(VideoSynthBase):
self.draw_quads(dst, self.black_quads, (10, 10, 10)) self.draw_quads(dst, self.black_quads, (10, 10, 10))
classes = dict(chess=Chess) classes = dict(chess=Chess)
def create_capture(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>' source: <int> or '<int>' or '<filename>' or 'synth:<params>'
''' '''
cap = None
try: source = int(source) try: source = int(source)
except ValueError: pass except ValueError: pass
else: else:
return cv2.VideoCapture(source) cap = cv2.VideoCapture(source)
source = str(source).strip() if cap is None:
if source.startswith('synth'): source = str(source).strip()
ss = filter(None, source.split(':')) if source.startswith('synth'):
params = dict( s.split('=') for s in ss[1:] ) ss = filter(None, source.split(':'))
try: Class = classes[params['class']] params = dict( s.split('=') for s in ss[1:] )
except: Class = VideoSynthBase try: Class = classes[params['class']]
except: Class = VideoSynthBase
return Class(**params) try: cap = Class(**params)
return cv2.VideoCapture(source) except: pass
if cap is None:
cap = cv2.VideoCapture(source)
presets = dict( if not cap.isOpened():
empty = 'synth:', print 'Warning: unable to open video source: ', source
lena = 'synth:bg=../cpp/lena.jpg:noise=0.1', if fallback is not None:
chess = 'synth:class=chess:bg=../cpp/lena.jpg:noise=0.1:size=640x480' return create_capture(fallback, None)
) return cap
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
...@@ -127,7 +137,7 @@ if __name__ == '__main__': ...@@ -127,7 +137,7 @@ if __name__ == '__main__':
args = dict(args) args = dict(args)
shotdir = args.get('--shotdir', '.') shotdir = args.get('--shotdir', '.')
if len(sources) == 0: if len(sources) == 0:
sources = [ presets['chess'] ] sources = [ 0 ]
print 'Press SPACE to save current frame' print 'Press SPACE to save current frame'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment