K
K
keddad2020-08-21 18:20:52
Python
keddad, 2020-08-21 18:20:52

How to load video in Opencv from byte string?

How can one read a byte string in cv2.VideoCapture? I tried like this:

cap = cv2.VideoCapture(io.BytesIO(decoded_vid)) # decoded_vid - bytes


When doing this I get a TypeError:
TypeError: Argument 'index' is required to be an integer

This seems to be a very popular use case, but I haven't been able to find examples of detailed work. It's still not quite clear why it expects an int argument - even the official doc has examples where a string with a file path is passed as an argument.

Full traceback:
ff_videocomp_1   |   File "/usr/local/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 390, in run_asgi
ff_videocomp_1   |     result = await app(self.scope, self.receive, self.send)
ff_videocomp_1   |   File "/usr/local/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
ff_videocomp_1   |     return await self.app(scope, receive, send)
ff_videocomp_1   |   File "/usr/local/lib/python3.8/site-packages/fastapi/applications.py", line 179, in __call__
ff_videocomp_1   |     await super().__call__(scope, receive, send)
ff_videocomp_1   |   File "/usr/local/lib/python3.8/site-packages/starlette/applications.py", line 111, in __call__
ff_videocomp_1   |     await self.middleware_stack(scope, receive, send)
ff_videocomp_1   |   File "/usr/local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
ff_videocomp_1   |     raise exc from None
ff_videocomp_1   |   File "/usr/local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
ff_videocomp_1   |     await self.app(scope, receive, _send)
ff_videocomp_1   |   File "/usr/local/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
ff_videocomp_1   |     raise exc from None
ff_videocomp_1   |   File "/usr/local/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
ff_videocomp_1   |     await self.app(scope, receive, sender)
ff_videocomp_1   |   File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 566, in __call__
ff_videocomp_1   |     await route.handle(scope, receive, send)
ff_videocomp_1   |   File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 227, in handle
ff_videocomp_1   |     await self.app(scope, receive, send)
ff_videocomp_1   |   File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 41, in app
ff_videocomp_1   |     response = await func(request)
ff_videocomp_1   |   File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 182, in app
ff_videocomp_1   |     raw_response = await run_endpoint_function(
ff_videocomp_1   |   File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 133, in run_endpoint_function
ff_videocomp_1   |     return await dependant.call(**values)
ff_videocomp_1   |   File "./main.py", line 26, in process
ff_videocomp_1   |     cap = cv2.VideoCapture(io.BytesIO(decoded_vid))
ff_videocomp_1   | TypeError: Argument 'index' is required to be an integer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-08-21
@bacon

1. We open the dock and read what VideoCapture takes as a parameter, so that it is "quite clear"
2. We publish the full traceback of errors, otherwise they are often not about what the questioner thinks about.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question