Ask Your Question
0

video write doesn't work from buffer

asked 2020-02-01 05:34:34 -0600

Hi, I'm trying to write a video from image buffer, and I have troubles with that. When I save the buffer to an image, and then read it with opencv and write to video, it works, but if I try to skip the saving process ( which makes more sense in this case), the video output is empty, even though when I compare the image I processed from buffer with the image read from disk with opencv, they are the same. What am I missing here?

writer = cv2.VideoWriter("output2.avi", cv2.VideoWriter_fourcc(*'MJPG'), 24, (WINDOW_WIDTH, WINDOW_HEIGHT))

for data in imgs:
    data_array = np.frombuffer(data, np.uint8)
    data_array.shape = WINDOW_WIDTH, WINDOW_HEIGHT, 4
    new_data_array = cv2.cvtColor(data_array, cv2.COLOR_RGBA2BGR)
    # for testing
    img = Image.frombuffer("RGBA", (WINDOW_WIDTH, WINDOW_HEIGHT), data)
    img.save("img.png", "png")
    im = cv2.imread("img.png", 1)
    print(im.all() == new_data_array.all()) # prints all true

    # writer.write(new_data_array) # doesn't work
    writer.write(im) # works
writer.release()
edit retag flag offensive close merge delete

Comments

data_array.shape = WINDOW_WIDTH, WINDOW_HEIGHT, 4

dont try with 4 channel images, it cannot write more than 3.

please also check image dimensions

berak gravatar imageberak ( 2020-02-01 08:40:51 -0600 )edit

I convert 4 channel RGBA to 3 channel BGR using cv2.cvtColor. The final result, new_data_array is exactly the same as the image read from disk, as shown by print(im.all() == new_data_array.all()). It prints True. Yet, when I write im (the image read from disk) it works, but when I write the new_data_array, it doesn't. I don't understand how it's even possible. Their types are the same.

makar_y gravatar imagemakar_y ( 2020-02-01 19:40:29 -0600 )edit

Actually, the problem was in transposing. In cv images they use (height, width, levels), instead of (width, height, levels). When I switched the two to make it data_array.shape = WINDOW_HEIGHT, WINDOW_WIDTH, 4, if finally worked. (I used a wrong equality form, I should've used (im == new_data_array).all() instead of the one I used. It would've shown that the arrays were in fact not equal

makar_y gravatar imagemakar_y ( 2020-02-01 20:14:17 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-02-01 07:38:16 -0600

mvuori gravatar image

Well, you don't save and read the data_arrays, but img, do img is the thing you should write

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-02-01 05:34:34 -0600

Seen: 488 times

Last updated: Feb 01 '20