Ask Your Question

Revision history [back]

Trying to create video from multiple frames

I'm trying to create a video from multiple frames (captured by opencv and ingested into kafka). I receive a byte string of the image (frame) and have verified that it's valid (by writing to a jpeg, showing with cv2.imshow(..), etc). What I cannot seem to conquer is creating a video clip from multiple frames.

Here is my current code:

import cv2 
import numpy as np 
from kafka
import KafkaConsumer

consumer = KafkaConsumer('cam', bootstrap_servers=['localhost:9092'])

fourcc = cv2.VideoWriter_fourcc(*'DIVX') out =
cv2.VideoWriter('test.avi', fourcc, 20.0, (1920, 1080)) i = 0

for message in consumer:

print("message.topic=%s, message.partition=%d,message.offset=%d, key=%s" % (message.topic, message.partition,
                                                                             message.offset, message.key))
i += 1

if i == 50:
    break

nparr = np.fromstring(message.value, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

cv2.imshow('THIS IS THE CURRENT FRAME', img)

out.write(img)

if (cv2.waitKey(1) & 0xFF) == ord('q'):  # Hit `q` to exit
    break

out.release() cv2.destroyAllWindows()

Any tips/help is greatly appreciated!