Trying to create video from multiple frames [closed]

asked 2018-12-16 19:17:22 -0600

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!

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-11-09 10:19:39.403109

Comments

how did you install your cv2 ?

(if it came prebuilt from a ppm, like pip or conda, it probably lacks any backend for this)

berak gravatar imageberak ( 2018-12-16 19:31:20 -0600 )edit

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!!)

yomateod gravatar imageyomateod ( 2018-12-16 19:32:37 -0600 )edit

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.

berak gravatar imageberak ( 2018-12-16 19:35:59 -0600 )edit
2

looks like I have it ya?

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES
      avcodec:                   YES (ver 58.35.100)
      avformat:                  YES (ver 58.20.100)
      avutil:                    YES (ver 56.22.100)
      swscale:                   YES (ver 5.3.100)
      avresample:                YES (ver 4.0.0)
    GStreamer:                   NO
    AVFoundation:                YES
yomateod gravatar imageyomateod ( 2018-12-16 19:38:24 -0600 )edit

yea, looks like.

and I can write to a (playable) video.

so, what exactly is the problem, now ?

berak gravatar imageberak ( 2018-12-16 19:45:15 -0600 )edit

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?

yomateod gravatar imageyomateod ( 2018-12-16 19:47:51 -0600 )edit

does the frame size match, what you give to the VideoWriter ?

berak gravatar imageberak ( 2018-12-16 19:49:44 -0600 )edit

yep, that was the first thing I checked (and hoped was the issue lol)

yomateod gravatar imageyomateod ( 2018-12-16 19:52:39 -0600 )edit

try different codecs, like VIDX (it created the avi container successfully)

berak gravatar imageberak ( 2018-12-17 01:13:52 -0600 )edit

@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.

supra56 gravatar imagesupra56 ( 2018-12-17 20:50:33 -0600 )edit