Trying to create video from multiple frames [closed]
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!
how did you install your cv2 ?
(if it came prebuilt from a ppm, like pip or conda, it probably lacks any backend for this)
via pip..
so I did a test and used my webcam and I can write to a (playable) video.
What do ya think is missing exactly? (and thank you for the response!!)
try a
print(cv2.getBuildInformation())
(see: "VideoIO" section there, you need either FFMPEG or GSTREAMER support for this, and most likely, you have to build it from src here.looks like I have it ya?
yea, looks like.
so, what exactly is the problem, now ?
Basically, when I create my video file,. it's always 11kb .. not playable..
I've also tested each frame by saving them as individual images and they are viewable w/out issue.
So I'm confused as to what I'm missing, or perhaps if I'm approaching this wrong.
I'm basically ingesting camera streams through kafka (using opencv, capturing each frame, injecting into kafka) and wanting to recreate the video as a file on the other end.
Any thoughts?
does the frame size match, what you give to the VideoWriter ?
yep, that was the first thing I checked (and hoped was the issue lol)
try different codecs, like VIDX (it created the avi container successfully)
@yomateod. You cannot do VideoWriter and save video to img.using Kafka. Very sorry, I can\t help you out. Btw, I removed answer that not going to helped.