blob: 661ea738b27935c46f48673151d90da8286c8058 [file] [log] [blame]
Guido van Rossumba066151992-09-22 17:23:17 +00001# Live video input class.
2# Note that importing this module attempts to initialize video.
3
4
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.
11
12try:
13 import sv
14 import SV
15 import imageop
16 try:
17 v = sv.OpenVideo()
18 have_video = 1
19 except sv.error:
20 have_video = 0
21except ImportError:
22 have_video = 0
23
24
25# The live video input class.
26# Only instantiate this if have_video is true!
27
28class LiveVideoIn:
29
Guido van Rossumd65f45d1992-09-24 16:53:51 +000030 # Initialize an instance. Arguments:
31 # vw, vh: size of the video window data to be captured.
Guido van Rossumf8848ac1992-09-24 16:55:31 +000032 # For some reason, vw MUST be a multiple of 4.
Guido van Rossumd65f45d1992-09-24 16:53:51 +000033 # 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).
Guido van Rossumba066151992-09-22 17:23:17 +000035
Guido van Rossum21a3ff91993-12-17 15:11:41 +000036 def __init__(self, pktmax, vw, vh, type):
Guido van Rossumba066151992-09-22 17:23:17 +000037 if not have_video:
38 raise RuntimeError, 'no video available'
Guido van Rossumd65f45d1992-09-24 16:53:51 +000039 if vw % 4 != 0:
40 raise ValueError, 'vw must be a multiple of 4'
41 self.pktmax = pktmax
Guido van Rossumba066151992-09-22 17:23:17 +000042 realvw = vh*SV.PAL_XMAX/SV.PAL_YMAX
43 if realvw < vw:
Guido van Rossumd65f45d1992-09-24 16:53:51 +000044 realvw = vw
Guido van Rossumba066151992-09-22 17:23:17 +000045 self.realwidth, self.realheight = v.QuerySize(realvw, vh)
Jack Jansen6bc8c7f1992-12-23 15:37:20 +000046 if not type in ('rgb8', 'grey', 'mono', 'grey2', 'grey4'):
47 raise 'Incorrent video data type', type
Jack Jansen3b253711992-12-14 12:25:21 +000048 self.type = type
Jack Jansen6bc8c7f1992-12-23 15:37:20 +000049 if type in ('grey', 'grey4', 'grey2', 'mono'):
Jack Jansen3b253711992-12-14 12:25:21 +000050 v.SetParam([SV.COLOR, SV.MONO, SV.INPUT_BYPASS, 1])
51 else:
52 v.SetParam([SV.COLOR, SV.DEFAULT_COLOR, \
53 SV.INPUT_BYPASS, 0])
Guido van Rossumd65f45d1992-09-24 16:53:51 +000054 # Initialize capture
55 (mode, self.realwidth, self.realheight, qsize, rate) = \
56 v.InitContinuousCapture(SV.RGB8_FRAMES, \
Jack Jansen7961ea71993-02-17 15:54:06 +000057 self.realwidth, self.realheight, 1, 2)
Guido van Rossumba066151992-09-22 17:23:17 +000058 self.width = vw
59 self.height = vh
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
Jack Jansen7961ea71993-02-17 15:54:06 +000065 self.lpp = pktmax / self.linewidth()
66 self.pktsize = self.lpp*self.linewidth()
Guido van Rossumba066151992-09-22 17:23:17 +000067 self.data = None
Guido van Rossumd65f45d1992-09-24 16:53:51 +000068 self.dataoffset = 0
Guido van Rossumba066151992-09-22 17:23:17 +000069 self.lpos = 0
Guido van Rossumd65f45d1992-09-24 16:53:51 +000070 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
Guido van Rossumba066151992-09-22 17:23:17 +000075
Guido van Rossumd65f45d1992-09-24 16:53:51 +000076 # Change the size of the video being displayed.
77
78 def resizevideo(self, vw, vh):
79 self.close()
Guido van Rossum21a3ff91993-12-17 15:11:41 +000080 self.__init__(self.pktmax, vw, vh, self.type)
Guido van Rossumd65f45d1992-09-24 16:53:51 +000081
Guido van Rossumba066151992-09-22 17:23:17 +000082 # Remove an instance.
83 # This turns off continuous capture.
84
85 def close(self):
86 v.EndContinuousCapture()
87
Jack Jansen7961ea71993-02-17 15:54:06 +000088 # Get the length in bytes of a video line
89 def linewidth(self):
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
96 else:
97 return self.width
98
Guido van Rossumba066151992-09-22 17:23:17 +000099 # 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
Guido van Rossum21a3ff91993-12-17 15:11:41 +0000105 # - scan line width = self.width (the vw argument to __init__())
Guido van Rossumba066151992-09-22 17:23:17 +0000106 # - number of scan lines = self.lpp (PKTMAX / vw)
107
108 def getnextpacket(self):
Guido van Rossumd65f45d1992-09-24 16:53:51 +0000109 if not self.data or self.dataoffset >= len(self.data):
Guido van Rossumba066151992-09-22 17:23:17 +0000110 try:
111 cd, id = v.GetCaptureData()
112 except sv.error:
113 return None
114 data = cd.InterleaveFields(1)
115 cd.UnlockCaptureData()
Guido van Rossumd65f45d1992-09-24 16:53:51 +0000116 if self.justright:
117 self.data = data
118 else:
119 self.data = imageop.crop(data, 1, \
120 self.realwidth, \
121 self.realheight, \
122 self.x0, self.y0, \
123 self.x1, self.y1)
Guido van Rossumba066151992-09-22 17:23:17 +0000124 self.lpos = 0
Guido van Rossumd65f45d1992-09-24 16:53:51 +0000125 self.dataoffset = 0
Jack Jansen3b253711992-12-14 12:25:21 +0000126 if self.type == 'mono':
127 self.data = imageop.dither2mono(self.data, \
128 self.width, self.height)
Jack Jansen6bc8c7f1992-12-23 15:37:20 +0000129 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)
Guido van Rossumd65f45d1992-09-24 16:53:51 +0000135 data = self.data[self.dataoffset:self.dataoffset+self.pktsize]
Guido van Rossumba066151992-09-22 17:23:17 +0000136 lpos = self.lpos
Guido van Rossumd65f45d1992-09-24 16:53:51 +0000137 self.dataoffset = self.dataoffset + self.pktsize
Jack Jansen7961ea71993-02-17 15:54:06 +0000138 self.lpos = self.lpos + self.lpp
Guido van Rossumba066151992-09-22 17:23:17 +0000139 return lpos, data