Move the sources to trunk
[opencv] / tests / python / highgui / seek_test.py
1 """
2 This script will test highgui's seek functionality
3 for different video formats
4 """
5
6 # import the necessary things for OpenCV and comparson routine
7 import os
8 import python
9 from python.highgui import *
10 from python.cv import *
11 import match
12
13
14 # path to videos and images we need
15 PREFIX=os.environ["top_srcdir"]+"/tests/python/testdata/"
16
17 # this is the folder with the videos and images
18 # and name of output window
19 IMAGES          = PREFIX+"images/"
20 VIDEOS          = PREFIX+"videos/"
21
22
23
24 show_frames=False
25
26 # testing routine, seeks through file and compares read images with frames in frames.QCIF[]
27 def seek_frame_ok(FILENAME,ERRORS):
28   # create a video reader using the tiny videofile VIDEOS+FILENAME
29   video=cvCreateFileCapture(VIDEOS+FILENAME)
30
31   if video is None:
32     # couldn't open video (FAIL)
33     return 1
34
35   if show_frames:
36     cvNamedWindow("test", CV_WINDOW_AUTOSIZE)
37
38   # skip 2 frames and read 3rd frame each until EOF and check if the read image is ok
39   for k in [0,3,6,9,12,15,18,21,24,27]:
40     cvSetCaptureProperty(video, CV_CAP_PROP_POS_FRAMES, k)
41
42     # try to query frame
43     image=cvQueryFrame(video)
44
45     if image is None:
46     # returned image is NULL (FAIL)
47       return 1
48
49     compresult = match.match(image,k,ERRORS[k])
50     if not compresult:
51       return 1
52
53     if show_frames:
54       cvShowImage("test",image)
55       cvWaitKey(200)
56
57   # same as above, just backwards...
58   for k in [27,24,21,18,15,12,9,6,3,0]:
59
60     cvSetCaptureProperty(video, CV_CAP_PROP_POS_FRAMES, k)
61
62     # try to query frame
63     image=cvQueryFrame(video)
64
65     if image is None:
66     # returned image is NULL (FAIL)
67       return 1
68
69     compresult = match.match(image,k,ERRORS[k])
70     if not compresult:
71       return 1
72
73     if show_frames:
74       cvShowImage("test",image)
75       cvWaitKey(200)
76
77   # ATTENTION: We do not release the video reader, window or any image.
78   # This is bad manners, but Python and OpenCV don't care,
79   # the whole memory segment will be freed on finish anyway...
80
81   del video
82   # everything is fine (PASS)
83   return 0
84
85
86 # testing routine, seeks through file and compares read images with frames in frames.QCIF[]
87 def seek_time_ok(FILENAME,ERRORS):
88
89   # create a video reader using the tiny videofile VIDEOS+FILENAME
90   video=cvCreateFileCapture(VIDEOS+FILENAME)
91
92   if video is None:
93     # couldn't open video (FAIL)
94     return 1
95
96   if show_frames:
97     cvNamedWindow("test", CV_WINDOW_AUTOSIZE)
98
99   # skip 2 frames and read 3rd frame each until EOF and check if the read image is ok
100   for k in [0,3,6,9,12,15,18,21,24,27]:
101
102     cvSetCaptureProperty(video, CV_CAP_PROP_POS_MSEC, k*40)
103
104     # try to query frame
105     image=cvQueryFrame(video)
106
107     if image is None:
108     # returned image is NULL (FAIL)
109       return 1
110
111     compresult = match.match(image,k,ERRORS[k])
112     if not compresult:
113       return 1
114
115     if show_frames:
116       cvShowImage("test",image)
117       cvWaitKey(200)
118
119   # same as above, just backwards...
120   for k in [27,24,21,18,15,12,9,6,3,0]:
121
122     cvSetCaptureProperty(video, CV_CAP_PROP_POS_MSEC, k*40)
123
124     # try to query frame
125     image=cvQueryFrame(video)
126
127     if image is None:
128     # returned image is NULL (FAIL)
129       return 1
130
131     compresult = match.match(image,k,ERRORS[k])
132     if not compresult:
133       return 1
134
135     if show_frames:
136       cvShowImage("test",image)
137       cvWaitKey(200)
138
139   # ATTENTION: We do not release the video reader, window or any image.
140   # This is bad manners, but Python and OpenCV don't care,
141   # the whole memory segment will be freed on finish anyway...
142
143   del video
144   # everything is fine (PASS)
145   return 0