我正在尝试从多个帧中创建一个视频(由opencv捕获并摄取到kafka中)。我收到图像(帧)的字节字符串,并已验证它是否有效(通过写入jpeg,显示为
cv2.imshow(..)
等)。我无法克服的是从多个帧创建视频剪辑。
这是我的当前代码:
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()
非常感谢您的任何提示/帮助!