cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
tst_scene_render.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 # Python 2/3 compatibility
37 from __future__ import print_function
38 
39 import numpy as np
40 import cv2 as cv
41 
42 from numpy import pi, sin, cos
43 
44 
45 defaultSize = 512
46 
48 
49  def __init__(self, bgImg = None, fgImg = None,
50  deformation = False, speed = 0.25, **params):
51  self.timetime = 0.0
52  self.timeSteptimeStep = 1.0 / 30.0
53  self.foregroundforeground = fgImg
54  self.deformationdeformation = deformation
55  self.speedspeed = speed
56 
57  if bgImg is not None:
58  self.sceneBgsceneBg = bgImg.copy()
59  else:
60  self.sceneBgsceneBg = np.zeros(defaultSize, defaultSize, np.uint8)
61 
62  self.ww = self.sceneBgsceneBg.shape[0]
63  self.hh = self.sceneBgsceneBg.shape[1]
64 
65  if fgImg is not None:
66  self.foregroundforeground = fgImg.copy()
67  self.centercenter = self.currentCentercurrentCenter = (int(self.ww/2 - fgImg.shape[0]/2), int(self.hh/2 - fgImg.shape[1]/2))
68 
69  self.xAmplxAmpl = self.sceneBgsceneBg.shape[0] - (self.centercenter[0] + fgImg.shape[0])
70  self.yAmplyAmpl = self.sceneBgsceneBg.shape[1] - (self.centercenter[1] + fgImg.shape[1])
71 
72  self.initialRectinitialRect = np.array([ (self.hh/2, self.ww/2), (self.hh/2, self.ww/2 + self.ww/10),
73  (self.hh/2 + self.hh/10, self.ww/2 + self.ww/10), (self.hh/2 + self.hh/10, self.ww/2)]).astype(int)
74  self.currentRectcurrentRect = self.initialRectinitialRect
75 
76  def getXOffset(self, time):
77  return int( self.xAmplxAmpl*cos(time*self.speedspeed))
78 
79 
80  def getYOffset(self, time):
81  return int(self.yAmplyAmpl*sin(time*self.speedspeed))
82 
83  def setInitialRect(self, rect):
84  self.initialRectinitialRect = rect
85 
86  def getRectInTime(self, time):
87 
88  if self.foregroundforeground is not None:
89  tmp = np.array(self.centercenter) + np.array((self.getXOffsetgetXOffset(time), self.getYOffsetgetYOffset(time)))
90  x0, y0 = tmp
91  x1, y1 = tmp + self.foregroundforeground.shape[0:2]
92  return np.array([y0, x0, y1, x1])
93  else:
94  x0, y0 = self.initialRectinitialRect[0] + np.array((self.getXOffsetgetXOffset(time), self.getYOffsetgetYOffset(time)))
95  x1, y1 = self.initialRectinitialRect[2] + np.array((self.getXOffsetgetXOffset(time), self.getYOffsetgetYOffset(time)))
96  return np.array([y0, x0, y1, x1])
97 
98  def getCurrentRect(self):
99 
100  if self.foregroundforeground is not None:
101 
102  x0 = self.currentCentercurrentCenter[0]
103  y0 = self.currentCentercurrentCenter[1]
104  x1 = self.currentCentercurrentCenter[0] + self.foregroundforeground.shape[0]
105  y1 = self.currentCentercurrentCenter[1] + self.foregroundforeground.shape[1]
106  return np.array([y0, x0, y1, x1])
107  else:
108  x0, y0 = self.currentRectcurrentRect[0]
109  x1, y1 = self.currentRectcurrentRect[2]
110  return np.array([x0, y0, x1, y1])
111 
112  def getNextFrame(self):
113  img = self.sceneBgsceneBg.copy()
114 
115  if self.foregroundforeground is not None:
116  self.currentCentercurrentCenter = (self.centercenter[0] + self.getXOffsetgetXOffset(self.timetime), self.centercenter[1] + self.getYOffsetgetYOffset(self.timetime))
117  img[self.currentCentercurrentCenter[0]:self.currentCentercurrentCenter[0]+self.foregroundforeground.shape[0],
118  self.currentCentercurrentCenter[1]:self.currentCentercurrentCenter[1]+self.foregroundforeground.shape[1]] = self.foregroundforeground
119  else:
120  self.currentRectcurrentRect = self.initialRectinitialRect + np.int( 30*cos(self.timetime*self.speedspeed) + 50*sin(self.timetime*self.speedspeed))
121  if self.deformationdeformation:
122  self.currentRectcurrentRect[1:3] += int(self.hh/20*cos(self.timetime))
123  cv.fillConvexPoly(img, self.currentRectcurrentRect, (0, 0, 255))
124 
125  self.timetime += self.timeSteptimeStep
126  return img
127 
128  def resetTime(self):
129  self.timetime = 0.0
130 
131 
132 def main():
133  backGr = cv.imread(cv.samples.findFile('graf1.png'))
134  fgr = cv.imread(cv.samples.findFile('box.png'))
135 
136  render = TestSceneRender(backGr, fgr)
137 
138  while True:
139 
140  img = render.getNextFrame()
141  cv.imshow('img', img)
142 
143  ch = cv.waitKey(3)
144  if ch == 27:
145  break
146 
147  print('Done')
148 
149 
150 if __name__ == '__main__':
151  print(__doc__)
152  main()
153  cv.destroyAllWindows()
def __init__(self, bgImg=None, fgImg=None, deformation=False, speed=0.25, **params)