9a74823599519a12f46ca5011277a4f0225df883
[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 cv2
6 import numpy as np
7
8 import gi
9 gi.require_version('Gst', '1.0')
10 from gi.repository import Gst
11 Gst.init(None)
12
13 from gi.repository import GObject
14 GObject.threads_init()
15
16
17 def calc_matrix():
18     top_x_shift = 200
19
20     width, height = 800, 480
21     corners = np.array([(0, 0), (width, 0), (width, height), (0, height)], dtype=np.float32)
22     target = np.array([(top_x_shift, 0), (width - top_x_shift, 0), (width, height), (0, height)], dtype=np.float32)
23
24     mat = cv2.getPerspectiveTransform(corners, target)
25     ret, inv_mat = cv2.invert(mat)
26
27     return inv_mat.flatten()
28
29
30
31 def main():
32     pipeline = Gst.ElementFactory.make('pipeline', None)
33
34     videosrc = Gst.ElementFactory.make('videotestsrc', None)
35     pipeline.add(videosrc)
36
37     # Test the perspective element
38     perspective = Gst.ElementFactory.make("perspective", None)
39     pipeline.add(perspective)
40
41     print perspective.get_property("matrix")
42
43     M = calc_matrix()
44     perspective.set_property("matrix", M)
45     print perspective.get_property("matrix")
46
47     # This should fail!
48     perspective.set_property("matrix", [0])
49     print perspective.get_property("matrix")
50
51     videoconvert = Gst.ElementFactory.make("autovideoconvert", None)
52     pipeline.add(videoconvert)
53
54     videosink = Gst.ElementFactory.make("autovideosink", None)
55     pipeline.add(videosink)
56
57     caps = Gst.caps_from_string(
58         "video/x-raw,format=\"AYUV\",width=800,height=480")
59
60     videosrc.link_filtered(perspective, caps)
61     perspective.link(videoconvert)
62     videoconvert.link(videosink)
63
64     pipeline.set_state(Gst.State.PLAYING)
65
66     loop = GObject.MainLoop()
67     loop.run()
68
69
70 if __name__ == '__main__':
71     main()