![](/img/trans.png)
[英]Is it possible to read a Video from POST request into python-ffmpeg or OpenCV?
[英]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.
但是 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
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
如果這不起作用(我知道它不適用於 ZipFile,因為它需要一個可查找的類似文件的對象),您可以通過其._file
屬性直接訪問文件 object,例如request.files['file']._file
.
更新
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輸入的路徑或設備。 至少VideoCapture
class。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.