示例#1
0
    def do_gst_push_src_fill(self, buffer: Gst.Buffer) -> Gst.FlowReturn:
        try:

            (
                image_array,
                image_frame_id,
                image_timestamp_ns,
            ) = self.image_acquirer.get_next_image(logger=Gst.warning)

            with map_gst_buffer(buffer, Gst.MapFlags.READ) as mapped:
                mapped_array = np.ndarray(image_array.shape,
                                          buffer=mapped,
                                          dtype=image_array.dtype)
                mapped_array[:] = image_array

            if self.timestamp_offset == 0:
                self.timestamp_offset = image_timestamp_ns
                self.previous_timestamp = image_timestamp_ns

            buffer.pts = image_timestamp_ns - self.timestamp_offset
            buffer.offset = image_frame_id
            buffer.offset_end = image_frame_id + 1
            buffer.duration = image_timestamp_ns - self.previous_timestamp

            self.previous_timestamp = image_timestamp_ns

            Gst.log(
                f"Sending buffer of size: {image_array.nbytes} bytes, "
                f"type: {image_array.dtype}, "
                f"offset: {image_frame_id}"
                f"timestamp offset: {buffer.pts // self.MILLISECONDS_PER_NANOSECOND}ms"
            )

        except Exception as ex:
            Gst.error(f"Error: {ex}")
            return Gst.FlowReturn.ERROR

        return Gst.FlowReturn.OK
示例#2
0
def gst_buffer_to_ndarray(buffer: Gst.Buffer,
                          *,
                          width: int,
                          height: int,
                          channels: int,
                          dtype: np.dtype,
                          bpp: int = 1,
                          do_copy: bool = False) -> np.ndarray:
    """Converts Gst.Buffer with known format (w, h, c, dtype) to np.ndarray"""

    result = None
    if do_copy:
        result = np.ndarray(buffer.get_size() // (bpp // BITS_PER_BYTE),
                            buffer=buffer.extract_dup(0, buffer.get_size()),
                            dtype=dtype)
    else:
        with map_gst_buffer(buffer, Gst.MapFlags.READ) as mapped:
            result = np.ndarray(buffer.get_size() // (bpp // BITS_PER_BYTE),
                                buffer=mapped,
                                dtype=dtype)
    if channels > 0:
        result = result.reshape(height, width, channels).squeeze()
    return result
示例#3
0
def gst_buffer_for_ndarray(buffer: Gst.Buffer, *, width: int, height: int, 
                          channels: int,
                          dtype: np.dtype, bpp: int = 1) ->  np.ndarray:
    """Converts Gst.Buffer with known format (w, h, c, dtype) to np.ndarray
    Returns: 
        np.ndarray(c, w, h)
    """
    with map_gst_buffer(buffer, Gst.MapFlags.READ) as mapped:
        result = np.ndarray((buffer.get_size() // (bpp // BITS_PER_BYTE)),
                            buffer=mapped, dtype=dtype)
        #make (c,h,w) array from memory buf
        if channels > 0 :
            result = result.reshape(channels, height, width)
        else : #make (c,w,h) array form memory buf   
            result = result.reshape(height,width)
        return result