|
| 1 | +# 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 | + |
| 12 | +try: |
| 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 |
| 21 | +except ImportError: |
| 22 | + have_video = 0 |
| 23 | + |
| 24 | + |
| 25 | +# The live video input class. |
| 26 | +# Only instantiate this if have_video is true! |
| 27 | + |
| 28 | +class LiveVideoIn: |
| 29 | + |
| 30 | + # Initialize an instance. |
| 31 | + # Parameters: |
| 32 | + # - vw, vh specify the size of the video window. |
| 33 | + # This initializes continuous capture. |
| 34 | + |
| 35 | + def init(self, pktmax, vw, vh): |
| 36 | + if not have_video: |
| 37 | + raise RuntimeError, 'no video available' |
| 38 | + realvw = vh*SV.PAL_XMAX/SV.PAL_YMAX |
| 39 | + if realvw < vw: |
| 40 | + print 'Funny, image too narrow...' |
| 41 | + self.realwidth, self.realheight = v.QuerySize(realvw, vh) |
| 42 | + ##print 'Recording video in size', \ |
| 43 | + ## self.realwidth, self.realheight |
| 44 | + self.width = vw |
| 45 | + self.height = vh |
| 46 | + self.x0 = (self.realwidth-self.width)/2 |
| 47 | + self.x1 = self.x0 + self.width - 1 |
| 48 | + self.y0 = (self.realheight-self.height)/2 |
| 49 | + self.y1 = self.y0 + self.height - 1 |
| 50 | + # Compute # full lines per packet |
| 51 | + self.lpp = pktmax / self.width |
| 52 | + self.pktsize = self.lpp*self.width |
| 53 | + ##print 'lpp =', self.lpp, '; pktsize =', self.pktsize |
| 54 | + # Initialize capture |
| 55 | + v.SetSize(self.realwidth, self.realheight) |
| 56 | + dummy = v.InitContinuousCapture(SV.RGB8_FRAMES, \ |
| 57 | + self.realwidth, self.realheight, 2, 5) |
| 58 | + self.data = None |
| 59 | + self.lpos = 0 |
| 60 | + return self |
| 61 | + |
| 62 | + # Remove an instance. |
| 63 | + # This turns off continuous capture. |
| 64 | + |
| 65 | + def close(self): |
| 66 | + v.EndContinuousCapture() |
| 67 | + |
| 68 | + # Get the next video packet. |
| 69 | + # This returns (lpos, data) where: |
| 70 | + # - lpos is the line position |
| 71 | + # - data is a piece of data |
| 72 | + # The dimensions of data are: |
| 73 | + # - pixel depth = 1 byte |
| 74 | + # - scan line width = self.width (the vw argument to init()) |
| 75 | + # - number of scan lines = self.lpp (PKTMAX / vw) |
| 76 | + |
| 77 | + def getnextpacket(self): |
| 78 | + if not self.data: |
| 79 | + try: |
| 80 | + cd, id = v.GetCaptureData() |
| 81 | + except sv.error: |
| 82 | + return None |
| 83 | + data = cd.InterleaveFields(1) |
| 84 | + cd.UnlockCaptureData() |
| 85 | + self.data = imageop.crop(data, 1, \ |
| 86 | + self.realwidth, \ |
| 87 | + self.realheight, \ |
| 88 | + self.x0, self.y0, \ |
| 89 | + self.x1, self.y1) |
| 90 | + self.lpos = 0 |
| 91 | + data = self.data[:self.pktsize] |
| 92 | + self.data = self.data[self.pktsize:] |
| 93 | + lpos = self.lpos |
| 94 | + self.lpos = self.lpos + self.lpp |
| 95 | + return lpos, data |
0 commit comments