1 # Live video input class.
2 # Note that importing this module attempts to initialize video.
5 # Check if video is available.
6 # There are three reasons for failure here:
7 # (1) this version of Python may not have the sv or imageop modules;
8 # (2) this machine may not have a video board;
9 # (3) initializing the video board may fail for another reason.
10 # The global variable have_video is set to true iff we reall do have video.
25 # The live video input class.
26 # Only instantiate this if have_video is true!
30 # Initialize an instance. Arguments:
31 # vw, vh: size of the video window data to be captured.
32 # For some reason, vw MUST be a multiple of 4.
33 # Note that the data has to be cropped unless vw and vh are
34 # just right for the video board (vw:vh == 4:3 and vh even).
36 def __init__(self
, pktmax
, vw
, vh
, type):
38 raise RuntimeError, 'no video available'
40 raise ValueError, 'vw must be a multiple of 4'
42 realvw
= vh
*SV
.PAL_XMAX
/SV
.PAL_YMAX
45 self
.realwidth
, self
.realheight
= v
.QuerySize(realvw
, vh
)
46 if not type in ('rgb8', 'grey', 'mono', 'grey2', 'grey4'):
47 raise 'Incorrent video data type', type
49 if type in ('grey', 'grey4', 'grey2', 'mono'):
50 v
.SetParam([SV
.COLOR
, SV
.MONO
, SV
.INPUT_BYPASS
, 1])
52 v
.SetParam([SV
.COLOR
, SV
.DEFAULT_COLOR
, \
55 (mode
, self
.realwidth
, self
.realheight
, qsize
, rate
) = \
56 v
.InitContinuousCapture(SV
.RGB8_FRAMES
, \
57 self
.realwidth
, self
.realheight
, 1, 2)
60 self
.x0
= (self
.realwidth
-self
.width
)/2
61 self
.x1
= self
.x0
+ self
.width
- 1
62 self
.y0
= (self
.realheight
-self
.height
)/2
63 self
.y1
= self
.y0
+ self
.height
- 1
64 # Compute # full lines per packet
65 self
.lpp
= pktmax
/ self
.linewidth()
66 self
.pktsize
= self
.lpp
*self
.linewidth()
70 self
.justright
= (self
.realwidth
== self
.width
and \
71 self
.realheight
== self
.height
)
72 ## if not self.justright:
73 ## print 'Want:', self.width, 'x', self.height,
74 ## print '; grab:', self.realwidth, 'x', self.realheight
76 # Change the size of the video being displayed.
78 def resizevideo(self
, vw
, vh
):
80 self
.__init
__(self
.pktmax
, vw
, vh
, self
.type)
83 # This turns off continuous capture.
86 v
.EndContinuousCapture()
88 # Get the length in bytes of a video line
90 if self
.type == 'mono':
91 return (self
.width
+7)/8
92 elif self
.type == 'grey2':
93 return (self
.width
+3)/4
94 elif self
.type == 'grey4':
95 return (self
.width
+1)/2
99 # Get the next video packet.
100 # This returns (lpos, data) where:
101 # - lpos is the line position
102 # - data is a piece of data
103 # The dimensions of data are:
104 # - pixel depth = 1 byte
105 # - scan line width = self.width (the vw argument to __init__())
106 # - number of scan lines = self.lpp (PKTMAX / vw)
108 def getnextpacket(self
):
109 if not self
.data
or self
.dataoffset
>= len(self
.data
):
111 cd
, id = v
.GetCaptureData()
114 data
= cd
.InterleaveFields(1)
115 cd
.UnlockCaptureData()
119 self
.data
= imageop
.crop(data
, 1, \
126 if self
.type == 'mono':
127 self
.data
= imageop
.dither2mono(self
.data
, \
128 self
.width
, self
.height
)
129 elif self
.type == 'grey2':
130 self
.data
= imageop
.dither2grey2(self
.data
, \
131 self
.width
, self
.height
)
132 elif self
.type == 'grey4':
133 self
.data
= imageop
.grey2grey4(self
.data
, \
134 self
.width
, self
.height
)
135 data
= self
.data
[self
.dataoffset
:self
.dataoffset
+self
.pktsize
]
137 self
.dataoffset
= self
.dataoffset
+ self
.pktsize
138 self
.lpos
= self
.lpos
+ self
.lpp