如何在Python中使用OpenCV从POST请求加载视频



我在Python Flask服务器中有REST API,它从POST请求中获取视频,并使用OpenCV执行一些情感识别,并返回带有情感数据的JSON响应。

但是OpenCV文档并没有提供任何从其字符串表示中读取视频的方法。它要么使用网络摄像头,要么使用url,但没有提及字符串表示中的视频。

我曾尝试将视频文件保存在本地服务器中,然后使用保存的文件完成处理,但这看起来效率不高,因为我们需要为多个用户扩展该功能。

有没有任何方法可以直接利用视频的字符串表示并逐帧处理它,而不是将其保存到本地并使用它。

以下是我尝试的方法。

@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

如果不是OpenCV,我愿意接受关于替代方案的建议,只是视频需要逐帧处理。

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

文件可在上找到

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

我不熟悉OpenCV,但您应该能够使用上面文档中的FileStorage流API,即request.files['file'].stream

如果这不起作用(我知道它对ZipFile不起作用,因为它需要一个类似文件的可查找对象(,您可以通过其._file属性(例如request.files['file']._file(直接访问文件对象。

更新

再次查看文档后(https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html(,并在本地重建上述应用程序,我很抱歉地说,看起来OpenCV实际上只接受路径或设备进行输入。至少是VideoCapture类。

最新更新