Add a script to create test files
[experiments/gstreamer.git] / python / gst-test-perspective.py
1 #!/usr/bin/env python
2
3 # test program for the "perspective" geometric tranform element
4
5 import cv
6
7 import gi
8 gi.require_version('Gst', '1.0')
9 from gi.repository import Gst
10 Gst.init(None)
11
12 from gi.repository import GObject
13 GObject.threads_init()
14
15
16 def calc_matrix():
17     top_x_shift = 200
18
19     width, height = 800, 480
20     corners = [(0, 0), (width, 0), (width, height), (0, height)]
21     target = [(top_x_shift, 0), (width - top_x_shift, 0), (width, height), (0, height)]
22
23     mat = cv.CreateMat(3, 3, cv.CV_64F)
24     cv.GetPerspectiveTransform(corners, target, mat)
25
26     inv_mat = cv.CreateMat(3, 3, cv.CV_64F)
27     cv.Invert(mat, inv_mat)
28
29     matrix = [inv_mat[j, i] for j in range(mat.rows) for i in range(mat.cols)]
30     return matrix
31
32
33 def main():
34     pipeline = Gst.ElementFactory.make('pipeline', None)
35
36     videosrc = Gst.ElementFactory.make('videotestsrc', None)
37     pipeline.add(videosrc)
38
39     # Test the perspective element
40     perspective = Gst.ElementFactory.make("perspective", None)
41     pipeline.add(perspective)
42
43     print perspective.get_property("matrix")
44
45     M = calc_matrix()
46     perspective.set_property("matrix", M)
47     print perspective.get_property("matrix")
48
49     # This should fail!
50     perspective.set_property("matrix", [0])
51     print perspective.get_property("matrix")
52
53     videoconvert = Gst.ElementFactory.make("autovideoconvert", None)
54     pipeline.add(videoconvert)
55
56     videosink = Gst.ElementFactory.make("autovideosink", None)
57     pipeline.add(videosink)
58
59     caps = Gst.caps_from_string(
60         "video/x-raw,format=\"AYUV\",width=800,height=480")
61
62     videosrc.link_filtered(perspective, caps)
63     perspective.link(videoconvert)
64     videoconvert.link(videosink)
65
66     pipeline.set_state(Gst.State.PLAYING)
67
68     loop = GObject.MainLoop()
69     loop.run()
70
71
72 if __name__ == '__main__':
73     main()