Skip to content

V4L2 VideoCapture always uses V4L2_PIX_FMT_BGR24 #18

@jampekka

Description

@jampekka

Even though there's logic to prefer MJPG or YUYV if available, VideoCapture always uses V4L2_PIX_FMT_BGR24 due to line:

self.v4l2_format.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24

Also, later on in the read-method this format is assumed for the buffer.

The jpeg compression really seems to be needed for two cameras to run in one USB controller at any resolution, even with the 0x80 quirk. Doing the JPEG handling here seems to save us from patching the uvcvideo-module.

I can get MJPG working with a following quick hack, although it'll probably break other formats:

--- a/pupil_src/shared_modules/uvc_capture/linux_video/v4l2_capture/__init__.py
+++ b/pupil_src/shared_modules/uvc_capture/linux_video/v4l2_capture/__init__.py
@@ -15,9 +15,12 @@ This file contains bindings to a webcam capture module that works with v4l2 and
 from ctypes import *
 from numpy.ctypeslib import ndpointer
 import numpy as np
+from PIL import Image
+from cStringIO import StringIO
 import os,sys
 #logging
 import logging
+
 logger = logging.getLogger(__name__)

 class CameraCaptureError(Exception):
@@ -216,7 +219,8 @@ class VideoCapture(object):
         self.v4l2_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE
         self.v4l2_format.fmt.pix.width       = size[0]
         self.v4l2_format.fmt.pix.height      = size[1]
-        self.v4l2_format.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24
+        #self.v4l2_format.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24
+        self.v4l2_format.fmt.pix.pixelformat = v4l2_fourcc(*self.prefered_format)
         self.v4l2_format.fmt.pix.field       = V4L2_FIELD_ANY
         if (-1 == dll.xioctl(self.device, VIDIOC_S_FMT, byref(self.v4l2_format))):
             self._close()
@@ -376,8 +380,10 @@ class VideoCapture(object):
                 # if ( self.ts > timestamp) or 1:
                 #     print "%s %s" %(self.src_str, self.get_time_monotonic()-timestamp)
                 buf_ptr = cast(buf_ptr,POINTER(c_uint8*buf.bytesused))
+
                 img = np.frombuffer(buf_ptr.contents,c_uint8)
-                img.shape = (self.v4l2_format.fmt.pix.height,self.v4l2_format.fmt.pix.width,3)
+                #img.shape = (self.v4l2_format.fmt.pix.height,self.v4l2_format.fmt.pix.width,3)
+               img = np.array(Image.open(StringIO(img.data)))
                 return Frame(timestamp, img)
             else:
                 logger.warning("Frame corrupted skipping it")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions