def setUpBuffer(self): if not self.available: return if not self.fmt: raise SystemError("Image Format not set!") self.reqbuf = v4l2.v4l2_requestbuffers() self.reqbuf.count = 1 self.reqbuf.type = self.fmt.type self.reqbuf.memory = v4l2.V4L2_MEMORY_MMAP success = fcntl.ioctl(self.device, v4l2.VIDIOC_REQBUFS, self.reqbuf) if success == -1: raise SystemError("Could not set up camera buffer") for index in range(self.reqbuf.count): buf = v4l2.v4l2_buffer() buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP buf.index = index fcntl.ioctl(self.device, v4l2.VIDIOC_QUERYBUF, buf) image = mmap.mmap(self.device.fileno(), buf.length, offset=buf.m.offset) self.imagebuffers.append(image) for index in range(self.reqbuf.count): buf = v4l2.v4l2_buffer() buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP buf.index = index fcntl.ioctl(self.device, v4l2.VIDIOC_QBUF, buf)
def Start(self, buff_num): if self.verbose: print '==== START ====' req = v4l2.v4l2_requestbuffers() req.count = buff_num req.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE req.memory = v4l2.V4L2_MEMORY_MMAP xioctl(self.vd, v4l2.VIDIOC_REQBUFS, req) if self.verbose: print '--requesetbuffers success' n_buffers = req.count self.buffers = list() for i in range(n_buffers): self.buffers.append(buffer_struct()) for i in range(n_buffers): buf = v4l2.v4l2_buffer() buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP buf.index = i xioctl(self.vd, v4l2.VIDIOC_QUERYBUF, buf) self.buffers[i].length = buf.length self.buffers[i].start = mmap.mmap(self.vd.fileno(), buf.length, flags=mmap.MAP_SHARED, prot=mmap.PROT_READ | mmap.PROT_WRITE, offset=buf.m.offset) if self.verbose: print '--Memory MAP' print ' +MMAP:', i, buf.m.offset, buf.length for i in range(n_buffers): buf = v4l2.v4l2_buffer() buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP buf.index = i xioctl(self.vd, v4l2.VIDIOC_QBUF, buf) if self.verbose: print '--requesetbuffers success' buftype = v4l2.v4l2_buf_type(v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE) xioctl(self.vd, v4l2.VIDIOC_STREAMON, buftype) time.sleep(1) buftype = v4l2.v4l2_buf_type(v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE) xioctl(self.vd, v4l2.VIDIOC_STREAMON, buftype) if self.verbose: print '**** START ****'
def init_mmap(self): req = v4l2.v4l2_requestbuffers() req.count = self.NUM_BUFFERS req.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE req.memory = v4l2.V4L2_MEMORY_MMAP try: ioctl(self.fd, v4l2.VIDIOC_REQBUFS, req) except Exception: raise Exception("video buffer request failed") if req.count < 2: raise Exception("Insufficient buffer memory on {}".format( self.device_name)) self.buffers = [] for x in range(req.count): buf = v4l2.v4l2_buffer() buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP buf.index = x ioctl(self.fd, v4l2.VIDIOC_QUERYBUF, buf) buf.buffer = mmap.mmap(self.fd, buf.length, mmap.PROT_READ, mmap.MAP_SHARED, offset=buf.m.offset) self.buffers.append(buf)
def init_mmap(self): req = v4l2.v4l2_requestbuffers() req.count = NUM_BUFFERS req.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE req.memory = v4l2.V4L2_MEMORY_MMAP try: ioctl(self.fd, v4l2.VIDIOC_REQBUFS, req) except Exception: raise Exception("video buffer request failed") if req.count < 2: raise Exception("Insufficient buffer memory on {}".format(self.device_name)) self.buffers = [] for x in range(req.count): buf = v4l2.v4l2_buffer() buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP buf.index = x ioctl(self.fd, v4l2.VIDIOC_QUERYBUF, buf) buf.buffer = mmap.mmap(self.fd, buf.length, mmap.PROT_READ, mmap.MAP_SHARED, offset=buf.m.offset) self.buffers.append(buf)
def __init__(self, width, height, dev): self.fd = os.open(dev, os.O_RDWR) cap = v4l2.v4l2_capability() fcntl.ioctl(self.fd, v4l2.VIDIOC_QUERYCAP, cap) fmt = v4l2.v4l2_format() fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE fmt.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_MJPEG fmt.fmt.pix.width = width fmt.fmt.pix.height = height fcntl.ioctl(self.fd, v4l2.VIDIOC_S_FMT, fmt) bufreq = v4l2.v4l2_requestbuffers() bufreq.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE bufreq.memory = v4l2.V4L2_MEMORY_MMAP bufreq.count = 1 fcntl.ioctl(self.fd, v4l2.VIDIOC_REQBUFS, bufreq) self.info = v4l2.v4l2_buffer() self.info.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE self.info.memory = v4l2.V4L2_MEMORY_MMAP self.info.index = 0 fcntl.ioctl(self.fd, v4l2.VIDIOC_QUERYBUF, self.info) self.buf = mmap.mmap( self.fd, self.info.length, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE, self.info.m.offset) self.type = ctypes.c_uint32(self.info.type)
def get_frame(self): #dqbuf buf = v4l2.v4l2_buffer() l.debug("dqbuf") buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP fcntl.ioctl(self.vd, v4l2.VIDIOC_DQBUF, buf) l.debug("process") #convert to numpy array mm = self.buffers[buf.index] frame = mm.read(self.framesize) img = fromstring(frame, dtype=uint16).reshape(self.H, self.W) #save RAW #out = open("image.raw", "wb") #out.write(frame) #out.close() mm.seek(0) #qbuf l.debug("qbuf") fcntl.ioctl(self.vd, v4l2.VIDIOC_QBUF, buf) return img
def _create_buffer(self, buffer_size=30): req = v4l2.v4l2_requestbuffers() req.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE req.memory = v4l2.V4L2_MEMORY_MMAP req.count = buffer_size # nr of buffer frames fcntl.ioctl(self.fd, v4l2.VIDIOC_REQBUFS, req) # tell the driver that we want some buffers self.buffers = [] for ind in range(req.count): # setup a buffer buf = v4l2.v4l2_buffer() buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP buf.index = ind fcntl.ioctl(self.fd, v4l2.VIDIOC_QUERYBUF, buf) mm = mmap.mmap(self.fd.fileno(), buf.length, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE, offset=buf.m.offset) self.buffers.append(mm) # queue the buffer for capture fcntl.ioctl(self.fd, v4l2.VIDIOC_QBUF, buf)
def _dqbuf(self): buf = v4l2.v4l2_buffer() buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP fcntl.ioctl(self, v4l2.VIDIOC_DQBUF, buf) return buf
def _alloc_buffers(self): req = v4l2.v4l2_requestbuffers() req.count = config.V4l2ReqBuffersCount req.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE req.memory = v4l2.V4L2_MEMORY_MMAP if fcntl.ioctl(self.camera, v4l2.VIDIOC_REQBUFS, req): print 'Failed to request buffers...' self.bufferscount = req.count print 'request buffers:%d' % self.bufferscount self._buffers = [] self._mmaps = [] for cnt in range(0, req.count): buf = v4l2.v4l2_buffer() self._buffers.append(buf) buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP buf.index = cnt if fcntl.ioctl(self.camera, v4l2.VIDIOC_QUERYBUF, buf): print 'failed to query buffers...' else: print 'index:%d, length:%d' % (buf.index, buf.length) self._mmaps.append( mmap.mmap(self.camera, buf.length, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE, offset=buf.m.offset)) fcntl.ioctl(self.camera, v4l2.VIDIOC_QBUF, buf)
def create_buffers(self, count): # Create buffers rbuf = v4l2.v4l2_requestbuffers() rbuf.count = count rbuf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE rbuf.memory = v4l2.V4L2_MEMORY_MMAP fcntl.ioctl(self, v4l2.VIDIOC_REQBUFS, rbuf) for i in range(rbuf.count): # Get buffer buf = v4l2.v4l2_buffer() buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP buf.index = i fcntl.ioctl(self, v4l2.VIDIOC_QUERYBUF, buf) # Mem map buffer mm = mmap.mmap(self.fileno(), buf.length, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE, offset=buf.m.offset) self.buffers.append(mm) # Queue the buffer for capture fcntl.ioctl(self, v4l2.VIDIOC_QBUF, buf)
def getFrame(self): if not self.available: return buf = v4l2.v4l2_buffer() buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP fcntl.ioctl(self.device, v4l2.VIDIOC_DQBUF, buf) data = self.imagebuffers[buf.index].read(buf.bytesused) self.imagebuffers[buf.index].seek(0) fcntl.ioctl(self.device, v4l2.VIDIOC_QBUF, buf) return self.addHuffman(data)
def _queue_all_buffers(self) -> None: """roughly equivalent to v4l2capture's queue_all_buffers""" assert not self.started assert len(self.bufmaps) != 0 for idx in range(len(self.bufmaps)): buf = v4l2.v4l2_buffer(type=v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE, index=idx, memory=v4l2.V4L2_MEMORY_MMAP) self._v4l2_ioctl(v4l2.VIDIOC_QBUF, buf)
def capture(self): select.select([self.fd], [], []) buf = v4l2.v4l2_buffer() buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP fcntl.ioctl(self.fd, v4l2.VIDIOC_DQBUF, buf) # get image from the driver queue mm = self.buffers[buf.index] result = mm.read(buf.bytesused) mm.seek(0) fcntl.ioctl(self.fd, v4l2.VIDIOC_QBUF, buf) # requeue the buffer return result
def _read_and_queue(self): """roughly equivalent to v4l2capture's read_and_queue""" assert self.started buf = v4l2.v4l2_buffer(type=v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE, memory=v4l2.V4L2_MEMORY_MMAP) self._v4l2_ioctl(v4l2.VIDIOC_DQBUF, buf) frame = self.bufmaps[buf.index].read(buf.bytesused) self.bufmaps[buf.index].seek(0) self._v4l2_ioctl(v4l2.VIDIOC_QBUF, buf) return frame
def capture(self): readable, writable, exceptional = select.select( [self.fd], [], [], 1.0) # Timeout 1 second if not (readable or writable or exceptional): return '' buf = v4l2.v4l2_buffer() buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP fcntl.ioctl(self.fd, v4l2.VIDIOC_DQBUF, buf) # get image from the driver queue mm = self.buffers[buf.index] result = mm.read(buf.bytesused) mm.seek(0) fcntl.ioctl(self.fd, v4l2.VIDIOC_QBUF, buf) # requeue the buffer return result
def GetFrame(self): start_time = time.time() buf = v4l2.v4l2_buffer() buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP ret = xioctl(self.vd, v4l2.VIDIOC_DQBUF, buf) buf_st = self.buffers[buf.index] buff = buf_st.start.read(buf_st.length) buf_st.start.seek(0) xioctl(self.vd, v4l2.VIDIOC_QBUF, buf) if self.verbose: print '# GetFrame:{0:.2f}ms'.format( (time.time() - start_time) * 1000) return buff
def _handle_frame(self): dqbuf = v4l2.v4l2_buffer() dqbuf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE dqbuf.memory = v4l2.V4L2_MEMORY_MMAP if fcntl.ioctl(self.camera, v4l2.VIDIOC_DQBUF, dqbuf): print 'Failed to get frame...index:%d' % dqbuf.index return False else: nparr = numpy.asarray(bytearray(self._mmaps[dqbuf.index]), numpy.uint8) if config.FormatPixelformat is v4l2.V4L2_PIX_FMT_YUYV: yuvarray = nparr.reshape((config.imageOperatingSize[1], config.imageOperatingSize[0], 2)) img = cv2.cvtColor(yuvarray, cv2.COLOR_YUV2BGR_YUYV) else: img = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR) self.currentFrame['content'] = img fcntl.ioctl(self.camera, v4l2.VIDIOC_QBUF, dqbuf) return True
def _setup_buffers(self, buf_count: int) -> None: """roughly equivalent to v4l2capture's create_buffers""" assert not self.started assert len(self.bufmaps) == 0 reqbuf = v4l2.v4l2_requestbuffers( type=v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE, count=buf_count, memory=v4l2.V4L2_MEMORY_MMAP) self._v4l2_ioctl(v4l2.VIDIOC_REQBUFS, reqbuf) assert reqbuf.count > 0 for idx in range(reqbuf.count): buf = v4l2.v4l2_buffer(type=v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE, index=idx, memory=v4l2.V4L2_MEMORY_MMAP) self._v4l2_ioctl(v4l2.VIDIOC_QUERYBUF, buf) self.bufmaps += [ mmap.mmap(self.dev_fd, buf.length, access=mmap.ACCESS_WRITE, offset=buf.m.offset) ]
def __init__(self): self.W = 1920 self.H = 1080 self.controls = {} #open l.debug("open") self.vd = open('/dev/video0', 'rb+', buffering=0) #queryctrl/g_ctrl qctrl = v4l2.v4l2_queryctrl() ctrl = v4l2.v4l2_control() #brightness qctrl.id = v4l2.V4L2_CID_BRIGHTNESS try: fcntl.ioctl(self.vd, v4l2.VIDIOC_QUERYCTRL, qctrl) ctrl.id = qctrl.id fcntl.ioctl(self.vd, v4l2.VIDIOC_G_CTRL, ctrl) except: l.error("QUERYCTRL/G_CTRL failed") self.controls["brightness"] = (ctrl.id, ctrl.value, qctrl.minimum, qctrl.maximum) #exposure qctrl.id = v4l2.V4L2_CID_EXPOSURE_ABSOLUTE try: fcntl.ioctl(self.vd, v4l2.VIDIOC_QUERYCTRL, qctrl) ctrl.id = qctrl.id fcntl.ioctl(self.vd, v4l2.VIDIOC_G_CTRL, ctrl) except: l.error("QUERYCTRL/G_CTRL failed") self.controls["exposure"] = (ctrl.id, ctrl.value, qctrl.minimum, qctrl.maximum) #querycap l.debug("querycap") cp = v4l2.v4l2_capability() fcntl.ioctl(self.vd, v4l2.VIDIOC_QUERYCAP, cp) #s_fmt l.debug("s_fmt") fmt = v4l2.v4l2_format() fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE fmt.fmt.pix.width = self.W fmt.fmt.pix.height = self.H fmt.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_SBGGR10 fmt.fmt.pix.field = v4l2.V4L2_FIELD_NONE fcntl.ioctl(self.vd, v4l2.VIDIOC_S_FMT, fmt) #g_fmt fmt = v4l2.v4l2_format() fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE fcntl.ioctl(self.vd, v4l2.VIDIOC_G_FMT, fmt) self.framesize = fmt.fmt.pix.sizeimage #reqbufs l.debug("reqbufs") req = v4l2.v4l2_requestbuffers() req.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE req.memory = v4l2.V4L2_MEMORY_MMAP req.count = 4 fcntl.ioctl(self.vd, v4l2.VIDIOC_REQBUFS, req) self.buffers = [] for ind in range(req.count): #querybufs buf = v4l2.v4l2_buffer() buf.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE buf.memory = v4l2.V4L2_MEMORY_MMAP buf.index = ind fcntl.ioctl(self.vd, v4l2.VIDIOC_QUERYBUF, buf) #mmap mm = mmap.mmap(self.vd.fileno(), buf.length, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE, offset=buf.m.offset) self.buffers.append(mm) #qbuf fcntl.ioctl(self.vd, v4l2.VIDIOC_QBUF, buf) #streamon l.debug("streamon") buf_type = v4l2.v4l2_buf_type(v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE) fcntl.ioctl(self.vd, v4l2.VIDIOC_STREAMON, buf_type) #select l.debug("select") t0 = time.time() max_t = 1 ready_to_read, ready_to_write, in_error = ([], [], []) while len(ready_to_read) == 0 and time.time() - t0 < max_t: ready_to_read, ready_to_write, in_error = select.select([self.vd], [], [], max_t)