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_BGR24 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) if fcntl.ioctl(self._video_device, _v4l2.VIDIOC_S_FMT, self._settings) < 0: print("ERROR: unable to set video format!")
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)
def open_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 vid_format.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_RGB24 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
def __init__(self, video_device, width, height, channels=3, input_pixfmt='BGR', output_pixfmt=_v4l2.V4L2_PIX_FMT_YUYV): if channels != 3: raise NotImplementedError( 'Code only supports inputs with 3 channels right now. You tried to intialize with {} channels' .format(channels)) 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.input_pixfmt = input_pixfmt 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 = output_pixfmt 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.colorspace = _v4l2.V4L2_COLORSPACE_JPEG #self._settings.fmt.pix.colorspace = _v4l2.V4L2_COLORSPACE_SRGB #self._settings.fmt.pix.colorspace = _v4l2.V4L2_COLORSPACE_RAW if self._settings.fmt.pix.pixelformat == _v4l2.V4L2_PIX_FMT_YUYV \ or self._settings.fmt.pix.pixelformat == _v4l2.V4L2_PIX_FMT_YVYU \ or self._settings.fmt.pix.pixelformat == _v4l2.V4L2_PIX_FMT_YYUV: self._settings.fmt.pix.bytesperline = width * 2 self._settings.fmt.pix.sizeimage = width * height * 2 self._buffer = np.zeros((self._settings.fmt.pix.height, 2 * self._settings.fmt.pix.width), dtype=np.uint8) elif self._settings.fmt.pix.pixelformat == _v4l2.V4L2_PIX_FMT_BGR24 or self._settings.fmt.pix.pixelformat == _v4l2.V4L2_PIX_FMT_RGB24: self._settings.fmt.pix.bytesperline = width * 3 self._settings.fmt.pix.sizeimage = width * height * 3 self._buffer = np.zeros((self._settings.fmt.pix.height, 3 * self._settings.fmt.pix.width), dtype=np.uint8) elif self._settings.fmt.pix.pixelformat == _v4l2.V4L2_PIX_FMT_YUV32 or self._settings.fmt.pix.pixelformat == _v4l2.V4L2_PIX_FMT_RGB32: self._settings.fmt.pix.bytesperline = width * 4 self._settings.fmt.pix.sizeimage = width * height * 4 self._buffer = np.zeros((self._settings.fmt.pix.height, 4 * self._settings.fmt.pix.width), dtype=np.uint8) else: raise NotImplementedError( 'Code does not support outputs in format {} right now.'.format( self._settings.fmt.pix.pixelformat)) self._yuv = np.zeros( (self._settings.fmt.pix.height, self._settings.fmt.pix.width, 3), dtype=np.uint8) fcntl.ioctl(self._video_device, _v4l2.VIDIOC_S_FMT, self._settings)