Read a Bytes image from Amazon Kinesis output in python

2024/10/9 20:21:46

I used imageio.get_reader(BytesIO(a), 'ffmpeg') to load a bytes image and save it as normal image.

But the below error throws when I read the image using imageio.get_reader(BytesIO(a), 'ffmpeg')

Traceback (most recent call last):File "<stdin>", line 1, in <module>File "/home/tango/anaconda3/lib/python3.6/site-packages/imageio/core/functions.py", line 186, in get_readerreturn format.get_reader(request)File "/home/tango/anaconda3/lib/python3.6/site-packages/imageio/core/format.py", line 164, in get_readerreturn self.Reader(self, request)File "/home/tango/anaconda3/lib/python3.6/site-packages/imageio/core/format.py", line 214, in __init__self._open(**self.request.kwargs.copy())File "/home/tango/anaconda3/lib/python3.6/site-packages/imageio/plugins/ffmpeg.py", line 323, in _openself._initialize()File "/home/tango/anaconda3/lib/python3.6/site-packages/imageio/plugins/ffmpeg.py", line 466, in _initializeself._meta.update(self._read_gen.__next__())File "/home/tango/anaconda3/lib/python3.6/site-packages/imageio_ffmpeg/_io.py", line 150, in read_framesraise IOError(fmt.format(err2))
OSError: Could not load meta information
=== stderr ===ffmpeg version 4.2 Copyright (c) 2000-2019 the FFmpeg developersbuilt with gcc 7.3.0 (crosstool-NG 1.23.0.449-a04d0)configuration: --prefix=/home/tango/anaconda3 --cc=/home/conda/feedstock_root/build_artifacts/ffmpeg_1566210161358/_build_env/bin/x86_64-conda_cos6-linux-gnu-cc --disable-doc --disable-openssl --enable-avresample --enable-gnutls --enable-gpl --enable-hardcoded-tables --enable-libfreetype --enable-libopenh264 --enable-libx264 --enable-pic --enable-pthreads --enable-shared --enable-static --enable-version3 --enable-zlib --enable-libmp3lamelibavutil      56. 31.100 / 56. 31.100libavcodec     58. 54.100 / 58. 54.100libavformat    58. 29.100 / 58. 29.100libavdevice    58.  8.100 / 58.  8.100libavfilter     7. 57.100 /  7. 57.100libavresample   4.  0.  0 /  4.  0.  0libswscale      5.  5.100 /  5.  5.100libswresample   3.  5.100 /  3.  5.100libpostproc    55.  5.100 / 55.  5.100
[matroska,webm @ 0x5619b9da3cc0] File ended prematurely
[matroska,webm @ 0x5619b9da3cc0] Could not find codec parameters for stream 0 (Video: h264, none, 1280x720): unspecified pixel format
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, matroska,webm, from '/tmp/imageio_zm6hhpgr':Metadata:title           : Kinesis Video SDKencoder         : Kinesis Video SDK 1.0.0AWS_KINESISVIDEO_FRAGMENT_NUMBER: 91343852333183888465720004820715065721442989478AWS_KINESISVIDEO_SERVER_TIMESTAMP: 1580791384.096AWS_KINESISVIDEO_PRODUCER_TIMESTAMP: 1580791377.843Duration: N/A, bitrate: N/AStream #0:0(eng): Video: h264, none, 1280x720, SAR 1:1 DAR 16:9, 1k tbr, 1k tbn, 2k tbc (default)Metadata:title           : kinesis_video
Stream mapping:Stream #0:0 -> #0:0 (h264 (native) -> rawvideo (native))
Press [q] to stop, [?] for help
Cannot determine format of input stream 0:0 after EOF
Error marking filters as finished
Conversion failed!

The above approach to read a MKV bytes file was done based on this thread

Or is there is any approach to parse and read the MKV bytes file.

Answer

Old question, but I'll leave an answer here for future readers :)

In general, if you have a bytes string that encodes a video (or image), chances are high that you can pass it directly into ImageIO's high-level API and that it can read the bytes directly. In the case of MKV (and some other video formats) ImageIO needs a bit of help because the current ffmpeg plugin struggles with byte strings. [There is a new plugin on the way to fix this and other issues.]

ImageIO<2.15.0

import imageio as iioreq = iio.core.Request(mkv_bytes, "r")# note: this is where imageIO needs some help, see
# https://github.com/imageio/imageio/issues/686
req._extension = ".mkv"first_frame = iio.imread(req)

ImageIO>2.15.0 (releases this week - W6 2022)

import imageio as iiofirst_frame = iio.v3.imread(mkv_bytes, format_hint=".mkv")
https://en.xdnf.cn/q/69981.html

Related Q&A

How to compute optical flow using tvl1 opencv function

Im trying to find python example for computing optical flow with tvl1 opencv function createOptFlow_DualTVL1 but it seems that there isnt enough documentation for it.Could anyone please let me do that?…

Python 3.3.4: python-daemon-3K ; How to use runner

Struggling to try and get a python daemon to work using Python 3.3.4. Im using the latest version of the python-daemon-3K from PyPi i.e. 1.5.8Starting point is the following code found How do you creat…

How to select specific data variables from xarray dataset

BACKGROUND I am trying to download GFS weather data netcdf4 files via xarray & OPeNDAP. Big thanks to Vorticity0123 for their prior post, which allowed me to get the bones of the python script sort…

List object has no attribute Values error

I would like to get the data to Excel worksheet. The problem is when I run the whole code I receive an error but when I run it separately no error it works. Here is what I want; from xlwings import Wor…

How to resize an image in python, while retaining aspect ratio, given a target size?

First off part of me feels like this is a stupid question, sorry about that. Currently the most accurate way Ive found of calculating the optimum scaling factor (best width and height for target pixel …

How to limit number of followed pages per site in Python Scrapy

I am trying to build a spider that could efficiently scrape text information from many websites. Since I am a Python user I was referred to Scrapy. However, in order to avoid scraping huge websites, I …

Why is Pythons sorted() slower than copy, then .sort()

Here is the code I ran:import timeitprint timeit.Timer(a = sorted(x), x = [(2, bla), (4, boo), (3, 4), (1, 2) , (0, 1), (4, 3), (2, 1) , (0, 0)]).timeit(number = 1000) print timeit.Timer(a=x[:];a.sort(…

How to efficiently unroll a matrix by value with numpy?

I have a matrix M with values 0 through N within it. Id like to unroll this matrix to create a new matrix A where each submatrix A[i, :, :] represents whether or not M == i.The solution below uses a lo…

Anaconda Python 3.6 -- pythonw and python supposed to be equivalent?

According to Python 3 documentation, python and pythonw should be equivalent for running GUI scripts as of 3.6With older versions of Python, there is one Mac OS X quirk that you need to be aware of: pr…

Good way of handling NoneType objects when printing in Python

How do I go about printin a NoneType object in Python?# score can be a NonType object logging.info("NEW_SCORE : "+score)Also why is that sometime I see a comma instead of the + above?