Replace of some synthetic scenes in python samples
This commit is contained in:
@@ -35,11 +35,12 @@ import cv2
|
|||||||
|
|
||||||
# local module
|
# local module
|
||||||
import video
|
import video
|
||||||
|
from video import presets
|
||||||
|
|
||||||
|
|
||||||
class App(object):
|
class App(object):
|
||||||
def __init__(self, video_src):
|
def __init__(self, video_src):
|
||||||
self.cam = video.create_capture(video_src)
|
self.cam = video.create_capture(video_src, presets['cube'])
|
||||||
ret, self.frame = self.cam.read()
|
ret, self.frame = self.cam.read()
|
||||||
cv2.namedWindow('camshift')
|
cv2.namedWindow('camshift')
|
||||||
cv2.setMouseCallback('camshift', self.onmouse)
|
cv2.setMouseCallback('camshift', self.onmouse)
|
||||||
|
@@ -30,6 +30,7 @@ import cv2
|
|||||||
|
|
||||||
# local modules
|
# local modules
|
||||||
import video
|
import video
|
||||||
|
from video import presets
|
||||||
import common
|
import common
|
||||||
from common import getsize, draw_keypoints
|
from common import getsize, draw_keypoints
|
||||||
from plane_tracker import PlaneTracker
|
from plane_tracker import PlaneTracker
|
||||||
@@ -37,7 +38,7 @@ from plane_tracker import PlaneTracker
|
|||||||
|
|
||||||
class App:
|
class App:
|
||||||
def __init__(self, src):
|
def __init__(self, src):
|
||||||
self.cap = video.create_capture(src)
|
self.cap = video.create_capture(src, presets['book'])
|
||||||
self.frame = None
|
self.frame = None
|
||||||
self.paused = False
|
self.paused = False
|
||||||
self.tracker = PlaneTracker()
|
self.tracker = PlaneTracker()
|
||||||
|
@@ -27,6 +27,7 @@ import numpy as np
|
|||||||
import cv2
|
import cv2
|
||||||
import video
|
import video
|
||||||
from common import draw_str
|
from common import draw_str
|
||||||
|
from video import presets
|
||||||
|
|
||||||
lk_params = dict( winSize = (19, 19),
|
lk_params = dict( winSize = (19, 19),
|
||||||
maxLevel = 2,
|
maxLevel = 2,
|
||||||
@@ -49,7 +50,7 @@ red = (0, 0, 255)
|
|||||||
|
|
||||||
class App:
|
class App:
|
||||||
def __init__(self, video_src):
|
def __init__(self, video_src):
|
||||||
self.cam = video.create_capture(video_src)
|
self.cam = self.cam = video.create_capture(video_src, presets['book'])
|
||||||
self.p0 = None
|
self.p0 = None
|
||||||
self.use_ransac = True
|
self.use_ransac = True
|
||||||
|
|
||||||
|
@@ -30,7 +30,7 @@ import cv2
|
|||||||
import video
|
import video
|
||||||
import common
|
import common
|
||||||
from plane_tracker import PlaneTracker
|
from plane_tracker import PlaneTracker
|
||||||
|
from video import presets
|
||||||
|
|
||||||
ar_verts = np.float32([[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0],
|
ar_verts = np.float32([[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0],
|
||||||
[0, 0, 1], [0, 1, 1], [1, 1, 1], [1, 0, 1],
|
[0, 0, 1], [0, 1, 1], [1, 1, 1], [1, 0, 1],
|
||||||
@@ -42,7 +42,7 @@ ar_edges = [(0, 1), (1, 2), (2, 3), (3, 0),
|
|||||||
|
|
||||||
class App:
|
class App:
|
||||||
def __init__(self, src):
|
def __init__(self, src):
|
||||||
self.cap = video.create_capture(src)
|
self.cap = video.create_capture(src, presets['book'])
|
||||||
self.frame = None
|
self.frame = None
|
||||||
self.paused = False
|
self.paused = False
|
||||||
self.tracker = PlaneTracker()
|
self.tracker = PlaneTracker()
|
||||||
|
@@ -38,6 +38,7 @@ from collections import namedtuple
|
|||||||
# local modules
|
# local modules
|
||||||
import video
|
import video
|
||||||
import common
|
import common
|
||||||
|
from video import presets
|
||||||
|
|
||||||
|
|
||||||
FLANN_INDEX_KDTREE = 1
|
FLANN_INDEX_KDTREE = 1
|
||||||
@@ -139,7 +140,7 @@ class PlaneTracker:
|
|||||||
|
|
||||||
class App:
|
class App:
|
||||||
def __init__(self, src):
|
def __init__(self, src):
|
||||||
self.cap = video.create_capture(src)
|
self.cap = video.create_capture(src, presets['book'])
|
||||||
self.frame = None
|
self.frame = None
|
||||||
self.paused = False
|
self.paused = False
|
||||||
self.tracker = PlaneTracker()
|
self.tracker = PlaneTracker()
|
||||||
|
116
samples/python/tst_scene_render.py
Normal file
116
samples/python/tst_scene_render.py
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
|
||||||
|
# Python 2/3 compatibility
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from numpy import pi, sin, cos
|
||||||
|
|
||||||
|
import cv2
|
||||||
|
|
||||||
|
defaultSize = 512
|
||||||
|
|
||||||
|
class TestSceneRender():
|
||||||
|
|
||||||
|
def __init__(self, bgImg = None, fgImg = None,
|
||||||
|
deformation = False, speed = 0.25, **params):
|
||||||
|
self.time = 0.0
|
||||||
|
self.timeStep = 1.0 / 30.0
|
||||||
|
self.foreground = fgImg
|
||||||
|
self.deformation = deformation
|
||||||
|
self.speed = speed
|
||||||
|
|
||||||
|
if bgImg != None:
|
||||||
|
self.sceneBg = bgImg.copy()
|
||||||
|
else:
|
||||||
|
self.sceneBg = np.zeros(defaultSize, defaultSize, np.uint8)
|
||||||
|
|
||||||
|
self.w = self.sceneBg.shape[0]
|
||||||
|
self.h = self.sceneBg.shape[1]
|
||||||
|
|
||||||
|
if fgImg != None:
|
||||||
|
self.foreground = fgImg.copy()
|
||||||
|
self.center = self.currentCenter = (int(self.w/2 - fgImg.shape[0]/2), int(self.h/2 - fgImg.shape[1]/2))
|
||||||
|
|
||||||
|
self.xAmpl = self.sceneBg.shape[0] - (self.center[0] + fgImg.shape[0])
|
||||||
|
self.yAmpl = self.sceneBg.shape[1] - (self.center[1] + fgImg.shape[1])
|
||||||
|
|
||||||
|
self.initialRect = np.array([ (self.h/2, self.w/2), (self.h/2, self.w/2 + self.w/10),
|
||||||
|
(self.h/2 + self.h/10, self.w/2 + self.w/10), (self.h/2 + self.h/10, self.w/2)]).astype(int)
|
||||||
|
self.currentRect = self.initialRect
|
||||||
|
|
||||||
|
def getXOffset(self, time):
|
||||||
|
return int( self.xAmpl*cos(time*self.speed))
|
||||||
|
|
||||||
|
|
||||||
|
def getYOffset(self, time):
|
||||||
|
return int(self.yAmpl*sin(time*self.speed))
|
||||||
|
|
||||||
|
def setInitialRect(self, rect):
|
||||||
|
self.initialRect = rect
|
||||||
|
|
||||||
|
def getRectInTime(self, time):
|
||||||
|
|
||||||
|
if self.foreground != None:
|
||||||
|
tmp = np.array(self.center) + np.array((self.getXOffset(time), self.getYOffset(time)))
|
||||||
|
x0, y0 = tmp
|
||||||
|
x1, y1 = tmp + self.foreground.shape[0:2]
|
||||||
|
return np.array([y0, x0, y1, x1])
|
||||||
|
else:
|
||||||
|
x0, y0 = self.initialRect[0] + np.array((self.getXOffset(time), self.getYOffset(time)))
|
||||||
|
x1, y1 = self.initialRect[2] + np.array((self.getXOffset(time), self.getYOffset(time)))
|
||||||
|
return np.array([y0, x0, y1, x1])
|
||||||
|
|
||||||
|
def getCurrentRect(self):
|
||||||
|
|
||||||
|
if self.foreground != None:
|
||||||
|
|
||||||
|
x0 = self.currentCenter[0]
|
||||||
|
y0 = self.currentCenter[1]
|
||||||
|
x1 = self.currentCenter[0] + self.foreground.shape[0]
|
||||||
|
y1 = self.currentCenter[1] + self.foreground.shape[1]
|
||||||
|
return np.array([y0, x0, y1, x1])
|
||||||
|
else:
|
||||||
|
x0, y0 = self.currentRect[0]
|
||||||
|
x1, y1 = self.currentRect[2]
|
||||||
|
return np.array([x0, y0, x1, y1])
|
||||||
|
|
||||||
|
def getNextFrame(self):
|
||||||
|
img = self.sceneBg.copy()
|
||||||
|
|
||||||
|
if self.foreground != None:
|
||||||
|
self.currentCenter = (self.center[0] + self.getXOffset(self.time), self.center[1] + self.getYOffset(self.time))
|
||||||
|
img[self.currentCenter[0]:self.currentCenter[0]+self.foreground.shape[0],
|
||||||
|
self.currentCenter[1]:self.currentCenter[1]+self.foreground.shape[1]] = self.foreground
|
||||||
|
else:
|
||||||
|
self.currentRect = self.initialRect + np.int( 30*cos(self.time*self.speed) + 50*sin(self.time*self.speed))
|
||||||
|
if self.deformation:
|
||||||
|
self.currentRect[1:3] += self.h/20*cos(self.time)
|
||||||
|
cv2.fillConvexPoly(img, self.currentRect, (0, 0, 255))
|
||||||
|
|
||||||
|
self.time += self.timeStep
|
||||||
|
return img
|
||||||
|
|
||||||
|
def resetTime(self):
|
||||||
|
self.time = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
backGr = cv2.imread('../data/graf1.png')
|
||||||
|
fgr = cv2.imread('../data/box.png')
|
||||||
|
|
||||||
|
render = TestSceneRender(backGr, fgr)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
|
img = render.getNextFrame()
|
||||||
|
cv2.imshow('img', img)
|
||||||
|
|
||||||
|
ch = 0xFF & cv2.waitKey(3)
|
||||||
|
if ch == 27:
|
||||||
|
break
|
||||||
|
#import os
|
||||||
|
#print (os.environ['PYTHONPATH'])
|
||||||
|
cv2.destroyAllWindows()
|
@@ -41,6 +41,7 @@ import cv2
|
|||||||
from time import clock
|
from time import clock
|
||||||
|
|
||||||
# local modules
|
# local modules
|
||||||
|
from tst_scene_render import TestSceneRender
|
||||||
import common
|
import common
|
||||||
|
|
||||||
class VideoSynthBase(object):
|
class VideoSynthBase(object):
|
||||||
@@ -81,6 +82,30 @@ class VideoSynthBase(object):
|
|||||||
def isOpened(self):
|
def isOpened(self):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
class Book(VideoSynthBase):
|
||||||
|
def __init__(self, **kw):
|
||||||
|
super(Book, self).__init__(**kw)
|
||||||
|
backGr = cv2.imread('../data/graf1.png')
|
||||||
|
fgr = cv2.imread('../data/box.png')
|
||||||
|
self.render = TestSceneRender(backGr, fgr, speed = 1)
|
||||||
|
|
||||||
|
def read(self, dst=None):
|
||||||
|
noise = np.zeros(self.render.sceneBg.shape, np.int8)
|
||||||
|
cv2.randn(noise, np.zeros(3), np.ones(3)*255*self.noise)
|
||||||
|
|
||||||
|
return True, cv2.add(self.render.getNextFrame(), noise, dtype=cv2.CV_8UC3)
|
||||||
|
|
||||||
|
class Cube(VideoSynthBase):
|
||||||
|
def __init__(self, **kw):
|
||||||
|
super(Cube, self).__init__(**kw)
|
||||||
|
self.render = TestSceneRender(cv2.imread('../data/pca_test1.jpg'), deformation = True, speed = 1)
|
||||||
|
|
||||||
|
def read(self, dst=None):
|
||||||
|
noise = np.zeros(self.render.sceneBg.shape, np.int8)
|
||||||
|
cv2.randn(noise, np.zeros(3), np.ones(3)*255*self.noise)
|
||||||
|
|
||||||
|
return True, cv2.add(self.render.getNextFrame(), noise, dtype=cv2.CV_8UC3)
|
||||||
|
|
||||||
class Chess(VideoSynthBase):
|
class Chess(VideoSynthBase):
|
||||||
def __init__(self, **kw):
|
def __init__(self, **kw):
|
||||||
super(Chess, self).__init__(**kw)
|
super(Chess, self).__init__(**kw)
|
||||||
@@ -129,12 +154,14 @@ 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, book=Book, cube=Cube)
|
||||||
|
|
||||||
presets = dict(
|
presets = dict(
|
||||||
empty = 'synth:',
|
empty = 'synth:',
|
||||||
lena = 'synth:bg=../data/lena.jpg:noise=0.1',
|
lena = 'synth:bg=../data/lena.jpg:noise=0.1',
|
||||||
chess = 'synth:class=chess:bg=../data/lena.jpg:noise=0.1:size=640x480'
|
chess = 'synth:class=chess:bg=../data/lena.jpg:noise=0.1:size=640x480',
|
||||||
|
book = 'synth:class=book:bg=../data/graf1.png:noise=0.1:size=640x480',
|
||||||
|
cube = 'synth:class=cube:bg=../data/pca_test1.jpg:noise=0.0:size=640x480'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user