OpenCV YUV420 to RGB

Hi, I’m trying to figure out how to convert a YUV420 image to an RGB image, but it’s not working:

buffer = self.model.picam2.capture_buffer("lores")

self.logger.debug(f"Captured frame: {buffer}")
self.logger.debug(f"Captured frame shape: {buffer.shape}")

rgb = cv2.cvtColor(buffer, cv2.COLOR_YUV420p2RGB)

ret, jpeg = cv2.imencode('.jpg', rgb)
if not ret:
    self.logger.error("Failed to encode frame as JPEG")
    continue

yield (b'--frame\r\n'
    b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n')

The error that this gives is:

Error in video feed: OpenCV(4.6.0) ./modules/imgproc/src/color.simd_helpers.hpp:108: error: (-215:Assertion failed) sz.width % 2 == 0 && sz.height % 3 == 0 in function ‘CvtHelper’

So I printed the shape and frame, and this is the result:

Captured frame: [118 120 122 ... 137 137 140]
Captured frame shape: (460800,)

What am I doing wrong here? This source suggests that this should work

I solved it, I was using capture_buffer, not capture_array. Oops!