Add a test program for the perspective element
authorAntonio Ospite <ao2@ao2.it>
Thu, 9 Oct 2014 09:00:07 +0000 (11:00 +0200)
committerAntonio Ospite <ao2@ao2.it>
Thu, 9 Oct 2014 09:00:07 +0000 (11:00 +0200)
python/gst-test-perspective.py [new file with mode: 0755]

diff --git a/python/gst-test-perspective.py b/python/gst-test-perspective.py
new file mode 100755 (executable)
index 0000000..b51591a
--- /dev/null
@@ -0,0 +1,73 @@
+#!/usr/bin/env python
+
+# test program for the "perspective" geometric tranform element
+
+import cv
+
+import gi
+gi.require_version('Gst', '1.0')
+from gi.repository import Gst
+Gst.init(None)
+
+from gi.repository import GObject
+GObject.threads_init()
+
+
+def calc_matrix():
+    top_x_shift = 200
+
+    width, height = 800, 480
+    corners = [(0, 0), (width, 0), (width, height), (0, height)]
+    target = [(top_x_shift, 0), (width - top_x_shift, 0), (width, height), (0, height)]
+
+    mat = cv.CreateMat(3, 3, cv.CV_64F)
+    cv.GetPerspectiveTransform(corners, target, mat)
+
+    inv_mat = cv.CreateMat(3, 3, cv.CV_64F)
+    cv.Invert(mat, inv_mat)
+
+    matrix = [inv_mat[j, i] for j in range(mat.rows) for i in range(mat.cols)]
+    return matrix
+
+
+def main():
+    pipeline = Gst.ElementFactory.make('pipeline', None)
+
+    videosrc = Gst.ElementFactory.make('videotestsrc', None)
+    pipeline.add(videosrc)
+
+    # Test the perspective element
+    perspective = Gst.ElementFactory.make("perspective", None)
+    pipeline.add(perspective)
+
+    print perspective.get_property("matrix")
+
+    M = calc_matrix()
+    perspective.set_property("matrix", M)
+    print perspective.get_property("matrix")
+
+    # This should fail!
+    perspective.set_property("matrix", [0])
+    print perspective.get_property("matrix")
+
+    videoconvert = Gst.ElementFactory.make("autovideoconvert", None)
+    pipeline.add(videoconvert)
+
+    videosink = Gst.ElementFactory.make("autovideosink", None)
+    pipeline.add(videosink)
+
+    caps = Gst.caps_from_string(
+        "video/x-raw,format=\"AYUV\",width=800,height=480")
+
+    videosrc.link_filtered(perspective, caps)
+    perspective.link(videoconvert)
+    videoconvert.link(videosink)
+
+    pipeline.set_state(Gst.State.PLAYING)
+
+    loop = GObject.MainLoop()
+    loop.run()
+
+
+if __name__ == '__main__':
+    main()