如何使用Python从OpenCV中的字符串表示加载视频?

2024-04-19 10:23:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个restapi函数,它应该从post请求中获取一个视频,使用OpenCV处理视频并返回一个文本响应。我被困在从它的字符串表示读取视频。你知道吗

我看了一些描述如何在OpenCV中读取视频的文档,它们要么是从路径读取,要么是从网络摄像头读取。例如,来自imutils的cv2.VideoCapture或FileVideoStream都使用文件路径来加载视频。但是,我想避免冗余IO操作,不想先将视频写入文件。你知道吗

我项目的相关部分:

    @app.route('/processvideo', methods = ['POST'])
        def process_video():
        # read as string
        fStr = request.files['file'].read() # file is read as string from the request

        npimg = np.fromstring(fStr, np.uint8) # string data is converted to numpy array.

        # image = cv2.imdecode(npimg, cv2.IMREAD_COLOR) # this functions doesn't work, because it only takes image, not video.

        return jsonify( { 'output': 'test' } )

我在cli中发送测试请求,如下所示:

    curl -F 'file=@demo.mp4' http://localhost:5000/processvideo

我想一帧一帧地处理传入的视频,所以我需要帧作为图像。从现在开始谢谢你的帮助。你知道吗


Tags: 文件路径readstring视频isrequestas