# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ############################################################################### print("Running testXfBitwise_and.py ...") print("Loading overlay") from pynq import Overlay bs = Overlay( "/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2bitwise_and.bit" ) bs.download() print("Loading xlnk") from pynq import Xlnk Xlnk.set_allocator_library( '/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2bitwise_and.so' ) mem_manager = Xlnk() import pynq_cv.overlays.xv2bitwise_and as xv2 import numpy as np import cv2 import time import OpenCVUtils as cvu print("Loading image ../images/bigBunny_1080.png") img1 = cv2.imread('../images/bigBunny_1080.png') imgY1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) print("Size of imgY1 is ", imgY1.shape)
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ############################################################################### print("Running testXfFilter2D.py ...") print("Loading overlay") from pynq import Overlay bs = Overlay( "/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2filter2D.bit") bs.download() print("Loading xlnk") from pynq import Xlnk Xlnk.set_allocator_library( '/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2filter2D.so') mem_manager = Xlnk() import pynq_cv.overlays.xv2filter2D as xv2 import numpy as np import cv2 import time import OpenCVUtils as cvu print("Loading image ../images/bigBunny_1080.png") img = cv2.imread('../images/bigBunny_1080.png') imgY = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) print("Size of imgY is ", imgY.shape) height, width, channels = img.shape
''' print(spaces) print('Loading new PYNQ Overlay: xv2Filter2DDilate') # Load filter2D + dilate overlay from pynq import Overlay bareHDMI = Overlay("/usr/local/lib/python3.6/dist-packages/" "pynq_cv/overlays/xv2Filter2DDilate.bit") import pynq_cv.overlays.xv2Filter2DDilate as xv2 # Load xlnk memory mangager from pynq import Xlnk Xlnk.set_allocator_library("/usr/local/lib/python3.6/dist-packages/" "pynq_cv/overlays/xv2Filter2DDilate.so") mem_manager = Xlnk() print('Loaded 2D Filter Overlay') print('Requesting video input') hdmi_in = bareHDMI.video.hdmi_in hdmi_out = bareHDMI.video.hdmi_out from pynq.lib.video import * hdmi_in.configure(PIXEL_GRAY) hdmi_out.configure(hdmi_in.mode) hdmi_in.cacheable_frames = False
# OR BUSINESS INTERRUPTION). HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ############################################################################### print("Running testXfEqualizeHist.py ...") print("Loading overlay") from pynq import Overlay bs = Overlay("/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2equalizeHist.bit") bs.download() print("Loading xlnk") from pynq import Xlnk Xlnk.set_allocator_library('/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2equalizeHist.so') mem_manager = Xlnk() import pynq_cv.overlays.xv2equalizeHist as xv2 import numpy as np import cv2 import time import OpenCVUtils as cvu print("Loading image ../images/bigBunny_1080.png") img1 = cv2.imread('../images/bigBunny_1080.png') imgY1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY) print("Size of imgY1 is ",imgY1.shape); height, width, channels = img1.shape
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ############################################################################### print("Running testXfCalcOpticalFlowDenseNonPyrLK.py ...") print("Loading overlay") from pynq import Overlay bs = Overlay( "/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2calcOpticalFlowDenseNonPyrLK.bit" ) bs.download() print("Loading xlnk") from pynq import Xlnk Xlnk.set_allocator_library( '/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2calcOpticalFlowDenseNonPyrLK.so' ) mem_manager = Xlnk() import pynq_cv.overlays.xv2calcOpticalFlowDenseNonPyrLK as xv2 import numpy as np import cv2 import time import OpenCVUtils as cvu numberOfIterations = 1 print("Number of loop iterations: " + str(numberOfIterations)) print("Loading image ../images/000005_10_L.png") prev = cv2.imread('../images/000005_10_L.png')
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ############################################################################### print("Running testXfMedianBlur.py ...") print("Loading overlay") from pynq import Overlay bs = Overlay( "/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2medianBlur.bit" ) bs.download() print("Loading xlnk") from pynq import Xlnk Xlnk.set_allocator_library( '/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2medianBlur.so') mem_manager = Xlnk() import pynq_cv.overlays.xv2medianBlur as xv2 import numpy as np import cv2 import time import OpenCVUtils as cvu print("Loading image ../images/bigBunny_1080.png") img = cv2.imread('../images/bigBunny_1080.png') imgY = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) print("Size of imgY is ", imgY.shape) height, width, channels = img.shape
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ############################################################################### print("Running testXfAccumulate.py ...") print("Loading overlay") from pynq import Overlay bs = Overlay( "/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2accumulate.bit" ) bs.download() print("Loading xlnk") from pynq import Xlnk Xlnk.set_allocator_library( '/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2accumulate.so') mem_manager = Xlnk() import pynq_cv.overlays.xv2accumulate as xv2 import numpy as np import cv2 import time import OpenCVUtils as cvu print("Loading image ../images/bigBunny_1080.png") img1 = cv2.imread('../images/bigBunny_1080.png') imgY1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) print("Size of imgY1 is ", imgY1.shape) height, width, channels = img1.shape
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ############################################################################### print("Running testXfWarpAffine.py ...") print("Loading overlay") from pynq import Overlay bs = Overlay( "/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2warpAffine.bit" ) bs.download() print("Loading xlnk") from pynq import Xlnk Xlnk.set_allocator_library( '/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2warpAffine.so') mem_manager = Xlnk() import pynq_cv.overlays.xv2warpAffine as xv2 import numpy as np import cv2 import time import OpenCVUtils as cvu print("Loading image ../images/bigBunny_1080.png") img = cv2.imread('../images/bigBunny_1080.png') imgY = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) print("Size of imgY is ", imgY.shape) height, width, channels = img.shape
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ############################################################################### print("Running testXfCornerHarris.py ...") print("Loading overlay") from pynq import Overlay bs = Overlay( "/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2cornerHarris.bit" ) bs.download() print("Loading xlnk") from pynq import Xlnk Xlnk.set_allocator_library( '/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2cornerHarris.so' ) mem_manager = Xlnk() import pynq_cv.overlays.xv2cornerHarris as xv2 import numpy as np import cv2 import time import OpenCVUtils as cvu print("Loading image ../images/bigBunny_1080.png") img1 = cv2.imread('../images/bigBunny_1080.png') imgY1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) print("Size of imgY1 is ", imgY1.shape)
class EKF(object): """EKF abstract class. The bitstream and lib inputs to this class are defaulted to None, so this method should only be used as the parent class. Directly using it will lead to exceptions. """ __metaclass__ = ABCMeta def __init__(self, n, m, pval=0.5, qval=0.1, rval=20.0, bitstream=None, library=None, cacheable=0): """Initialize the EKF object. Parameters ---------- n : int number of states m : int number of observables/measurements pval : float prediction noise covariance qval : float state noise covariance rval : float measurement noise covariance bitstream : str string identifier of the bitstream library : str string identifier of the C library cacheable : int Whether the buffers should be cacheable - defaults to 0 """ self.bitstream_name = bitstream self.overlay = Overlay(self.bitstream_name) self.library = library self.xlnk = Xlnk() self.xlnk.set_allocator_library(self.library) self._ffi = cffi.FFI() self.dlib = self._ffi.dlopen(self.library) self._ffi.cdef(self.ffi_interface) # Whether to use sds_alloc or sds_alloc_non_cacheable self.cacheable = cacheable # No previous prediction noise covariance self.P_pre = None # Current state is zero, with diagonal noise covariance matrix self.x = np.zeros(n) self.P_post = np.eye(n) * pval # Set up covariance matrices for process noise and measurement noise self.Q = np.eye(n) * qval self.R = np.eye(m) * rval # Identity matrix self.I = np.eye(n) def reload_overlay(self): """Reloading the bitstream onto PL. This method is no needed during initialization, but can be called explicitly by users. """ self.overlay.download() def step(self, z, **kwargs): """Runs one step of the EKF on observations z. This is the SW only implementation. Parameters ---------- z: tuple A tuple of length m Returns ------- numpy.ndarray representing the updated state. """ # Predict $\hat{x}_k = f(\hat{x}_{k-1})$ self.x, F = self.f(self.x, **kwargs) # $P_k = F_{k-1} P_{k-1} F^T_{k-1} + Q_{k-1}$ self.P_pre = np.dot(np.dot(F, self.P_post), F.T) + self.Q # Update h, H = self.h(self.x, **kwargs) # $G_k = P_k H^T_k (H_k P_k H^T_k + R)^{-1}$ G = np.dot(np.dot(self.P_pre, H.T), np.linalg.inv( np.dot(np.dot(H, self.P_pre), H.T) + self.R)) # $\hat{x}_k = \hat{x_k} + G_k(z_k - h(\hat{x}_k))$ self.x += np.dot(G, (z - h)) # $P_k = (I - G_k H_k) P_k$ self.P_post = np.dot(self.I - np.dot(G, H), self.P_pre) return self.x def copy_array(self, x, dtype=np.int32): """Copy numpy array to contiguous memory. Parameters ---------- x: np.array Input numpy array, not necessarily in contiguous memory. dtype : type Data type for the give numpy array. Returns ------- xlnk.cma_array Physically contiguous memory. """ size = x.shape data_buffer = self.xlnk.cma_array(shape=size, dtype=dtype, cacheable=self.cacheable) np.copyto(data_buffer, x.astype(dtype), casting="unsafe") return data_buffer @abstractmethod def ffi_interface(self): raise NotImplementedError("ffi_interface is not implemented.") @abstractmethod def f(self, x): """Abstract method for f(x) Your implementing class should define this method for the state-transition function f(x). Your state-transition fucntion should return a NumPy array of n elements representing the new state, and a nXn NumPy array of elements representing the the Jacobian of the function with respect to the new state. """ raise NotImplementedError("Method f() is not implemented.") @abstractmethod def h(self, x): """Abstract method for h(x) Your implementing class should define this method for the observation function h(x), returning a NumPy array of m elements, and a NumPy array of m x n elements representing the Jacobian matrix H of the observation function with respect to the observation. """ raise NotImplementedError("Method h() is not implemented.")
# OR BUSINESS INTERRUPTION). HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ############################################################################### print("Running testXfInitUndistortRectifyMap.py ...") print("Loading overlay") from pynq import Overlay bs = Overlay("/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2initUndistortRectifyMap.bit") bs.download() print("Loading xlnk") from pynq import Xlnk Xlnk.set_allocator_library('/usr/local/lib/python3.6/dist-packages/pynq_cv/overlays/xv2initUndistortRectifyMap.so') mem_manager = Xlnk() import pynq_cv.overlays.xv2initUndistortRectifyMap as xv2 import numpy as np import cv2 import time import OpenCVUtils as cvu print("Loading image ../images/bigBunny_1080.png") img1 = cv2.imread('../images/bigBunny_1080.png') imgY1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY) print("Size of imgY1 is ",imgY1.shape); height, width, channels = img1.shape