38 Sample shows how VideoCapture class can be used to acquire video
39 frames from a camera of a movie file. Also the sample provides
40 an example of procedural video generation by an object, mimicking
41 the VideoCapture interface (see Chess class).
43 'create_capture' is a convenience function for capture creation,
44 falling back to procedural video in case of error.
47 video.py [--shotdir <shot path>] [source0] [source1] ...'
50 - integer number for camera capture
52 - synth:<params> for procedural video
55 synth:bg=lena.jpg:noise=0.1
56 synth:class=chess:bg=lena.jpg:noise=0.1:size=640x480
60 SPACE - save current frame to <shot path> directory
65 from __future__
import print_function
72 from numpy
import pi, sin, cos
75 from tst_scene_render
import TestSceneRender
79 def __init__(self, size=None, noise=0.0, bg = None, **params):
83 self.
bgbg = cv.imread(cv.samples.findFile(bg))
84 h, w = self.
bgbg.shape[:2]
88 w, h = map(int, size.split(
'x'))
98 w, h = self.frame_size
101 buf = np.zeros((h, w, 3), np.uint8)
108 noise = np.zeros((h, w, 3), np.int8)
109 cv.randn(noise, np.zeros(3), np.ones(3)*255*self.noise)
110 buf = cv.add(buf, noise, dtype=cv.CV_8UC3)
119 backGr = cv.imread(cv.samples.findFile(
'graf1.png'))
120 fgr = cv.imread(cv.samples.findFile(
'box.png'))
124 noise = np.zeros(self.
renderrenderrender.sceneBg.shape, np.int8)
125 cv.randn(noise, np.zeros(3), np.ones(3)*255*self.
noisenoise)
127 return True, cv.add(self.
renderrenderrender.getNextFrame(), noise, dtype=cv.CV_8UC3)
135 noise = np.zeros(self.
renderrenderrender.sceneBg.shape, np.int8)
136 cv.randn(noise, np.zeros(3), np.ones(3)*255*self.
noisenoise)
138 return True, cv.add(self.
renderrenderrender.getNextFrame(), noise, dtype=cv.CV_8UC3)
149 for i, j
in np.ndindex(sy, sx):
150 q = [[j, i, 0], [j+1, i, 0], [j+1, i+1, 0], [j, i+1, 0]]
151 [white_quads, black_quads][(i + j) % 2].append(q)
156 self.
KK = np.float64([[fx*w, 0, 0.5*(w-1)],
157 [0, fx*w, 0.5*(h-1)],
164 img_quads = cv.projectPoints(quads.reshape(-1, 3), self.
rvecrvec, self.
tvectvec, self.
KK, self.
dist_coefdist_coef) [0]
165 img_quads.shape = quads.shape[:2] + (2,)
167 cv.fillConvexPoly(img, np.int32(q*4), color, cv.LINE_AA, shift=2)
174 center = np.array([0.5*sx, 0.5*sy, 0.0])
175 phi = pi/3 + sin(t*3)*pi/8
176 c, s = cos(phi), sin(phi)
177 ofs = np.array([sin(1.2*t), cos(1.8*t), 0]) * sx * 0.2
178 eye_pos = center + np.array([cos(t)*c, sin(t)*c, s]) * 15.0 + ofs
179 target_pos = center + ofs
181 R, self.
tvectvec = common.lookat(eye_pos, target_pos)
182 self.
rvecrvec = common.mtx2rvec(R)
188 classes = dict(chess=Chess, book=Book, cube=Cube)
192 lena =
'synth:bg=lena.jpg:noise=0.1',
193 chess =
'synth:class=chess:bg=lena.jpg:noise=0.1:size=640x480',
194 book =
'synth:class=book:bg=graf1.png:noise=0.1:size=640x480',
195 cube =
'synth:class=cube:bg=pca_test1.jpg:noise=0.0:size=640x480'
200 '''source: <int> or '<int>|<filename>|synth [:<param_name>=<value> [:...]]'
202 source =
str(source).strip()
205 source = re.sub(
r'(^|=)([a-zA-Z]):([/\\a-zA-Z0-9])',
r'\1?disk\2?\3', source)
206 chunks = source.split(
':')
207 chunks = [re.sub(
r'\?disk([a-zA-Z])\?',
r'\1:', s)
for s
in chunks]
210 try: source =
int(source)
211 except ValueError:
pass
212 params = dict( s.split(
'=')
for s
in chunks[1:] )
215 if source ==
'synth':
216 Class = classes.get(params.get(
'class',
None), VideoSynthBase)
217 try: cap = Class(**params)
220 cap = cv.VideoCapture(source)
222 w, h = map(int, params[
'size'].
split(
'x'))
223 cap.set(cv.CAP_PROP_FRAME_WIDTH, w)
224 cap.set(cv.CAP_PROP_FRAME_HEIGHT, h)
225 if cap
is None or not cap.isOpened():
226 print(
'Warning: unable to open video source: ', source)
227 if fallback
is not None:
231 if __name__ ==
'__main__':
237 args, sources = getopt.getopt(sys.argv[1:],
'',
'shotdir=')
239 shotdir = args.get(
'--shotdir',
'.')
240 if len(sources) == 0:
243 caps = list(map(create_capture, sources))
247 for i, cap
in enumerate(caps):
248 ret, img = cap.read()
250 cv.imshow(
'capture %d' % i, img)
255 for i, img
in enumerate(imgs):
256 fn =
'%s/shot_%d_%03d.bmp' % (shotdir, i, shot_idx)
260 cv.destroyAllWindows()
def draw_quads(self, img, quads, color=(0, 255, 0))
def __init__(self, size=None, noise=0.0, bg=None, **params)
def create_capture(source=0, fallback=presets['chess'])