cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
video.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # *****************************************************************************
4 # * cloudFPGA
5 # * Copyright 2016 -- 2022 IBM Corporation
6 # * Licensed under the Apache License, Version 2.0 (the "License");
7 # * you may not use this file except in compliance with the License.
8 # * You may obtain a copy of the License at
9 # *
10 # * http://www.apache.org/licenses/LICENSE-2.0
11 # *
12 # * Unless required by applicable law or agreed to in writing, software
13 # * distributed under the License is distributed on an "AS IS" BASIS,
14 # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # * See the License for the specific language governing permissions and
16 # * limitations under the License.
17 # *----------------------------------------------------------------------------
18 
19 # *****************************************************************************
20 # * OpenCV
21 # * Copyright 2021 -- OpenCV team
22 # * Licensed under the Apache License, Version 2.0 (the "License");
23 # * you may not use this file except in compliance with the License.
24 # * You may obtain a copy of the License at
25 # *
26 # * http://www.apache.org/licenses/LICENSE-2.0
27 # *
28 # * Unless required by applicable law or agreed to in writing, software
29 # * distributed under the License is distributed on an "AS IS" BASIS,
30 # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 # * See the License for the specific language governing permissions and
32 # * limitations under the License.
33 # *----------------------------------------------------------------------------
34 
35 '''
36 Video capture sample.
37 
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).
42 
43 'create_capture' is a convenience function for capture creation,
44 falling back to procedural video in case of error.
45 
46 Usage:
47  video.py [--shotdir <shot path>] [source0] [source1] ...'
48 
49  sourceN is an
50  - integer number for camera capture
51  - name of video file
52  - synth:<params> for procedural video
53 
54 Synth examples:
55  synth:bg=lena.jpg:noise=0.1
56  synth:class=chess:bg=lena.jpg:noise=0.1:size=640x480
57 
58 Keys:
59  ESC - exit
60  SPACE - save current frame to <shot path> directory
61 
62 '''
63 
64 # Python 2/3 compatibility
65 from __future__ import print_function
66 
67 import numpy as np
68 import cv2 as cv
69 
70 import re
71 
72 from numpy import pi, sin, cos
73 
74 # local modules
75 from tst_scene_render import TestSceneRender
76 import common
77 
78 class VideoSynthBase(object):
79  def __init__(self, size=None, noise=0.0, bg = None, **params):
80  self.bgbg = None
81  self.frame_sizeframe_size = (640, 480)
82  if bg is not None:
83  self.bgbg = cv.imread(cv.samples.findFile(bg))
84  h, w = self.bgbg.shape[:2]
85  self.frame_sizeframe_size = (w, h)
86 
87  if size is not None:
88  w, h = map(int, size.split('x'))
89  self.frame_sizeframe_size = (w, h)
90  self.bgbg = cv.resize(self.bgbg, self.frame_sizeframe_size)
91 
92  self.noisenoise = float(noise)
93 
94  def render(self, dst):
95  pass
96 
97  def read(self, dst=None):
98  w, h = self.frame_size
99 
100  if self.bg is None:
101  buf = np.zeros((h, w, 3), np.uint8)
102  else:
103  buf = self.bg.copy()
104 
105  self.render(buf)
106 
107  if self.noise > 0.0:
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)
111  return True, buf
112 
113  def isOpened(self):
114  return True
115 
117  def __init__(self, **kw):
118  super(Book, self).__init__(**kw)
119  backGr = cv.imread(cv.samples.findFile('graf1.png'))
120  fgr = cv.imread(cv.samples.findFile('box.png'))
121  self.renderrenderrender = TestSceneRender(backGr, fgr, speed = 1)
122 
123  def read(self, dst=None):
124  noise = np.zeros(self.renderrenderrender.sceneBg.shape, np.int8)
125  cv.randn(noise, np.zeros(3), np.ones(3)*255*self.noisenoise)
126 
127  return True, cv.add(self.renderrenderrender.getNextFrame(), noise, dtype=cv.CV_8UC3)
128 
130  def __init__(self, **kw):
131  super(Cube, self).__init__(**kw)
132  self.renderrenderrender = TestSceneRender(cv.imread(cv.samples.findFile('pca_test1.jpg')), deformation = True, speed = 1)
133 
134  def read(self, dst=None):
135  noise = np.zeros(self.renderrenderrender.sceneBg.shape, np.int8)
136  cv.randn(noise, np.zeros(3), np.ones(3)*255*self.noisenoise)
137 
138  return True, cv.add(self.renderrenderrender.getNextFrame(), noise, dtype=cv.CV_8UC3)
139 
141  def __init__(self, **kw):
142  super(Chess, self).__init__(**kw)
143 
144  w, h = self.frame_sizeframe_size
145 
146  self.grid_sizegrid_size = sx, sy = 10, 7
147  white_quads = []
148  black_quads = []
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)
152  self.white_quadswhite_quads = np.float32(white_quads)
153  self.black_quadsblack_quads = np.float32(black_quads)
154 
155  fx = 0.9
156  self.KK = np.float64([[fx*w, 0, 0.5*(w-1)],
157  [0, fx*w, 0.5*(h-1)],
158  [0.0,0.0, 1.0]])
159 
160  self.dist_coefdist_coef = np.float64([-0.2, 0.1, 0, 0])
161  self.tt = 0
162 
163  def draw_quads(self, img, quads, color = (0, 255, 0)):
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,)
166  for q in img_quads:
167  cv.fillConvexPoly(img, np.int32(q*4), color, cv.LINE_AA, shift=2)
168 
169  def render(self, dst):
170  t = self.tt
171  self.tt += 1.0/30.0
172 
173  sx, sy = self.grid_sizegrid_size
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
180 
181  R, self.tvectvec = common.lookat(eye_pos, target_pos)
182  self.rvecrvec = common.mtx2rvec(R)
183 
184  self.draw_quadsdraw_quads(dst, self.white_quadswhite_quads, (245, 245, 245))
185  self.draw_quadsdraw_quads(dst, self.black_quadsblack_quads, (10, 10, 10))
186 
187 
188 classes = dict(chess=Chess, book=Book, cube=Cube)
189 
190 presets = dict(
191  empty = 'synth:',
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'
196 )
197 
198 
199 def create_capture(source = 0, fallback = presets['chess']):
200  '''source: <int> or '<int>|<filename>|synth [:<param_name>=<value> [:...]]'
201  '''
202  source = str(source).strip()
203 
204  # Win32: handle drive letter ('c:', ...)
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]
208 
209  source = chunks[0]
210  try: source = int(source)
211  except ValueError: pass
212  params = dict( s.split('=') for s in chunks[1:] )
213 
214  cap = None
215  if source == 'synth':
216  Class = classes.get(params.get('class', None), VideoSynthBase)
217  try: cap = Class(**params)
218  except: pass
219  else:
220  cap = cv.VideoCapture(source)
221  if 'size' in params:
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:
228  return create_capture(fallback, None)
229  return cap
230 
231 if __name__ == '__main__':
232  import sys
233  import getopt
234 
235  print(__doc__)
236 
237  args, sources = getopt.getopt(sys.argv[1:], '', 'shotdir=')
238  args = dict(args)
239  shotdir = args.get('--shotdir', '.')
240  if len(sources) == 0:
241  sources = [ 0 ]
242 
243  caps = list(map(create_capture, sources))
244  shot_idx = 0
245  while True:
246  imgs = []
247  for i, cap in enumerate(caps):
248  ret, img = cap.read()
249  imgs.append(img)
250  cv.imshow('capture %d' % i, img)
251  ch = cv.waitKey(1)
252  if ch == 27:
253  break
254  if ch == ord(' '):
255  for i, img in enumerate(imgs):
256  fn = '%s/shot_%d_%03d.bmp' % (shotdir, i, shot_idx)
257  cv.imwrite(fn, img)
258  print(fn, 'saved')
259  shot_idx += 1
260  cv.destroyAllWindows()
def read(self, dst=None)
Definition: video.py:123
def __init__(self, **kw)
Definition: video.py:117
def draw_quads(self, img, quads, color=(0, 255, 0))
Definition: video.py:163
def render(self, dst)
Definition: video.py:169
def __init__(self, **kw)
Definition: video.py:141
def __init__(self, **kw)
Definition: video.py:130
def read(self, dst=None)
Definition: video.py:134
def isOpened(self)
Definition: video.py:113
def render(self, dst)
Definition: video.py:94
def read(self, dst=None)
Definition: video.py:97
def __init__(self, size=None, noise=0.0, bg=None, **params)
Definition: video.py:79
def create_capture(source=0, fallback=presets['chess'])
Definition: video.py:199