Beispiel #1
0
def initialize_loopback_device(loopback_device,
                               width,
                               height,
                               channels,
                               pixel_format=None):
    import v4l2

    if os.path.exists(loopback_device):
        device = open(loopback_device, "wb", 0)
    else:
        msg = "Cannot open video device %s, path do not exist. " % loopback_device
        msg += "Make sure that the v4l2loopback kernel module is loaded (modprobe v4l2loopback). "
        msg += "Falling back to MJPEG."
        raise RuntimeError(msg)

    f = v4l2.v4l2_format()
    f.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
    f.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_RGB24
    f.fmt.pix.width = width
    f.fmt.pix.height = height
    f.fmt.pix.field = v4l2.V4L2_FIELD_NONE
    f.fmt.pix.bytesperline = width * channels
    f.fmt.pix.sizeimage = width * height * channels
    f.fmt.pix.colorspace = v4l2.V4L2_COLORSPACE_SRGB

    res = fcntl.ioctl(device, v4l2.VIDIOC_S_FMT, f)

    if res != 0:
        raise RuntimeError("Could not initialize video device: %d" % res)

    return device
Beispiel #2
0
def send_loopback(images):
    d=os.open("/dev/video1", os.O_RDWR)
    cap=v.v4l2_capability()
    ioctl(d, v.VIDIOC_QUERYCAP, cap)
    vid_format=v.v4l2_format()
    #ioctl(d, v.VIDIOC_G_FMT, vid_format)
    vid_format.type=v.V4L2_BUF_TYPE_VIDEO_OUTPUT
    vid_format.fmt.pix.width=640
    #vid_format.fmt.pix.sizeimage=1036800
    vid_format.fmt.pix.height=480
    vid_format.fmt.pix.pixelformat=v.V4L2_PIX_FMT_RGB24
    vid_format.fmt.pix.field=v.V4L2_FIELD_NONE
    vid_format.fmt.pix.colorspace=v.V4L2_COLORSPACE_SRGB
    ioctl(d, v.VIDIOC_S_FMT, vid_format)
    print "frame size", vid_format.fmt.pix.sizeimage, len(images[0][0]), images[0][1]
    raw_input()
    counter=0
    old_t=time()
    fps_period=1./29.97
    while True:
        counter+=1
        #print "Image", counter
        for img, size in images:
            t=time()
            delta_time=t-old_t
            print "Delta", delta_time,
            old_t=t
            if delta_time<fps_period:
                print "sleeping a bit"
                sleep(fps_period-delta_time)
            os.write(d, img)
    async def run(self, args):
        # Open camera driver
        fd = open('/dev/video0', 'wb')

        BUFTYPE = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
        MEMTYPE = v4l2.V4L2_MEMORY_MMAP
        FRAME_FORMAT = v4l2.V4L2_PIX_FMT_RGB565

        # Set format
        width = 640
        height = 480
        sizeimage = width * height * 2
        linewidth = 640

        fmt = v4l2.v4l2_format()
        fmt.type = BUFTYPE
        fmt.fmt.pix.width = width
        fmt.fmt.pix.height = height
        fmt.fmt.pix.pixelformat = FRAME_FORMAT
        fmt.fmt.pix.sizeimage = sizeimage
        fmt.fmt.pix.field = v4l2.V4L2_FIELD_NONE
        fmt.fmt.pix.bytesperline = linewidth
        fmt.fmt.pix.colorspace = v4l2.V4L2_COLORSPACE_SRGB

        ret = fcntl.ioctl(fd, v4l2.VIDIOC_S_FMT, fmt)
        print("fcntl.ioctl(fd, v4l2.VIDIOC_S_FMT, fmt) = %d" % ret)

        buffer_size = fmt.fmt.pix.sizeimage
        print("buffer_size = " + str(buffer_size))

        print("Init ft60x driver")
        self.ftd3xx = FTD3xxWrapper()
        read_bytes = self.consumer_fn(self.ftd3xx, fd)
Beispiel #4
0
def send_loopback(images):
    d = os.open("/dev/video1", os.O_RDWR)
    cap = v.v4l2_capability()
    ioctl(d, v.VIDIOC_QUERYCAP, cap)
    vid_format = v.v4l2_format()
    #ioctl(d, v.VIDIOC_G_FMT, vid_format)
    vid_format.type = v.V4L2_BUF_TYPE_VIDEO_OUTPUT
    vid_format.fmt.pix.width = 640
    #vid_format.fmt.pix.sizeimage=1036800
    vid_format.fmt.pix.height = 480
    vid_format.fmt.pix.pixelformat = v.V4L2_PIX_FMT_RGB24
    vid_format.fmt.pix.field = v.V4L2_FIELD_NONE
    vid_format.fmt.pix.colorspace = v.V4L2_COLORSPACE_SRGB
    ioctl(d, v.VIDIOC_S_FMT, vid_format)
    print "frame size", vid_format.fmt.pix.sizeimage, len(
        images[0][0]), images[0][1]
    raw_input()
    counter = 0
    old_t = time()
    fps_period = 1. / 29.97
    while True:
        counter += 1
        #print "Image", counter
        for img, size in images:
            t = time()
            delta_time = t - old_t
            print "Delta", delta_time,
            old_t = t
            if delta_time < fps_period:
                print "sleeping a bit"
                sleep(fps_period - delta_time)
            os.write(d, img)
Beispiel #5
0
    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)
Beispiel #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
Beispiel #7
0
    def set_format(self, width, height, fourcc):
        fmt = v4l2.v4l2_format()
        fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        fcntl.ioctl(self, v4l2.VIDIOC_G_FMT, fmt)

        fmt.fmt.pix.width = width
        fmt.fmt.pix.height = height
        fmt.fmt.pix.pixelformat = fourcc

        fcntl.ioctl(self, v4l2.VIDIOC_S_FMT, fmt)
 def _set_resolution(self, width, height, pixelformat='MJPG'):
     fmt = v4l2.v4l2_format()
     fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
     fcntl.ioctl(self.fd, v4l2.VIDIOC_G_FMT, fmt)  # get current settings
     fmt.fmt.pix.width = width
     fmt.fmt.pix.height = height
     fourcc = (ord(pixelformat[0])) | (ord(pixelformat[1]) << 8) | (
         ord(pixelformat[2]) << 16) | (ord(pixelformat[3]) << 24)
     fmt.fmt.pix.pixelformat = fourcc
     fcntl.ioctl(self.fd, v4l2.VIDIOC_S_FMT, fmt)
Beispiel #9
0
	def setImageFormat(self):
		if not self.available: return
		self.fmt = v4l2.v4l2_format()
		self.fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
		self.fmt.fmt.pix.width = 160
		self.fmt.fmt.pix.height = 120
		self.fmt.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_MJPEG
		self.fmt.fmt.pix.field = v4l2.V4L2_FIELD_NONE
		success = fcntl.ioctl(self.device, v4l2.VIDIOC_S_FMT, self.fmt)
		if success == -1:
			raise SystemError("Camera does not support image format!")
Beispiel #10
0
def setup_fromat(width, height, channels):
    # Set up the formatting of our loopback device
    format = v4l2.v4l2_format()
    format.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
    format.fmt.pix.field = v4l2.V4L2_FIELD_NONE
    format.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_YUV420
    format.fmt.pix.width = width
    format.fmt.pix.height = height
    format.fmt.pix.bytesperline = width * channels
    format.fmt.pix.sizeimage = width * height * channels
    return format
Beispiel #11
0
    def __init__(self,
                 video_device,
                 width,
                 height,
                 channels=3,
                 input_pixfmt='RGB'):

        if channels != 3:
            raise NotImplementedError(
                'Code only supports inputs with 3 channels right now. You tried to intialize with {} channels'
                .format(channels))

        if input_pixfmt != 'RGB':
            raise NotImplementedError(
                'Code only supports RGB pixfmt. You tried to intialize with {}'
                .format(input_pixfmt))

        if not os.path.exists(video_device):
            sys.stderr.write(
                '\n--- Make sure the v4l2loopback kernel module is loaded ---\n'
            )
            sys.stderr.write('sudo modprobe v4l2loopback devices=1\n\n')
            raise FileNotFoundError(
                'device does not exist: {}'.format(video_device))

        self._channels = channels
        self._video_device = os.open(video_device, os.O_WRONLY | os.O_SYNC)

        self._settings = _v4l2.v4l2_format()
        self._settings.type = _v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
        self._settings.fmt.pix.pixelformat = _v4l2.V4L2_PIX_FMT_YUYV
        self._settings.fmt.pix.width = width
        self._settings.fmt.pix.height = height
        self._settings.fmt.pix.field = _v4l2.V4L2_FIELD_NONE
        self._settings.fmt.pix.bytesperline = width * 2
        self._settings.fmt.pix.sizeimage = width * height * 2
        self._settings.fmt.pix.colorspace = _v4l2.V4L2_COLORSPACE_JPEG

        self._buffer = np.zeros(
            (self._settings.fmt.pix.height, 2 * self._settings.fmt.pix.width),
            dtype=np.uint8)

        self._yuv = np.zeros(
            (self._settings.fmt.pix.height, self._settings.fmt.pix.width, 3),
            dtype=np.uint8)
        self._ones = np.ones(
            (self._settings.fmt.pix.height, self._settings.fmt.pix.width, 1),
            dtype=np.uint8)
        self._rgb2yuv = np.array([[0.299, 0.587, 0.114, 0],
                                  [-0.168736, -0.331264, 0.5, 128],
                                  [0.5, -0.418688, -0.081312, 128]])

        fcntl.ioctl(self._video_device, _v4l2.VIDIOC_S_FMT, self._settings)
Beispiel #12
0
 def _set_device_format(self):
     fmt = v4l2.v4l2_format()
     fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
     fmt.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_BGR32
     fmt.fmt.pix.width = self.width
     fmt.fmt.pix.height = self.height
     fmt.fmt.pix.field = v4l2.V4L2_FIELD_NONE
     fmt.fmt.pix.bytesperline = fmt.fmt.pix.width * 4
     fmt.fmt.pix.sizeimage = fmt.fmt.pix.width * fmt.fmt.pix.height * 4
     fmt.fmt.pix.colorspace = v4l2.V4L2_COLORSPACE_SRGB
     ret_code = fcntl.ioctl(self._device_fd, v4l2.VIDIOC_S_FMT, fmt)
     if ret_code == -1:
         fail_with_error("Unable to get device capabilities")
 def init_device(self):
     cap = v4l2.v4l2_capability()
     fmt = v4l2.v4l2_format()
     
     ioctl(self.fd, v4l2.VIDIOC_QUERYCAP, cap)
     
     if not (cap.capabilities & v4l2.V4L2_CAP_VIDEO_CAPTURE):
         raise Exception("{} is not a video capture device".format(self.device_name))
     
     fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
     ioctl(self.fd, v4l2.VIDIOC_G_FMT, fmt)
     
     self.init_mmap()
Beispiel #14
0
    def _verify_capabilities(self) -> Tuple[int, int]:
        fmt = v4l2.v4l2_format(type=v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE)
        self._v4l2_ioctl(v4l2.VIDIOC_G_FMT, fmt)

        # ensure that the device supports 'JFIF JPEG' format video capture
        assert fmt.fmt.pix.pixelformat == v4l2.V4L2_PIX_FMT_JPEG

        # sanity-check the allegedly supported camera width and height
        assert fmt.fmt.win.w.left > 0 and fmt.fmt.win.w.left <= 10240
        assert fmt.fmt.win.w.top > 0 and fmt.fmt.win.w.top <= 10240

        # return supported resolution
        return (fmt.fmt.win.w.top, fmt.fmt.win.w.left)
    def init_device(self):
        cap = v4l2.v4l2_capability()
        fmt = v4l2.v4l2_format()

        ioctl(self.fd, v4l2.VIDIOC_QUERYCAP, cap)

        if not (cap.capabilities & v4l2.V4L2_CAP_VIDEO_CAPTURE):
            raise Exception("{} is not a video capture device".format(
                self.device_name))

        fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        ioctl(self.fd, v4l2.VIDIOC_G_FMT, fmt)

        self.init_mmap()
Beispiel #16
0
def set_video_output(device, height, width, channels):
    import v4l2
    import fcntl
    format = v4l2.v4l2_format()
    format.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
    format.fmt.pix.field = v4l2.V4L2_FIELD_NONE
    format.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_BGR24
    format.fmt.pix.width = width
    format.fmt.pix.height = height
    format.fmt.pix.bytesperline = width * channels
    format.fmt.pix.sizeimage = width * height * channels

    print("set format result (0 is good):{}".format(
        fcntl.ioctl(device, v4l2.VIDIOC_S_FMT, format)))
    return device
Beispiel #17
0
 def v4l_init(self):
     self.d=os.open(self.v4l_device, os.O_RDWR)
     cap=v.v4l2_capability()
     ioctl(self.d, v.VIDIOC_QUERYCAP, cap)
     vid_format=v.v4l2_format()
     #ioctl(d, v.VIDIOC_G_FMT, vid_format)
     vid_format.type=v.V4L2_BUF_TYPE_VIDEO_OUTPUT
     vid_format.fmt.pix.width=720
     vid_format.fmt.pix.sizeimage=1036800
     vid_format.fmt.pix.height=480
     vid_format.fmt.pix.pixelformat=v.V4L2_PIX_FMT_YUYV
     vid_format.fmt.pix.field=v.V4L2_FIELD_NONE
     vid_format.fmt.pix.colorspace=v.V4L2_COLORSPACE_SRGB
     ioctl(self.d, v.VIDIOC_S_FMT, vid_format)
     print "frame size", vid_format.fmt.pix.sizeimage, len(self.s_packets)
     self.old_t=time()
Beispiel #18
0
 def v4l_init(self):
     self.d = os.open(self.v4l_device, os.O_RDWR)
     cap = v.v4l2_capability()
     ioctl(self.d, v.VIDIOC_QUERYCAP, cap)
     vid_format = v.v4l2_format()
     #ioctl(d, v.VIDIOC_G_FMT, vid_format)
     vid_format.type = v.V4L2_BUF_TYPE_VIDEO_OUTPUT
     vid_format.fmt.pix.width = 720
     vid_format.fmt.pix.sizeimage = 1036800
     vid_format.fmt.pix.height = 480
     vid_format.fmt.pix.pixelformat = v.V4L2_PIX_FMT_YUYV
     vid_format.fmt.pix.field = v.V4L2_FIELD_NONE
     vid_format.fmt.pix.colorspace = v.V4L2_COLORSPACE_SRGB
     ioctl(self.d, v.VIDIOC_S_FMT, vid_format)
     print "frame size", vid_format.fmt.pix.sizeimage, len(self.s_packets)
     self.old_t = time()
    def _initialize_video_device(self, pixel_format, width, height, channels):
        f = v4l2.v4l2_format()
        f.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
        f.fmt.pix.pixelformat = pixel_format
        f.fmt.pix.width = width
        f.fmt.pix.height = height
        f.fmt.pix.field = v4l2.V4L2_FIELD_NONE
        f.fmt.pix.bytesperline = width * channels
        f.fmt.pix.sizeimage = width * height * channels
        f.fmt.pix.colorspace = v4l2.V4L2_COLORSPACE_SRGB

        res = fcntl.ioctl(self.video_device, v4l2.VIDIOC_S_FMT, f)

        if res != 0:
            raise RuntimeError("Could not initialize video device: %d" % res)

        return True
Beispiel #20
0
def initialize_video_device(pixel_format, width, height, channels):
    f = v4l2.v4l2_format()
    f.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
    f.fmt.pix.pixelformat = pixel_format
    f.fmt.pix.width = width
    f.fmt.pix.height = height
    f.fmt.pix.field = v4l2.V4L2_FIELD_NONE
    f.fmt.pix.bytesperline = width * channels
    f.fmt.pix.sizeimage = width * height * channels
    f.fmt.pix.colorspace = v4l2.V4L2_COLORSPACE_SRGB

    res = fcntl.ioctl(VIDEO_DEVICE, v4l2.VIDIOC_S_FMT, f)

    if res != 0:
        raise RuntimeError("Could not initialize video device: %d" % res)

    return True
Beispiel #21
0
    def run(self):

        # Open camera driver
        fd = os.open('/dev/video2', os.O_RDWR, 0)

        cap = v4l2.v4l2_capability()

        ioctl(fd, v4l2.VIDIOC_QUERYCAP, cap)

        print(hex(cap.capabilities))

        BUFTYPE = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
        MEMTYPE = v4l2.V4L2_MEMORY_MMAP
        FRAME_FORMAT = v4l2.V4L2_PIX_FMT_RGB565

        # Set format
        width = 320
        height = 240
        sizeimage = width * height * 2
        linewidth = width

        fmt = v4l2.v4l2_format()
        fmt.type = BUFTYPE
        fmt.fmt.pix.width = width
        fmt.fmt.pix.height = height
        fmt.fmt.pix.pixelformat = FRAME_FORMAT
        fmt.fmt.pix.sizeimage = sizeimage
        fmt.fmt.pix.field = v4l2.V4L2_FIELD_NONE
        fmt.fmt.pix.bytesperline = linewidth
        fmt.fmt.pix.colorspace = v4l2.V4L2_COLORSPACE_SRGB

        ret = ioctl(fd, v4l2.VIDIOC_S_FMT, fmt)
        print("fcntl.ioctl(fd, v4l2.VIDIOC_S_FMT, fmt) = %d" % ret)

        buffer_size = fmt.fmt.pix.sizeimage
        print("buffer_size = " + str(buffer_size))

        fb_address = 0x24000000

        while True:
            subprocess.check_output(
                f"echo 'dump_image fb.bin {hex(fb_address)} {hex(buffer_size)}; exit' | nc localhost 4444",
                shell=True)
            with open("fb.bin", "rb") as f_in:
                os.write(fd, f_in.read())
Beispiel #22
0
def screengrabber(args):
    # Find address of framebuffer
    with open(args.elf, "rb") as f:
        elffile = ELFFile(f)
        fb_address = get_symbol_by_symbol_name(elffile,
                                               "framebuffer1").entry.st_value

    # Open camera driver
    fd = os.open(args.device, os.O_RDWR, 0)

    cap = v4l2.v4l2_capability()

    ioctl(fd, v4l2.VIDIOC_QUERYCAP, cap)

    BUFTYPE = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
    FRAME_FORMAT = v4l2.V4L2_PIX_FMT_RGB565

    # Set format
    width = args.width
    height = args.height
    sizeimage = width * height * 2
    linewidth = width

    fmt = v4l2.v4l2_format()
    fmt.type = BUFTYPE
    fmt.fmt.pix.width = width
    fmt.fmt.pix.height = height
    fmt.fmt.pix.pixelformat = FRAME_FORMAT
    fmt.fmt.pix.sizeimage = sizeimage
    fmt.fmt.pix.field = v4l2.V4L2_FIELD_NONE
    fmt.fmt.pix.bytesperline = linewidth
    fmt.fmt.pix.colorspace = v4l2.V4L2_COLORSPACE_SRGB

    ret = ioctl(fd, v4l2.VIDIOC_S_FMT, fmt)
    print("fcntl.ioctl(fd, v4l2.VIDIOC_S_FMT, fmt) = %d" % ret)

    buffer_size = fmt.fmt.pix.sizeimage

    with OpenOCD(host=args.host, port=args.port) as ocd:
        while True:
            fb = ocd.send(
                f"dump_image fb.bin {hex(fb_address)} {hex(buffer_size)}; exit"
            )
            with open("fb.bin", "rb") as f_in:
                os.write(fd, f_in.read())
Beispiel #23
0
    def _set_format(self, shape_wanted: Tuple[int, int],
                    fourcc) -> Tuple[int, int]:
        """roughly equivalent to v4l2capture's set_format"""
        assert not self.started

        fmt = v4l2.v4l2_format(type=v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE)
        self._v4l2_ioctl(v4l2.VIDIOC_G_FMT, fmt)

        fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        fmt.fmt.pix.width = shape_wanted[1]
        fmt.fmt.pix.height = shape_wanted[0]
        fmt.fmt.pix.bytesperline = 0
        fmt.fmt.pix.pixelformat = fourcc
        fmt.fmt.pix.field = v4l2.V4L2_FIELD_ANY
        self._v4l2_ioctl(v4l2.VIDIOC_S_FMT, fmt)

        # return actual resolution
        return (fmt.fmt.pix.height, fmt.fmt.pix.width)
Beispiel #24
0
 def open_yuyv_camera(self):
     d = os.open(self.webcam, os.O_RDWR)
     cap = v4l2.v4l2_capability()
     ioctl(d, v4l2.VIDIOC_QUERYCAP, cap)
     vid_format = v4l2.v4l2_format()
     vid_format.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
     vid_format.fmt.pix.width = self.width
     vid_format.fmt.pix.height = self.height
     if self.input_fmt == "RGB24":
         vid_format.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_RGB24
     elif self.input_fmt == "YUY2":
         vid_format.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_YUYV
     else:
         assert False
     vid_format.fmt.pix.field = v4l2.V4L2_FIELD_NONE
     vid_format.fmt.pix.colorspace = v4l2.V4L2_COLORSPACE_SRGB
     ioctl(d, v4l2.VIDIOC_S_FMT, vid_format)
     return d
Beispiel #25
0
    def init_device(self):
        cap = v4l2.v4l2_capability()
        fmt = v4l2.v4l2_format()

        ioctl(self.fd, v4l2.VIDIOC_QUERYCAP, cap)

        if not (cap.capabilities & v4l2.V4L2_CAP_VIDEO_CAPTURE):
            raise Exception("{} is not a video capture device".format(
                self.device_name))

        fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
        fmt.fmt.pix.width = 640
        fmt.fmt.pix.height = 480
        fmt.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_MJPEG
        fmt.fmt.pix.field = v4l2.V4L2_FIELD_NONE
        ioctl(self.fd, v4l2.VIDIOC_S_FMT, fmt)

        self.init_mmap()
Beispiel #26
0
    def load(self, width, height):

        print("make sure you ran: sudo modprobe v4l2loopback")
        self.device = open('/dev/video0', 'wb')
        capability = v4l2.v4l2_capability()
        fmt = v4l2.V4L2_PIX_FMT_YUYV
        format = v4l2.v4l2_format()
        format.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
        format.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_BGR24
        format.fmt.pix.width = width
        format.fmt.pix.height = height
        format.fmt.pix.field = v4l2.V4L2_FIELD_NONE
        format.fmt.pix.bytesperline = width * 3
        format.fmt.pix.sizeimage = width * height * 3
        ioctl(self.device, v4l2.VIDIOC_S_FMT, format)

        # TODO: Launch ffmpeg command

        return self
Beispiel #27
0
 def GetCurrentFormat(self):
   form = v4l2.v4l2_format()
   form.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
   xioctl(self.vd, v4l2.VIDIOC_G_FMT, form)
   
   self.shape = (form.fmt.pix.height, form.fmt.pix.width) ##### self.shape
   
   stp = v4l2.v4l2_streamparm()
   stp.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
   xioctl(self.vd, v4l2.VIDIOC_G_PARM, stp)
   self.fps = stp.parm.capture.timeperframe.denominator/stp.parm.capture.timeperframe.numerator    
   if self.verbose:
     print '--GetCurrentFormat'
     print '  + width:', form.fmt.pix.width
     print '  + height:', form.fmt.pix.height
     print '  + pixelformat:', hex(form.fmt.pix.pixelformat)
     print '  + sizeimage:', form.fmt.pix.sizeimage
   #print 'Get:', self.shape, self.fps
   return form, stp
Beispiel #28
0
 def __init__(self, device):
     width = 1280
     height = 720
     if device:
         self.camera = open(device, 'wb')
     else:
         self.camera = self.findDummyCamera()
     if not self.camera:
         exit(1)
     fmt = v4l2.v4l2_format()
     fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
     fmt.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_YUYV
     fmt.fmt.pix.width = width
     fmt.fmt.pix.height = height
     fmt.fmt.pix.field = v4l2.V4L2_FIELD_NONE
     fmt.fmt.pix.bytesperline = width * 2
     fmt.fmt.pix.sizeimage = width * height * 2
     fmt.fmt.pix.colorspace = v4l2.V4L2_COLORSPACE_SRGB
     ret = fcntl.ioctl(self.camera, v4l2.VIDIOC_S_FMT, fmt)
Beispiel #29
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
def setup_cam_out(path, width, height):
    f = open(path, 'wb')

    if not f:
        raise Exception

    v = v4l2.v4l2_format()
    v.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT

    if fcntl.ioctl(f, v4l2.VIDIOC_G_FMT, v) < 0:
        raise Exception

    v.fmt.pix.width = width
    v.fmt.pix.height = height
    v.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_RGB24
    v.fmt.pix.sizeImage = width * height * 3

    if fcntl.ioctl(f, v4l2.VIDIOC_S_FMT, v) < 0:
        raise Exception

    return f
Beispiel #31
0
def write_to_video_device(device, image_data, pixel_format, width, height):
    if device:
        channels = 3
        f = v4l2.v4l2_format()
        f.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
        if pixel_format == 'RGB24':
            f.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_RGB24
        else:
            f.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_RGB32
            channels = 4
        f.fmt.pix.width = width
        f.fmt.pix.height = height
        f.fmt.pix.field = v4l2.V4L2_FIELD_NONE
        f.fmt.pix.bytesperline = width * channels
        f.fmt.pix.sizeimage = width * height * channels
        f.fmt.pix.colorspace = v4l2.V4L2_COLORSPACE_SRGB

        res = fcntl.ioctl(device, v4l2.VIDIOC_S_FMT, f)

        if res == 0:
            device.write(image_data)
        else:
            print("Could not write frame to v4l2 loopback device")
Beispiel #32
0
def write_to_video_device(device, image_data, pixel_format, width, height):
    if device:
        channels = 3
        f = v4l2.v4l2_format()
        f.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
        if pixel_format == 'RGB24':
            f.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_RGB24
        else:
            f.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_RGB32
            channels = 4
        f.fmt.pix.width = width
        f.fmt.pix.height = height
        f.fmt.pix.field = v4l2.V4L2_FIELD_NONE
        f.fmt.pix.bytesperline = width * channels
        f.fmt.pix.sizeimage = width * height * channels
        f.fmt.pix.colorspace = v4l2.V4L2_COLORSPACE_SRGB

        res = fcntl.ioctl(device, v4l2.VIDIOC_S_FMT, f)

        if res == 0:
            device.write(image_data)
        else:
            print "Could not write frame to v4l2 loopback device"
Beispiel #33
0
    def setup(self):
        # prepare input camera to capture frames
        cam_id = int(self.in_dev_name[len('/dev/video'):])
        self.in_dev = cv2.VideoCapture(cam_id)  #self.in_dev_name)
        ok, im = self.in_dev.read()
        if not ok:
            raise IOError('Unable to read frames from device %s' %
                          self.in_dev_name)

        # get frame domensions
        height, width, _ = im.shape
        channels = 2

        # open output camera
        if not os.path.exists(self.out_dev_name):
            raise IOError('Device %s does not exist' % self.out_dev_name)
        self.out_dev = open(self.out_dev_name, 'wb')

        # get output camera capabilities
        capability = v4l2.v4l2_capability()
        print('Get capabilities result: %s' %
              (fcntl.ioctl(self.out_dev, v4l2.VIDIOC_QUERYCAP, capability)))
        print('Capabilities: %s' % hex(capability.capabilities))
        print('V4l2 driver: %s' % capability.driver.decode())

        # set up formatting of output camera
        format = v4l2.v4l2_format()
        format.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
        format.fmt.pix.field = v4l2.V4L2_FIELD_NONE
        format.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_YUYV
        format.fmt.pix.width = width
        format.fmt.pix.height = height
        format.fmt.pix.bytesperline = width * channels
        format.fmt.pix.sizeimage = width * height * channels
        format.fmt.pix.colorspace = v4l2.V4L2_COLORSPACE_SRGB
        print('Set format result: %d' %
              fcntl.ioctl(self.out_dev, v4l2.VIDIOC_S_FMT, format))
Beispiel #34
0
def get_v4l2_loopback_device(dev, opt):
    import fcntl
    import v4l2

    format = v4l2.v4l2_format()
    format.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
    format.fmt.pix.field = v4l2.V4L2_FIELD_NONE
    format.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_RGB24
    format.fmt.pix.width = opt.loadSize
    format.fmt.pix.height = opt.loadSize
    format.fmt.pix.bytesperline = opt.loadSize * 3
    format.fmt.pix.sizeimage = opt.loadSize * opt.loadSize * 3

    if not os.path.exists(dev):
        print(
            f"Error {dev} does not exist. did you 'sudo modprobe v4l2loopback video_nr=69'?"
        )
        return

    device = open(dev, 'wb')

    fcntl.ioctl(device, v4l2.VIDIOC_S_FMT, format)

    return device
Beispiel #35
0
 def get_pixelformat(self):
     fmt = v4l2.v4l2_format()
     fmt.type = v4l2.V4L2_BUF_TYPE_VIDEO_CAPTURE
     ret = fcntl.ioctl(self.vd, v4l2.VIDIOC_G_FMT, fmt)
     return ret, fmt.fmt.pix.pixelformat
Beispiel #36
0
width = 640
height = 480
FPS = 24
seconds = 1
imgs = []
for i in range(0,5):
    img=cv2.imread('640x480/%s%d.png'%(basename,i))
    img=cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    imgs.append(img)

devName = '/dev/video7'
if not os.path.exists(devName):
    print ("Warning: device does not exist",devName)
device = open(devName, 'wb')
height,width,channels = img.shape
format                      = v4l2.v4l2_format()
format.type                 = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
format.fmt.pix.field        = v4l2.V4L2_FIELD_NONE
format.fmt.pix.pixelformat  = v4l2.V4L2_PIX_FMT_BGR24
format.fmt.pix.width        = width
format.fmt.pix.height       = height
format.fmt.pix.bytesperline = width * channels
format.fmt.pix.sizeimage    = width * height * channels
print ("set format result (0 is good):{}".format(fcntl.ioctl(device, v4l2.VIDIOC_S_FMT, format)))
print("begin loopback write")

p=pyaudio.PyAudio()
bufcount=0
fpb=50
elapsed=0.0
starttime=time.time()
Beispiel #37
0
    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)
Beispiel #38
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()