简体   繁体   中英

How to load Video from POST request using OpenCV in Python?

I have REST API in Python Flask server which takes a video from the POST request and performs some emotional recognition using OpenCV and return a JSON response with the emotional data.

But the OpenCV documentation doesn't provide any way to read the Video from its string representation. It either takes webcam or a url but there is no mention about the video in string representation.

I have tried saving the video file in my server local and then using that saved file to complete my processing but this doesn't look efficient since we need to scale the feature for multiple users.

Is there any way to directly utilise the video in its string representation and process it frame by frame rather than saving it to local and using it.

Below is how I am trying to do it.

@app.route('/processCapturedVideo', methods = ['POST'])
        def processCapturedVideo():
        # Read as the video file from post request form data
        fileStr = request.files['file'].read() 
        # Process the video in string format using OpenCV and get the result
        return {'data': result}, 200

If not for OpenCV, I am open for suggestion regarding the alternatives, only that the video needs to processed frame by frame.

request.files['file'] is a Werkzeug.datastructures.filestorage .

Documentation can be found at

https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.FileStorage

I am not familiar with OpenCV, but you should be able to use FileStorage s stream API from above documentation, ie request.files['file'].stream

If that does not work (I know it does not work for ZipFile, as it needs a seekable file-like object), you can directly access the file object via its ._file attribute, eg request.files['file']._file .

Update

After having another look at the documentation ( https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html ) and rebuilding above app locally, I am sorry to say, it looks like as OpenCV actually only accepts either a path or a device for input. At least the VideoCapture class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM