I am writing simple REST API using Flask, which reads uploaded video and returns some JSON. Video is sent as file by POST request, with content type application/x-www-form-urlencoded.
I want to process it using opencv, but I do not know how to read it from raw binary string.
The only solution I came up with, is to save video to file and read it using opencv VideoCapture class. However I would like to avoid IO operations, because I think it will slow down my API.
So my question is:
How can I transform video from raw bytes representation to opencv representation (list of numpy arrays)?
I know this is a little too late to answer this question. Actually I don't know how do you receive video as string. but I tried to simulate your situation. I opened an image as a bytestring
f = open('image.jpg','rb')
data = f.read()
f.close()
print(data)
The output is
b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00`\x00`\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e\x1d\x1a\x1c\x1c $.\' ",#\x1c\x1c(7),01444\x1f\'9=82<.342\xff\xdb\x00C\x01\t\t\t\x0c\x0b\x0c\x18\r\r\x182!\x1c!22222222222222222222222222222222222222222222222222\xff\xc0\x00\x11\x08\x01q\x01n\x03\x01"\x00\x02\...
and then converted to numpy array:
img_array = np.asarray(bytearray(data), dtype=np.uint8)
im = cv2.imdecode(img_array, 0)
cv2.imshow("test", im)
cv2.waitKey()
and it shows image. hope this be helpful
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With