def get_device_controls(fd): # original enumeration method queryctrl = v4l2.v4l2_queryctrl(v4l2.V4L2_CID_BASE) while queryctrl.id < v4l2.V4L2_CID_LASTP1: try: fcntl.ioctl(fd, v4l2.VIDIOC_QUERYCTRL, queryctrl) except IOError, e: # this predefined control is not supported by this device assert e.errno == errno.EINVAL queryctrl.id += 1 continue yield queryctrl queryctrl = v4l2.v4l2_queryctrl(queryctrl.id + 1)
def _get_device_controls(self): # original enumeration method queryctrl = v4l2.v4l2_queryctrl(v4l2.V4L2_CID_BASE) while queryctrl.id < v4l2.V4L2_CID_LASTP1: try: ioctl(self.camera, v4l2.VIDIOC_QUERYCTRL, queryctrl) except IOError, e: # this predefined control is not supported by this device assert e.errno == errno.EINVAL queryctrl.id += 1 continue yield queryctrl queryctrl = v4l2.v4l2_queryctrl(queryctrl.id + 1)
def _set_device_controls(self): queryctrl = v4l2.v4l2_queryctrl() queryctrl.id = self.CtrlId if fcntl.ioctl(self.camera, v4l2.VIDIOC_QUERYCTRL, queryctrl): print 'queryctrl.id:0x%x is not supprotted' % (queryctrl.id) else: print 'queryctrl id:0x%x,type:%d,name:%s,minimum:%d,maximum:%d,step:%d\n' % ( queryctrl.id, queryctrl.type, queryctrl.name, queryctrl.minimum, queryctrl.maximum, queryctrl.step) ctrl = v4l2.v4l2_control() ctrl.id = self.CtrlId if fcntl.ioctl(self.camera, v4l2.VIDIOC_G_CTRL, ctrl): print 'ctrl.id:0x%x is not supprotted' % (ctrl.id) else: print 'Get current control...id:0x%x,valus:%d\n' % (ctrl.id, ctrl.value) ctrl = v4l2.v4l2_control() ctrl.id = self.CtrlId ctrl.value = self.CtrlValue if fcntl.ioctl(self.camera, v4l2.VIDIOC_S_CTRL, ctrl): print 'ctrl.id:0x%x is not supprotted' % (ctrl.id) else: print 'Set control...id:0x%x,valus:%d\n' % (ctrl.id, ctrl.value) ctrl = v4l2.v4l2_control() ctrl.id = self.CtrlId if fcntl.ioctl(self.camera, v4l2.VIDIOC_G_CTRL, ctrl): print 'ctrl.id:0x%x is not supprotted' % (ctrl.id) else: print 'Check current control...id:0x%x,valus:%d\n' % (ctrl.id, ctrl.value)
def get_device_controls_by_class(fd, control_class): # enumeration by control class queryctrl = v4l2.v4l2_queryctrl(control_class | v4l2.V4L2_CTRL_FLAG_NEXT_CTRL) while True: try: fcntl.ioctl(fd, v4l2.VIDIOC_QUERYCTRL, queryctrl) except IOError, e: assert e.errno == errno.EINVAL break if (v4l2.V4L2_CTRL_ID2CLASS(queryctrl.id) != control_class): break yield queryctrl queryctrl = v4l2.v4l2_queryctrl(queryctrl.id | v4l2.V4L2_CTRL_FLAG_NEXT_CTRL)
def get_device_controls_by_class(fd, control_class): # enumeration by control class queryctrl = v4l2.v4l2_queryctrl( control_class | v4l2.V4L2_CTRL_FLAG_NEXT_CTRL) while True: try: ioctl(fd, v4l2.VIDIOC_QUERYCTRL, queryctrl) except IOError, e: assert e.errno == errno.EINVAL break if (v4l2.V4L2_CTRL_ID2CLASS(queryctrl.id) != control_class): break yield queryctrl queryctrl = v4l2.v4l2_queryctrl( queryctrl.id | v4l2.V4L2_CTRL_FLAG_NEXT_CTRL)
def camera_info(self): # Log capability info cp = v4l2.v4l2_capability() ioctl(self.vd, v4l2.VIDIOC_QUERYCAP, cp) self.logger.debug("driver: " + cp.driver.decode()) self.logger.debug("card: " + cp.card.decode()) self.driver = cp.driver.decode() self.card = cp.card.decode() # Log controls available queryctrl = v4l2.v4l2_queryctrl(v4l2.V4L2_CID_BASE) while queryctrl.id < v4l2.V4L2_CID_LASTP1: try: ioctl(self.vd, v4l2.VIDIOC_QUERYCTRL, queryctrl) except IOError as e: # this predefined control is not supported by this device assert e.errno == errno.EINVAL queryctrl.id += 1 continue self.logger.debug("Camera control: " + queryctrl.name.decode()) queryctrl = v4l2.v4l2_queryctrl(queryctrl.id + 1) queryctrl.id = v4l2.V4L2_CID_PRIVATE_BASE while True: try: ioctl(self.vd, v4l2.VIDIOC_QUERYCTRL, queryctrl) except IOError as e: # no more custom controls available on this device assert e.errno == errno.EINVAL break self.logger.debug("Camera control: " + queryctrl.name.decode()) queryctrl = v4l2.v4l2_queryctrl(queryctrl.id + 1) # Log formats available capture = v4l2.v4l2_fmtdesc() capture.index = 0 capture.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE try: while (ioctl(self.vd, v4l2.VIDIOC_ENUM_FMT, capture) >= 0): self.logger.debug("Camera format: " + capture.description.decode()) capture.index += 1 except: pass
def ex_1_8_enum_ctrls(): print print print print 'ex_1_8_enum_ctrls()' queryctrl = v4l2.v4l2_queryctrl() querymenu = v4l2.v4l2_querymenu() print dir(querymenu) print querymenu.id def enumerate_menu(): print " Menu items:" querymenu.id = queryctrl.id for querymenu.index in xrange(queryctrl.minimum, queryctrl.maximum + 1): if 0 == fcntl.ioctl(vd, v4l2.VIDIOC_QUERYCAP, querymenu): print " %s" % querymenu.name for queryctrl.id in xrange(v4l2.V4L2_CID_BASE, v4l2.V4L2_CID_LASTP1): break print 'loop: %d' % queryctrl.id if 0 == fcntl.ioctl(vd, v4l2.VIDIOC_QUERYCTRL, queryctrl): if queryctrl.flags & v4l2.V4L2_CTRL_FLAG_DISABLED: continue print "Control %s" % queryctrl.name if queryctrl.type == v4l2.V4L2_CTRL_TYPE_MENU: enumerate_menu() else: #if errno == EINVAL: # continue; #perror("VIDIOC_QUERYCTRL"); print "VIDIOC_QUERYCTRL" sys.exit(1) queryctrl.id = v4l2.V4L2_CID_PRIVATE_BASE while True: if 0 != fcntl.ioctl(vd, v4l2.VIDIOC_QUERYCTRL, queryctrl): # how to check this? #if (errno == EINVAL) # break; # perror("VIDIOC_QUERYCTRL"); print "VIDIOC_QUERYCTRL" sys.exit(1) if not (queryctrl.flags & v4l2.V4L2_CTRL_FLAG_DISABLED): print "Control %s" % queryctrl.name if queryctrl.type == v4l2.V4L2_CTRL_TYPE_MENU: enumerate_menu() queryctrl.id += 1
def query_v4l2_ctrls(dev): ctrls = [] ctrl = v4l2.v4l2_queryctrl() ctrl.id = v4l2.V4L2_CID_BASE while True: try: fcntl.ioctl(dev, v4l2.VIDIOC_QUERYCTRL, ctrl) except Exception: return ctrls if not ctrl.flags & v4l2.V4L2_CTRL_FLAG_DISABLED and \ ctrl.type in \ SUPPORTED_CTRL_TYPES: ctrls.append(ctrl) ctrl = v4l2.v4l2_queryctrl() ctrl.id = ctrls[-1].id ctrl.id |= v4l2.V4L2_CTRL_FLAG_NEXT_CTRL return ctrls
def EnumerateControls(self): self.dictValidControls = dict() qc = v4l2.v4l2_queryctrl() qc.id = v4l2.V4L2_CTRL_CLASS_USER | v4l2.V4L2_CTRL_FLAG_NEXT_CTRL if self.verbose: print '--EnumerateControls' for i in range(20): #while True: try: xioctl(self.vd, v4l2.VIDIOC_QUERYCTRL, qc) except: break if self.verbose: print ' +', i, ':', qc.id, qc.name, qc.type, qc.flags if qc.flags == v4l2.V4L2_CTRL_FLAG_DISABLED: continue if qc.type == v4l2.V4L2_CTRL_TYPE_MENU: print 'sdkim Todo enumerate_control_menu() ' self.dictValidControls[qc.name] = qc.id qc.id |= v4l2.V4L2_CTRL_FLAG_NEXT_CTRL return self.dictValidControls
def read_camera_controls( ): # CID_BASE IDs do not include all settings that v4l2-ctl -L has, check CAMERA_CLASS_BASE ID range for the rest vd = open('/dev/video0', 'rb+', buffering=0) encoding = 'utf-8' qctrl = v4l2.v4l2_queryctrl() mctrl = v4l2.v4l2_querymenu() vctrl = v4l2.v4l2_control() qctrl.id = v4l2.V4L2_CID_CAMERA_CLASS_BASE mctrl.index = 0 while qctrl.id < v4l2.V4L2_CID_PRIVACY: try: vctrl.id = qctrl.id fcntl.ioctl(vd, v4l2.VIDIOC_QUERYCTRL, qctrl) fcntl.ioctl(vd, v4l2.VIDIOC_G_CTRL, vctrl) print("Control name:", str(qctrl.name, encoding)) print("Control type, 1=int, 2=bool, 3=menu:", qctrl.type) print("Maximum:", qctrl.maximum) print("Minimum:", qctrl.minimum) print("Step:", qctrl.step) print("Default:", qctrl.default_value) print("Value:", vctrl.value) if qctrl.type == 3: # is menu while mctrl.index <= qctrl.maximum: try: # needed because sometimes index 0 doesn't exist but 1 does mctrl.id = qctrl.id fcntl.ioctl(vd, v4l2.VIDIOC_QUERYMENU, mctrl) print("Menu name:", str(qctrl.name, encoding)) print("Menu option name:", str(mctrl.name, encoding)) print("Menu option index:", mctrl.index) mctrl.index += 1 except: mctrl.index += 1 except: pass qctrl.id += 1 vd.close()
def read_base_capabalities(): vd = open('/dev/video0', 'rb+', buffering=0) cp = v4l2.v4l2_capability() encoding = 'utf-8' # basic info fcntl.ioctl(vd, v4l2.VIDIOC_QUERYCAP, cp) print(str(cp.card, encoding)) print(str(cp.driver, encoding)) print("video capture device?\t", bool(cp.capabilities & v4l2.V4L2_CAP_VIDEO_CAPTURE)) print("Supports read() call?\t", bool(cp.capabilities & v4l2.V4L2_CAP_READWRITE)) print("Supports streaming?\t", bool(cp.capabilities & v4l2.V4L2_CAP_STREAMING)) # current height, width qctrl = v4l2.v4l2_format() qctrl.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE fcntl.ioctl(vd, v4l2.VIDIOC_G_FMT, qctrl) print("Width:", qctrl.fmt.pix.width) print("Height:", qctrl.fmt.pix.height) # output overview qctrl = v4l2.v4l2_fmtdesc() qctrl.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE qctrl.index = 0 fcntl.ioctl(vd, v4l2.VIDIOC_ENUM_FMT, qctrl) print("Format:", str(qctrl.description, encoding)) print("Pixelformat ID:", qctrl.pixelformat) get_outputs( qctrl.pixelformat ) # pass pixelformat to read outputs, increase index for different codecs # main controls qctrl = v4l2.v4l2_queryctrl() mctrl = v4l2.v4l2_querymenu() vctrl = v4l2.v4l2_control() qctrl.id = v4l2.V4L2_CID_BASE mctrl.index = 0 while qctrl.id < v4l2.V4L2_CID_LASTP1: # LASTP1 is last item in CID_BASE try: vctrl.id = qctrl.id fcntl.ioctl(vd, v4l2.VIDIOC_QUERYCTRL, qctrl) fcntl.ioctl(vd, v4l2.VIDIOC_G_CTRL, vctrl) print("Control name:", str(qctrl.name, encoding)) print("Control type, 1=int, 2=bool, 3=menu:", qctrl.type) ''' There are more types, 4=BUTTON, 5=INTEGER64, 6=CTRL_CLASS, 7=STRING, 8=BITMASK, 9=INTEGER_MENU Not sure what to do with those, can't test ''' print("Maximum:", qctrl.maximum) print("Minimum:", qctrl.minimum) print("Step:", qctrl.step) print("Default:", qctrl.default_value) print("Value:", vctrl.value) set_value(vctrl.id) # test setting value ''' if qctrl.type == 1: # int win.label = Gtk.Label(hexpand = True, vexpand = False) win.label.set_text(str(qctrl.name, encoding)) win.label.set_size_request(-1, 35) win.label.set_halign(Gtk.Align.END) adj = Gtk.Adjustment(value = vctrl.value, lower = qctrl.minimum, upper = qctrl.maximum, step_increment = qctrl.step, page_increment = 5, page_size=0) win.scale = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL, adjustment=adj) win.scale.set_digits(0) win.scale.set_value_pos(Gtk.PositionType.RIGHT) win.scale.set_size_request(-1, 35) win.scale.connect("value-changed", set_int_value, card, vctrl.id) win.intcontrolbox.pack_start(win.scale, False, False, 0) win.intlabelbox.pack_start(win.label, False, False, 0) ''' if qctrl.type == 3: # is menu while mctrl.index <= qctrl.maximum: try: mctrl.id = qctrl.id fcntl.ioctl(vd, v4l2.VIDIOC_QUERYMENU, mctrl) print("Menu name:", str(qctrl.name, encoding)) print("Menu option name:", str(mctrl.name, encoding)) print("Menu option index:", mctrl.index) mctrl.index += 1 except: mctrl.index += 1 except: pass qctrl.id += 1 vd.close()
assert e.errno == errno.EINVAL queryctrl.id += 1 continue yield queryctrl queryctrl = v4l2.v4l2_queryctrl(queryctrl.id + 1) queryctrl.id = v4l2.V4L2_CID_PRIVATE_BASE while True: try: ioctl(self.camera, v4l2.VIDIOC_QUERYCTRL, queryctrl) except IOError, e: # no more custom controls available on this device assert e.errno == errno.EINVAL break yield queryctrl queryctrl = v4l2.v4l2_queryctrl(queryctrl.id + 1) def _init_camera(self): print 'Initialize camera devices ...' self._open_devices() def start(self): self.gameOn = True self._init_camera() while self.gameOn: ret, self.currentFrame['content'] = self.camera.read() if ret: self.currentFrame['content'] = cv2.resize( src=self.currentFrame['content'], dsize=config.imageOperatingSize)
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)
def __init__(self, primary_text, v4l2_control = False, v4l2_auto_control = False): Palette.__init__(self, label=primary_text) vbox = gtk.VBox() self.set_content(vbox) vbox.show() if v4l2_control: self._query_control = v4l2.v4l2_queryctrl(v4l2_control) self._control = v4l2.v4l2_control(v4l2_control) ioctl(VD, v4l2.VIDIOC_QUERYCTRL, self._query_control) ioctl(VD, v4l2.VIDIOC_G_CTRL, self._control) _max = self._query_control.maximum _min = self._query_control.minimum if v4l2_control == v4l2.V4L2_CID_EXPOSURE: _min = 0 _max = 512 elif v4l2_control == v4l2.V4L2_CID_GAIN: _min = 0 _max = 37 elif v4l2_control == v4l2.V4L2_CID_CONTRAST: _min = 0 _max = 127 elif v4l2_control == v4l2.V4L2_CID_BRIGHTNESS: _min = 0 _max = 255 #elif v4l2_control == v4l2.V4L2_CID_NIGHT_MODE: # _min = 0 # _max = 1 self._adjustment = gtk.Adjustment(value=self._control.value, lower=_min, upper=_max, step_incr=1, page_incr=1, page_size=0) self._hscale = gtk.HScale(self._adjustment) self._hscale.set_digits(0) self._hscale.set_draw_value(False) self._hscale.set_update_policy(gtk.UPDATE_DISCONTINUOUS) vbox.add(self._hscale) self._hscale.show() self._adjustment_handler_id = \ self._adjustment.connect('value_changed', self.__adjustment_changed_cb) if v4l2_auto_control: self._auto_query_control =\ v4l2.v4l2_queryctrl(v4l2_auto_control) self._auto_control = v4l2.v4l2_control(v4l2_auto_control) self._auto_button = gtk.CheckButton('Auto') self._auto_button.set_active(self._auto_control.value) self._auto_button.connect('toggled', self.__auto_button_toggled_cb) vbox.add(self._auto_button) self._auto_button.show() if self._auto_control.value == True: self._hscale.set_sensitive(False) vbox.show()
assert e.errno == errno.EINVAL queryctrl.id += 1 continue yield queryctrl queryctrl = v4l2.v4l2_queryctrl(queryctrl.id + 1) queryctrl.id = v4l2.V4L2_CID_PRIVATE_BASE while True: try: fcntl.ioctl(fd, v4l2.VIDIOC_QUERYCTRL, queryctrl) except IOError, e: # no more custom controls available on this device assert e.errno == errno.EINVAL break yield queryctrl queryctrl = v4l2.v4l2_queryctrl(queryctrl.id + 1) ''' Control: Red Balance Range: 0 - 1023 Control: Blue Balance Range: 0 - 1023 Control: Gain Range: 0 - 511 Control: Exposure Range: 0 - 800 ''' # Return name of all controls def ctrls(fd): ret = []