def bytes_to_yuv(data, resolution):
"""
Converts a bytes object containing YUV data to a `numpy`_ array.
"""
width, height = resolution
fwidth, fheight = raw_resolution(resolution)
y_len = fwidth * fheight
uv_len = (fwidth // 2) * (fheight // 2)
if len(data) != (y_len + 2 * uv_len):
raise PiCameraValueError(
'Incorrect buffer length for resolution %dx%d' % (width, height))
# Separate out the Y, U, and V values from the array
a = np.frombuffer(data, dtype=np.uint8)
Y = a[:y_len]
U = a[y_len:-uv_len]
V = a[-uv_len:]
# Reshape the values into two dimensions, and double the size of the
# U and V values (which only have quarter resolution in YUV4:2:0)
Y = Y.reshape((fheight, fwidth))
U = U.reshape((fheight // 2, fwidth // 2)).repeat(2, axis=0).repeat(2, axis=1)
V = V.reshape((fheight // 2, fwidth // 2)).repeat(2, axis=0).repeat(2, axis=1)
# Stack the channels together and crop to the actual resolution
return np.dstack((Y, U, V))[:height, :width]
评论列表
文章目录