1992-09-22 17:23:17 +00:00
|
|
|
# Live video input class.
|
|
|
|
# Note that importing this module attempts to initialize video.
|
|
|
|
|
|
|
|
|
|
|
|
# Check if video is available.
|
|
|
|
# There are three reasons for failure here:
|
|
|
|
# (1) this version of Python may not have the sv or imageop modules;
|
|
|
|
# (2) this machine may not have a video board;
|
|
|
|
# (3) initializing the video board may fail for another reason.
|
|
|
|
# The global variable have_video is set to true iff we reall do have video.
|
|
|
|
|
|
|
|
try:
|
|
|
|
import sv
|
|
|
|
import SV
|
|
|
|
import imageop
|
|
|
|
try:
|
|
|
|
v = sv.OpenVideo()
|
|
|
|
have_video = 1
|
|
|
|
except sv.error:
|
|
|
|
have_video = 0
|
|
|
|
except ImportError:
|
|
|
|
have_video = 0
|
|
|
|
|
|
|
|
|
|
|
|
# The live video input class.
|
|
|
|
# Only instantiate this if have_video is true!
|
|
|
|
|
|
|
|
class LiveVideoIn:
|
|
|
|
|
1992-09-24 16:53:51 +00:00
|
|
|
# Initialize an instance. Arguments:
|
|
|
|
# vw, vh: size of the video window data to be captured.
|
1992-09-24 16:55:31 +00:00
|
|
|
# For some reason, vw MUST be a multiple of 4.
|
1992-09-24 16:53:51 +00:00
|
|
|
# Note that the data has to be cropped unless vw and vh are
|
|
|
|
# just right for the video board (vw:vh == 4:3 and vh even).
|
1992-09-22 17:23:17 +00:00
|
|
|
|
1992-12-14 12:25:21 +00:00
|
|
|
def init(self, pktmax, vw, vh, type):
|
1992-09-22 17:23:17 +00:00
|
|
|
if not have_video:
|
|
|
|
raise RuntimeError, 'no video available'
|
1992-09-24 16:53:51 +00:00
|
|
|
if vw % 4 != 0:
|
|
|
|
raise ValueError, 'vw must be a multiple of 4'
|
|
|
|
self.pktmax = pktmax
|
1992-09-22 17:23:17 +00:00
|
|
|
realvw = vh*SV.PAL_XMAX/SV.PAL_YMAX
|
|
|
|
if realvw < vw:
|
1992-09-24 16:53:51 +00:00
|
|
|
realvw = vw
|
1992-09-22 17:23:17 +00:00
|
|
|
self.realwidth, self.realheight = v.QuerySize(realvw, vh)
|
1992-12-23 15:37:20 +00:00
|
|
|
if not type in ('rgb8', 'grey', 'mono', 'grey2', 'grey4'):
|
|
|
|
raise 'Incorrent video data type', type
|
1992-12-14 12:25:21 +00:00
|
|
|
self.type = type
|
1992-12-23 15:37:20 +00:00
|
|
|
if type in ('grey', 'grey4', 'grey2', 'mono'):
|
1992-12-14 12:25:21 +00:00
|
|
|
v.SetParam([SV.COLOR, SV.MONO, SV.INPUT_BYPASS, 1])
|
|
|
|
else:
|
|
|
|
v.SetParam([SV.COLOR, SV.DEFAULT_COLOR, \
|
|
|
|
SV.INPUT_BYPASS, 0])
|
1992-09-24 16:53:51 +00:00
|
|
|
# Initialize capture
|
|
|
|
(mode, self.realwidth, self.realheight, qsize, rate) = \
|
|
|
|
v.InitContinuousCapture(SV.RGB8_FRAMES, \
|
|
|
|
self.realwidth, self.realheight, 1, 5)
|
1992-09-22 17:23:17 +00:00
|
|
|
self.width = vw
|
|
|
|
self.height = vh
|
|
|
|
self.x0 = (self.realwidth-self.width)/2
|
|
|
|
self.x1 = self.x0 + self.width - 1
|
|
|
|
self.y0 = (self.realheight-self.height)/2
|
|
|
|
self.y1 = self.y0 + self.height - 1
|
|
|
|
# Compute # full lines per packet
|
|
|
|
self.lpp = pktmax / self.width
|
|
|
|
self.pktsize = self.lpp*self.width
|
|
|
|
self.data = None
|
1992-09-24 16:53:51 +00:00
|
|
|
self.dataoffset = 0
|
1992-09-22 17:23:17 +00:00
|
|
|
self.lpos = 0
|
1992-09-24 16:53:51 +00:00
|
|
|
self.justright = (self.realwidth == self.width and \
|
|
|
|
self.realheight == self.height)
|
|
|
|
## if not self.justright:
|
|
|
|
## print 'Want:', self.width, 'x', self.height,
|
|
|
|
## print '; grab:', self.realwidth, 'x', self.realheight
|
1992-09-22 17:23:17 +00:00
|
|
|
return self
|
|
|
|
|
1992-09-24 16:53:51 +00:00
|
|
|
# Change the size of the video being displayed.
|
|
|
|
|
|
|
|
def resizevideo(self, vw, vh):
|
|
|
|
self.close()
|
1992-12-14 12:25:21 +00:00
|
|
|
self = self.init(self.pktmax, vw, vh, self.type)
|
1992-09-24 16:53:51 +00:00
|
|
|
|
1992-09-22 17:23:17 +00:00
|
|
|
# Remove an instance.
|
|
|
|
# This turns off continuous capture.
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
v.EndContinuousCapture()
|
|
|
|
|
|
|
|
# Get the next video packet.
|
|
|
|
# This returns (lpos, data) where:
|
|
|
|
# - lpos is the line position
|
|
|
|
# - data is a piece of data
|
|
|
|
# The dimensions of data are:
|
|
|
|
# - pixel depth = 1 byte
|
|
|
|
# - scan line width = self.width (the vw argument to init())
|
|
|
|
# - number of scan lines = self.lpp (PKTMAX / vw)
|
|
|
|
|
|
|
|
def getnextpacket(self):
|
1992-09-24 16:53:51 +00:00
|
|
|
if not self.data or self.dataoffset >= len(self.data):
|
1992-09-22 17:23:17 +00:00
|
|
|
try:
|
|
|
|
cd, id = v.GetCaptureData()
|
|
|
|
except sv.error:
|
|
|
|
return None
|
|
|
|
data = cd.InterleaveFields(1)
|
|
|
|
cd.UnlockCaptureData()
|
1992-09-24 16:53:51 +00:00
|
|
|
if self.justright:
|
|
|
|
self.data = data
|
|
|
|
else:
|
|
|
|
self.data = imageop.crop(data, 1, \
|
|
|
|
self.realwidth, \
|
|
|
|
self.realheight, \
|
|
|
|
self.x0, self.y0, \
|
|
|
|
self.x1, self.y1)
|
1992-09-22 17:23:17 +00:00
|
|
|
self.lpos = 0
|
1992-09-24 16:53:51 +00:00
|
|
|
self.dataoffset = 0
|
1992-12-14 12:25:21 +00:00
|
|
|
if self.type == 'mono':
|
|
|
|
self.data = imageop.dither2mono(self.data, \
|
|
|
|
self.width, self.height)
|
1992-12-23 15:37:20 +00:00
|
|
|
elif self.type == 'grey2':
|
|
|
|
self.data = imageop.dither2grey2(self.data, \
|
|
|
|
self.width, self.height)
|
|
|
|
elif self.type == 'grey4':
|
|
|
|
self.data = imageop.grey2grey4(self.data, \
|
|
|
|
self.width, self.height)
|
1992-09-24 16:53:51 +00:00
|
|
|
data = self.data[self.dataoffset:self.dataoffset+self.pktsize]
|
1992-09-22 17:23:17 +00:00
|
|
|
lpos = self.lpos
|
1992-09-24 16:53:51 +00:00
|
|
|
self.dataoffset = self.dataoffset + self.pktsize
|
1992-12-14 12:25:21 +00:00
|
|
|
if self.type == 'mono':
|
|
|
|
self.lpos = self.lpos + self.lpp*8
|
1992-12-23 15:37:20 +00:00
|
|
|
elif self.type == 'grey2':
|
|
|
|
self.lpos = self.lpos + self.lpp*4
|
|
|
|
elif self.type == 'grey4':
|
|
|
|
self.lpos = self.lpos + self.lpp*2
|
1992-12-14 12:25:21 +00:00
|
|
|
else:
|
|
|
|
self.lpos = self.lpos + self.lpp
|
1992-09-22 17:23:17 +00:00
|
|
|
return lpos, data
|