def get_possible_resolutions(self):
        import v4l2
        from fcntl import ioctl

        fd = os.open(self.__device_path, os.O_RDWR)
        fmt = v4l2.v4l2_fmtdesc()
        fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        resolutions = set()

        # Special thanks to @Rirush for this solution
        while True:
            try:
                ioctl(fd, v4l2.VIDIOC_ENUM_FMT, fmt)
            except OSError:
                break
            frm = v4l2.v4l2_frmsizeenum()
            frm.pixel_format = fmt.pixelformat

            while True:
                try:
                    ioctl(fd, v4l2.VIDIOC_ENUM_FRAMESIZES, frm)
                except OSError:
                    break
                frm.index += 1
                resolutions.add((frm.discrete.width, frm.discrete.height))

            fmt.index += 1

        os.close(fd)
        return resolutions
Exemple #2
0
 def get_pixelformats(self):
     pixfmts = []
     fmtdesc = v4l2.v4l2_fmtdesc()
     fmtdesc.index = 0
     fmtdesc.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
     while True:
         try:
             fcntl.ioctl(self.vd, v4l2.VIDIOC_ENUM_FMT, fmtdesc)
             pixfmts.append((fmtdesc.pixelformat, fmtdesc.description))
             fmtdesc.index += 1
         except Exception as e:
             break
     return pixfmts
Exemple #3
0
def get_camera_pixelformats(device, index):
    vd = open(device)
    res = v4l2.v4l2_fmtdesc()
    res.type = 9
    res.index = index
    try:
        print(res.type)
        fcntl.ioctl(vd, v4l2.VIDIOC_ENUM_FMT, res)
        print('got it')
    except:
        res = None
    vd.close()
    return res
Exemple #4
0
 def get_pixfmt_cfg(self):
     ret, pixfmt = self.get_pixelformat()
     if pixfmt != v4l2.V4L2_PIX_FMT_Y16:
         return ArducamUtils.AUTO_CONVERT_TO_RGB
     fmtdesc = v4l2.v4l2_fmtdesc()
     fmtdesc.index = 0
     fmtdesc.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
     while True:
         try:
             fcntl.ioctl(self.vd, v4l2.VIDIOC_ENUM_FMT, fmtdesc)
             pixfmt = ArducamUtils.pixfmt_map.get(fmtdesc.pixelformat, None)
             if pixfmt != None:
                 return pixfmt
             fmtdesc.index += 1
         except Exception as e:
             break
     return ArducamUtils.AUTO_CONVERT_TO_RGB
Exemple #5
0
 def FormatDescript(self):
     dictFmtDiscript = dict()
     if self.verbose:
         print '--FormatDescript'
     fmt = v4l2.v4l2_fmtdesc()
     fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
     for i in range(20):  #while True:
         try:
             fmt.index = i
             xioctl(self.vd, v4l2.VIDIOC_ENUM_FMT, fmt)
         except:
             break
         if self.verbose:
             print '  + Description:', fmt.description
             print '  + PixelFormat:', hex(fmt.pixelformat)
         dictFmtDiscript[fmt.description] = fmt.pixelformat
     return dictFmtDiscript
Exemple #6
0
    def _configure_format(self):
        print 'Check format description ...'
        fmtdesc = v4l2.v4l2_fmtdesc()
        fmtdesc.index = 0
        fmtdesc.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        print 'Support format:'
        if not fcntl.ioctl(self.camera, v4l2.VIDIOC_ENUM_FMT, fmtdesc):
            print 'index:%d,type:%d,flags:%d,description:%s,pixelformat:%x\n' % (
                fmtdesc.index, fmtdesc.type, fmtdesc.flags,
                fmtdesc.description, fmtdesc.pixelformat)
            fmtdesc.index = fmtdesc.index + 1
        if not fcntl.ioctl(self.camera, v4l2.VIDIOC_ENUM_FMT, fmtdesc):
            print 'index:%d,type:%d,flags:%d,description:%s,pixelformat:%x\n' % (
                fmtdesc.index, fmtdesc.type, fmtdesc.flags,
                fmtdesc.description, fmtdesc.pixelformat)

        print 'Get current video format ...'
        video_format = v4l2.v4l2_format()
        video_format.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        if not fcntl.ioctl(self.camera, v4l2.VIDIOC_G_FMT, video_format):
            print 'type:%d,width:%d,height:%d,pixelformat:0x%x,field:%d\n' % (
                video_format.type, video_format.fmt.pix.width,
                video_format.fmt.pix.height, video_format.fmt.pix.pixelformat,
                video_format.fmt.pix.field)

        print 'Set current video format ...'
        video_format.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        video_format.fmt.pix.width = config.imageOperatingSize[0]
        video_format.fmt.pix.height = config.imageOperatingSize[1]
        video_format.fmt.pix.pixelformat = config.FormatPixelformat
        video_format.fmt.pix.field = config.FormatField

        if not fcntl.ioctl(self.camera, v4l2.VIDIOC_S_FMT, video_format):
            print 'Set format successfuly'

        print 'Check video format ...'
        video_format = v4l2.v4l2_format()
        video_format.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        if not fcntl.ioctl(self.camera, v4l2.VIDIOC_G_FMT, video_format):
            print 'type:%d,width:%d,height:%d,pixelfformat:0x%x,field:%d\n' % (
                video_format.type, video_format.fmt.pix.width,
                video_format.fmt.pix.height, video_format.fmt.pix.pixelformat,
                video_format.fmt.pix.field)
        self.sizeimage = video_format.fmt.pix.sizeimage
Exemple #7
0
    def get_formats(self):
        b = v4l2.v4l2_fmtdesc()
        b.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        b.index = 0

        l = []

        while True:
            try:
                fcntl.ioctl(self, v4l2.VIDIOC_ENUM_FMT, b)

                l.append((fourcc_to_string(b.pixelformat),
                          array_to_string(b.description)))

                b.index += 1

            except OSError: break

        return l
Exemple #8
0
    def __getPixelFormats(self, device, maxformats=5):
        """Query the camera to see what pixel formats it supports.  A list of
		dicts is returned consisting of format and description.  The caller
		should check whether this camera supports VIDEO_CAPTURE before
		calling this function.
		"""
        if '/dev/video' not in str(device):
            device = '/dev/video' + str(device)

        supported_formats = []
        fmt = v4l2.v4l2_fmtdesc()
        fmt.index = 0
        fmt.type = v4l2.V4L2_CAP_VIDEO_CAPTURE
        try:
            while fmt.index < maxformats:
                with open(device, 'r') as vd:
                    if fcntl.ioctl(vd, v4l2.VIDIOC_ENUM_FMT, fmt) == 0:
                        pixelformat = {}
                        # save the int type for re-use later
                        pixelformat['pixelformat_int'] = fmt.pixelformat
                        pixelformat['pixelformat'] = "%s%s%s%s" % \
                         (chr(fmt.pixelformat & 0xFF),
                         chr((fmt.pixelformat >> 8) & 0xFF),
                         chr((fmt.pixelformat >> 16) & 0xFF),
                         chr((fmt.pixelformat >> 24) & 0xFF))
                        if pixelformat['pixelformat'] != 'H264':
                            pixelformat[
                                'description'] = fmt.description.decode()
                            supported_formats.append(pixelformat)
                fmt.index = fmt.index + 1
        except IOError as e:
            # EINVAL is the ioctl's way of telling us that there are no
            # more formats, so we ignore it
            if e.errno != errno.EINVAL:
                self._logger.error(
                    "Unable to determine Pixel Formats, this may be a driver issue."
                )
                return None

            return supported_formats

        return supported_formats
Exemple #9
0
def getCameraFormats(number):
    name = '/dev/video{}'.format(number)
    formats = []
    try:
        with open(name, 'r') as fd:
            fmt = v4l2.v4l2_fmtdesc()
            fmt.index = 0
            fmt.type = v4l2.V4L2_CAP_VIDEO_CAPTURE
            while fcntl.ioctl(fd, v4l2.VIDIOC_ENUM_FMT, fmt) == 0:
                pixelformat = '{:c}{:c}{:c}{:c}'.format(
                    fmt.pixelformat & 255, (fmt.pixelformat >> 8) & 255,
                    (fmt.pixelformat >> 16) & 255, fmt.pixelformat >> 24)
                frmsize = v4l2.v4l2_frmsizeenum()
                frmsize.index = 0
                frmsize.pixel_format = fmt.pixelformat
                while fcntl.ioctl(fd, v4l2.VIDIOC_ENUM_FRAMESIZES,
                                  frmsize) == 0:
                    if frmsize.type != v4l2.V4L2_FRMSIZE_TYPE_DISCRETE:
                        break
                    width = frmsize.discrete.width
                    height = frmsize.discrete.height
                    frmival = v4l2.v4l2_frmivalenum()
                    frmival.index = 0
                    frmival.pixel_format = fmt.pixelformat
                    frmival.width = width
                    frmival.height = height
                    try:
                        while fcntl.ioctl(fd, v4l2.VIDIOC_ENUM_FRAMEINTERVALS,
                                          frmival) == 0:
                            if frmival.type != v4l2.V4L2_FRMIVAL_TYPE_DISCRETE:
                                break
                            fps = int(frmival.discrete.denominator /
                                      frmival.discrete.numerator)
                            formats.append((pixelformat, width, height, fps))
                            frmival.index += 1
                    except:
                        None
                    frmsize.index += 1
                fmt.index += 1
    except IOError as e:
        None
    return formats
Exemple #10
0
 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
Exemple #11
0
 def _configure_format(self):
     print 'Check format description ...'
     fmtdesc = v4l2.v4l2_fmtdesc()
     fmtdesc.index = 0
     fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE
     print 'Support format:'
     while True:
         ret, ioctl(self.camera, VIDIOC_ENUM_FMT, fmtdesc)
         if ret:
             print fmtdesc.index, fmtdesc.description, fmtdesc.pixelformat
             fmtdesc.index = fmtdesc.index + 1
         else:
             break
     print 'Set video format ...'
     video_format = v4l2.v4l2_format()
     video_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE
     video_format.fmt.pix.width = config.imageOperatingSize[0]
     video_format.fmt.pix.height = config.imageOperatingSize[1]
     video_format.fmt.pix.pixelfformat = V4L2_PIX_FMT_YUYV
     video_format.fmt.pix.field = V4L2_FIELD_INTERLACED
Exemple #12
0
	def __getPixelFormats(self, device, maxformats=5):
		"""Query the camera to see what pixel formats it supports.  A list of
		dicts is returned consisting of format and description.  The caller
		should check whether this camera supports VIDEO_CAPTURE before
		calling this function.
		"""
		if '/dev/video' not in str(device):
			device = '/dev/video' + str(device)

		supported_formats = []
		fmt = v4l2.v4l2_fmtdesc()
		fmt.index = 0
		fmt.type = v4l2.V4L2_CAP_VIDEO_CAPTURE
		try:
			with open(device, 'r') as vd:
				while fmt.index < maxformats:
					if fcntl.ioctl(vd, v4l2.VIDIOC_ENUM_FMT, fmt) == 0:
						pixelformat = {}
						# save the int type for re-use later
						pixelformat['pixelformat_int'] = fmt.pixelformat
						pixelformat['pixelformat'] = "%s%s%s%s" % \
							(chr(fmt.pixelformat & 0xFF),
							chr((fmt.pixelformat >> 8) & 0xFF),
							chr((fmt.pixelformat >> 16) & 0xFF),
							chr((fmt.pixelformat >> 24) & 0xFF))
						if pixelformat['pixelformat'] != 'H264':
							pixelformat['description'] = fmt.description.decode()
							supported_formats.append(pixelformat)
					fmt.index = fmt.index + 1
		except IOError as e:
			# EINVAL is the ioctl's way of telling us that there are no
			# more formats, so we ignore it
			if e.errno != errno.EINVAL:
				self._logger.error("Unable to determine Pixel Formats, this may be a driver issue.")
				return None

			return supported_formats

		return supported_formats
Exemple #13
0
 def check_format(self, format):
     capture = v4l2.v4l2_fmtdesc()
     capture.index = 0
     capture.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
     available = False
     try:
         while (ioctl(self.vd, v4l2.VIDIOC_ENUM_FMT, capture) >= 0):
             self.logger.debug("Checking format: {} : {}".format(format, capture.description.decode()))
             if format.lower() == "h264":
                 if re.search('H264', capture.description.decode().lower()) or re.search('H.264', capture.description.decode().lower()):
                     available = True
             elif format.lower() == "mjpeg":
                 if re.search('jpeg', capture.description.decode().lower()):
                     available = True
             elif format.lower() == "yuv" or format.lower() == "raw":
                 if re.search('^yu', capture.description.decode().lower()):
                     available = True
             else:
                 if re.search(format.lower(), capture.description.decode().lower()):
                     available = True
             capture.index += 1
     except:
         pass
     return available
Exemple #14
0
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()
Exemple #15
0
import cv2
import v4l2
import fcntl
import os
import time
from fcntl import ioctl
vd = open('/dev/video0', 'r')  # 打开视频设备文件
vd = os.open('/dev/video0', os.O_RDWR, 0)
cp = v4l2.v4l2_capability()  # 查询视频设备的能力
print(fcntl.ioctl(vd, v4l2.VIDIOC_QUERYCAP, cp))
print(cp.driver)  # 驱动名字
print(cp.card)  # 设备名字
fmt = v4l2.v4l2_fmtdesc()  # 查询视频设备的能力, 支持Motion-JPEG和YUYV4:2:2
fmt.index = 1
fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
print(fcntl.ioctl(vd, v4l2.VIDIOC_ENUM_FMT, fmt))
print(fmt.index, fmt.description)
print('------------------------------------------------')
# 控制曝光
ctrl = v4l2.v4l2_control()  # 括号漏了,各种报错...
ctrl.id = v4l2.V4L2_CID_EXPOSURE_AUTO

ctrl.value = v4l2.V4L2_EXPOSURE_MANUAL  # 只能是V4L2_EXPOSURE_MANUAL 或V4L2_EXPOSURE_APERTURE_PRIORITY
fcntl.ioctl(vd, v4l2.VIDIOC_S_CTRL, ctrl)
# print('exposure auto type: ', ctrl.value)
#
# ctrl1 = v4l2.v4l2_control()
# ctrl1.id = v4l2.V4L2_CID_EXPOSURE_ABSOLUTE
# ctrl1.value = 12
# fcntl.ioctl(vd, v4l2.VIDIOC_S_CTRL, ctrl1)
# fcntl.ioctl(vd, v4l2.VIDIOC_G_CTRL, ctrl1)
Exemple #16
0
 def _enum_pixel_formats(self):
     return self._enum_common(
         v4l2.VIDIOC_ENUM_FMT, lambda idx: v4l2.v4l2_fmtdesc(
             type=v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE,
             index=idx,
         ))