def __init__( self, parent, camera_port, input_port, format, resize, **options): # If a resize hasn't been requested, check the input_port format. If # it requires conversion, force the use of a resizer to perform the # conversion if not resize: if parent.RAW_FORMATS[format] != input_port[0].format[0].encoding.value: resize = parent.resolution # Workaround: If a non-alpha format is requested when a resizer is # required, we use the alpha-inclusive format and set a flag to get the # callback to strip the alpha bytes (for some reason the resizer won't # work with non-alpha output formats - firmware bug?) if resize: width, height = resize self._strip_alpha = format in ('rgb', 'bgr') else: width, height = parent.resolution self._strip_alpha = False width = mmal.VCOS_ALIGN_UP(width, 32) height = mmal.VCOS_ALIGN_UP(height, 16) # Workaround (#83): when the resizer is used the width and height must # be aligned (both the actual and crop values) to avoid an error when # the output port format is set if resize: resize = (width, height) # Workaround: Calculate the expected image size, to be used by the # callback to decide when a frame ends. This is to work around a # firmware bug that causes the raw image to be returned twice when the # maximum camera resolution is requested self._expected_size = int(width * height * self.RAW_ENCODINGS[format][1]) self._image_size = 0 super(PiRawEncoderMixin, self).__init__( parent, camera_port, input_port, format, resize, **options)
def _create_resizer(self, width, height): self.resizer = ct.POINTER(mmal.MMAL_COMPONENT_T)() mmal_check( mmal.mmal_component_create( mmal.MMAL_COMPONENT_DEFAULT_RESIZER, self.resizer), prefix="Failed to create resizer component") if not self.resizer[0].input_num: raise PiCameraError("No input ports on resizer component") if not self.resizer[0].output_num: raise PiCameraError("No output ports on resizer component") # Copy the original input port's format to the resizer's input, # then the resizer's input format to the output, and configure it mmal.mmal_format_copy( self.resizer[0].input[0][0].format, self.input_port[0].format) mmal_check( mmal.mmal_port_format_commit(self.resizer[0].input[0]), prefix="Failed to set resizer input port format") mmal.mmal_format_copy( self.resizer[0].output[0][0].format, self.resizer[0].input[0][0].format) fmt = self.resizer[0].output[0][0].format fmt[0].es[0].video.width = mmal.VCOS_ALIGN_UP(width, 32) fmt[0].es[0].video.height = mmal.VCOS_ALIGN_UP(height, 16) fmt[0].es[0].video.crop.x = 0 fmt[0].es[0].video.crop.y = 0 fmt[0].es[0].video.crop.width = width fmt[0].es[0].video.crop.height = height mmal_check( mmal.mmal_port_format_commit(self.resizer[0].output[0]), prefix="Failed to set resizer output port format")
def test_overlay_array1(camera, mode): resolution, framerate = mode # Draw a cross overlay w, h = resolution w = mmal.VCOS_ALIGN_UP(w, 32) h = mmal.VCOS_ALIGN_UP(h, 16) a = np.zeros((h, w, 3), dtype=np.uint8) a[resolution[1] // 2, :, :] = 0xff a[:, resolution[0] // 2, :] = 0xff overlay = camera.add_overlay(a, resolution, alpha=128) assert len(camera.overlays) == 1 assert camera.overlays[0].alpha == 128 camera.remove_overlay(overlay) assert not camera.overlays
def normalize_dimension(dimension): """ Normalizes a dimension so that it's width is a multiple of 32 and it's height is a multiple of 16. :param dimension: :return: """ if len(dimension) == 4: return (int(dimension[0]), int(dimension[1]), mmal.VCOS_ALIGN_UP(int(dimension[2]), 32), mmal.VCOS_ALIGN_UP(int(dimension[3]), 16)) else: return (mmal.VCOS_ALIGN_UP(int(dimension[0]), 32), mmal.VCOS_ALIGN_UP(int(dimension[1]), 16))
def __init__(self, parent, source, size=None, layer=0, alpha=255, fullscreen=True, window=None, crop=None, rotation=0, vflip=False, hflip=False): super(PiOverlayRenderer, self).__init__(parent, layer, alpha, fullscreen, window, crop, rotation, vflip, hflip) # Copy format from camera's preview port, then adjust the encoding to # RGB888 and optionally adjust the resolution and size port = self.renderer[0].input[0] fmt = port[0].format mmal.mmal_format_copy( fmt, parent._camera[0].output[parent.CAMERA_PREVIEW_PORT][0].format) fmt[0].encoding = mmal.MMAL_ENCODING_RGB24 fmt[0].encoding_variant = mmal.MMAL_ENCODING_RGB24 if size is not None: w, h = size fmt[0].es[0].video.width = mmal.VCOS_ALIGN_UP(w, 32) fmt[0].es[0].video.height = mmal.VCOS_ALIGN_UP(h, 16) fmt[0].es[0].video.crop.width = w fmt[0].es[0].video.crop.height = h mmal_check(mmal.mmal_port_format_commit(port), prefix="Overlay format couldn't be set") port[0].buffer_num = port[0].buffer_num_min port[0].buffer_size = port[0].buffer_size_recommended mmal_check(mmal.mmal_component_enable(self.renderer), prefix="Overlay couldn't be enabled") mmal_check(mmal.mmal_port_enable(port, _overlay_callback), prefix="Overlay input port couldn't be enabled") self.pool = mmal.mmal_port_pool_create(port, port[0].buffer_num, port[0].buffer_size) if not self.pool: raise PiCameraRuntimeError("Couldn't create pool for overlay") self.update(source)