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