美文网首页pythonodoo
python 读取视频帧,得到图片(二)

python 读取视频帧,得到图片(二)

作者: 隔壁小红馆 | 来源:发表于2019-12-27 15:11 被阅读0次
方式二:通过cv2库
  1. 安装环境:
    因为没有直接的cv2库,可用 opencv-python 代替
    pip install opencv-python

  2. 使用cv2库提取视频帧

import cv2
video_full_path="video/01_01.wmv"
cap = cv2.VideoCapture(video_full_path)
print cap.isOpened()
frame_count = 1
success = True
while(success):
success, frame = cap.read()
print 'Read a new frame: ', success
params = []
#params.append(cv.CV_IMWRITE_PXM_BINARY)
params.append(1)
cv2.imwrite("video" + "_%d.jpg" % frame_count, frame, params)
frame_count = frame_count + 1
cap.release()

这是我自己写的一个cv2提取帧(有临时文件夹产生 TemporaryDirectory()

    def _parse_thzcm_document(self, document_id, only_preview_fields):
        import cv2
        from tempfile import TemporaryDirectory
        file = self.env['muk_dms.file'].search([('id', '=', document_id)])
        # vidcap = cv2.VideoCapture('D:\\weixin\\xiaohong\\vi.mp4')
        vidcap = cv2.VideoCapture('D:\\weixin\\xiaohong\\test.mp4')
        if vidcap.isOpened():
            success, image = vidcap.read()
            with TemporaryDirectory() as temp_folder:
                cv2.imwrite(temp_folder+'\\test.jpg', image)
                image_id = tools.image_resize_image_big(base64.b64encode(open(temp_folder + '\\test.jpg', 'rb').read()))
                values = {'slide_type': 'video', 'image': image_id, 'document_id': document_id}
                return {'values': values}
        else:
            # return {'error': _('Unknown document')}
            values = {'document_id': document_id}
            return {'values': values}

看完记得点赞哦,笔芯

相关文章

网友评论

    本文标题:python 读取视频帧,得到图片(二)

    本文链接:https://www.haomeiwen.com/subject/njjdoctx.html