blob: 66cd649b83801e62eff6469eda51f7d5c46487e0 [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.
32 # For some reason, vw MUST be a multiples 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).
Guido van Rossumba066151992-09-22 17:23:17 +000035
36 def init(self, pktmax, vw, vh):
37 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)
Guido van Rossumd65f45d1992-09-24 16:53:51 +000046 # Initialize capture
47 (mode, self.realwidth, self.realheight, qsize, rate) = \
48 v.InitContinuousCapture(SV.RGB8_FRAMES, \
49 self.realwidth, self.realheight, 1, 5)
Guido van Rossumba066151992-09-22 17:23:17 +000050 self.width = vw
51 self.height = vh
52 self.x0 = (self.realwidth-self.width)/2
53 self.x1 = self.x0 + self.width - 1
54 self.y0 = (self.realheight-self.height)/2
55 self.y1 = self.y0 + self.height - 1
56 # Compute # full lines per packet
57 self.lpp = pktmax / self.width
58 self.pktsize = self.lpp*self.width
Guido van Rossumba066151992-09-22 17:23:17 +000059 self.data = None
Guido van Rossumd65f45d1992-09-24 16:53:51 +000060 self.dataoffset = 0
Guido van Rossumba066151992-09-22 17:23:17 +000061 self.lpos = 0
Guido van Rossumd65f45d1992-09-24 16:53:51 +000062 self.justright = (self.realwidth == self.width and \
63 self.realheight == self.height)
64## if not self.justright:
65## print 'Want:', self.width, 'x', self.height,
66## print '; grab:', self.realwidth, 'x', self.realheight
Guido van Rossumba066151992-09-22 17:23:17 +000067 return self
68
Guido van Rossumd65f45d1992-09-24 16:53:51 +000069 # Change the size of the video being displayed.
70
71 def resizevideo(self, vw, vh):
72 self.close()
73 self = self.init(self.pktmax, vw, vh)
74
Guido van Rossumba066151992-09-22 17:23:17 +000075 # Remove an instance.
76 # This turns off continuous capture.
77
78 def close(self):
79 v.EndContinuousCapture()
80
81 # Get the next video packet.
82 # This returns (lpos, data) where:
83 # - lpos is the line position
84 # - data is a piece of data
85 # The dimensions of data are:
86 # - pixel depth = 1 byte
87 # - scan line width = self.width (the vw argument to init())
88 # - number of scan lines = self.lpp (PKTMAX / vw)
89
90 def getnextpacket(self):
Guido van Rossumd65f45d1992-09-24 16:53:51 +000091 if not self.data or self.dataoffset >= len(self.data):
Guido van Rossumba066151992-09-22 17:23:17 +000092 try:
93 cd, id = v.GetCaptureData()
94 except sv.error:
95 return None
96 data = cd.InterleaveFields(1)
97 cd.UnlockCaptureData()
Guido van Rossumd65f45d1992-09-24 16:53:51 +000098 if self.justright:
99 self.data = data
100 else:
101 self.data = imageop.crop(data, 1, \
102 self.realwidth, \
103 self.realheight, \
104 self.x0, self.y0, \
105 self.x1, self.y1)
Guido van Rossumba066151992-09-22 17:23:17 +0000106 self.lpos = 0
Guido van Rossumd65f45d1992-09-24 16:53:51 +0000107 self.dataoffset = 0
108 data = self.data[self.dataoffset:self.dataoffset+self.pktsize]
Guido van Rossumba066151992-09-22 17:23:17 +0000109 lpos = self.lpos
Guido van Rossumd65f45d1992-09-24 16:53:51 +0000110 self.dataoffset = self.dataoffset + self.pktsize
Guido van Rossumba066151992-09-22 17:23:17 +0000111 self.lpos = self.lpos + self.lpp
112 return lpos, data