cv2.VideoCapture无法读取来自前端的视频。

huangapple go评论81阅读模式
英文:

cv2.VideoCapture isn't read video coming from frontend

问题

@app.route('/process', methods=['POST'])
def process():
    if request.method == 'POST':
        vid = request.files['file']
        cap = cv2.VideoCapture(vid)
        while cap.isOpened():
            ret, frame = cap.read()
            if not ret:
                break

            do_something()

            k = cv2.waitKey(1)
            if k == 27:
                break
英文:
@app.route('/process', methods=['POST'])
def process():
    if request.method == 'POST':
        vid = request.files['file']
        cap = cv2.VideoCapture(vid)
        while(cap.isOpened()):
            ret,frame = cap.read()
            if not ret:
                break

            do_something()

            k = cv2.waitKey(1)
            if k == 27:
                break

I am sending a video from frontend to flask server to process it. But when I run this code, this error shows up on line cap = cv2.VideoCapture(vid):

TypeError: an integer is required (got type FileStorage)

The video coming is converted in class 'werkzeug.datastructures.FileStorage' and cv2.VideoCapture isn't accepting input as this class. What should I do?

I tried to save the video on local system using vid.save('abc.webm') and then read it using cv2.VideoCapture and it works perfectly. But I don't want to save it on system.

Please help. Thank you in advance.

答案1

得分: 1

你需要使用.read()方法来从FileStorage对象中获取字节。

vid = request.files['file'].read()
cap = cv2.VideoCapture(vid)
英文:

What you want is .read() to get the bytes from the FileStorage object.

vid = request.files['file'].read()
cap = cv2.VideoCapture(vid)

huangapple
  • 本文由 发表于 2020年1月6日 20:27:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定