当前位置:网站首页>CV2 read video - and save image or video
CV2 read video - and save image or video
2022-07-08 01:52:00 【step step】
title: cv2 Read video , And save images or videos
date: 2022-07-02 18:10:24
tags: opencv
@TOC
cv2 Read video , And save images or videos
cv2 The general process of reading video
- Get video cap = cv2.VideoCapture()
- Judge whether the obtained video is successful , If the video object is successfully read, it returns True. cap.isOpened()
- Read by frame ret, frame = cap.read()
- Show the image cv2.waitKey(1)
Read the local file video and show
from turtle import color
import numpy as np
import cv2 as cv
import cv2
def read_video_show(videoName):
''' opencv Read video cv2.VideoCapture(filename) # Read local video cv2.VideoCapture(index) # Get the camera '''
#1. Read video
cap = cv2.VideoCapture(videoName) # If the parameter is 0, Local camera
#2. Judge whether the read video stream is successful
while cap.isOpened(): # When successful
#3. Get images per frame
ret, frame = cap.read() # If successful ,ret by True, Otherwise False;frame It's an image
if ret: # Successfully obtained image
cv2.imshow('frame', frame) # Two parameters , One is the name of the display , One is pixel content
key = cv2.waitKey(25) # Stop 25ms, When it comes to 0 The jam will not continue in the first frame
if key == ord(' ') or key == ord('q'): # When typing a space or q when , The exit while loop
break
cap.release() # Release video
cv2.destroyAllWindows() # Release all windows that display images
def read_video_save(videoName):
''' opencv Save the video cv2.VideoWriter(filename, fourcc, fps, frameSize, [isColor]) '''
cap = cv2.VideoCapture(videoName)
# Video properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # Get the width of the original video
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # Get the original video
fps = int(cap.get(cv2.CAP_PROP_FPS)) # Frame rate
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC)) # Video coding
# Output of video object
out = cv2.VideoWriter('video_output.avi', fourcc, 20.0, (width, height))
# out = cv2.VideoWriter('out.avi', fourcc, 20.0, (width, height))
while cap.isOpened():
ret, frame = cap.read()
cv2.imshow('fame', frame)
key = cv2.waitKey(25)
out.write(frame) # Write video
if key == ord('q'):
break
cap.release() # Release video
out.release()
cv2.destroyAllWindows() # Release all display windows
def read_video_write(videoName):
''' Read the video and save the screenshot cv2.imwrite(filename, frame) '''
cap = cv2.VideoCapture(videoName)
# Video properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # Get the width of the original video
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # Get the original video
fps = int(cap.get(cv2.CAP_PROP_FPS)) # Frame rate
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC)) # Video coding
n, i = 0, 0 # Total frames , Save the second i A picture
while cap.isOpened():
ret, frame = cap.read()
if ret:
n += 1
if n % fps == 0:
i += 1
filename = '{:0>4}.jpg'.format(str(i))
cv2.imwrite(filename, frame) # Save to snapshot
cv2.imshow('frame', frame)
key = cv2.waitKey(25)
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
边栏推荐
- Android 创建的sqlite3数据存放位置
- 子矩阵的和
- cv2读取视频-并保存图像或视频
- 日志特征选择汇总(基于天池比赛)
- QT -- package the program -- don't install qt- you can run it directly
- GBASE观察 | 数据泄露频发 信息系统安全应如何守护
- 剑指 Offer II 041. 滑动窗口的平均值
- The method of using thread in PowerBuilder
- Apache multiple component vulnerability disclosure (cve-2022-32533/cve-2022-33980/cve-2021-37839)
- common commands
猜你喜欢

Voice of users | winter goes and spring comes, waiting for flowers to bloom -- on gbase 8A learning comprehension

ANSI / nema- mw- 1000-2020 magnetic iron wire standard Latest original

云原生应用开发之 gRPC 入门

Summary of log feature selection (based on Tianchi competition)

How to make enterprise recruitment QR code?

剑指 Offer II 041. 滑动窗口的平均值

图解网络:揭开TCP四次挥手背后的原理,结合男女朋友分手的例子,通俗易懂

Introduction to grpc for cloud native application development

Graphic network: uncover the principle behind TCP's four waves, combined with the example of boyfriend and girlfriend breaking up, which is easy to understand

Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
随机推荐
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
QT build with built-in application framework -- Hello World -- use min GW 32bit
body有8px的神秘边距
Voice of users | winter goes and spring comes, waiting for flowers to bloom -- on gbase 8A learning comprehension
Why does the updated DNS record not take effect?
C language - modularization -clion (static library, dynamic library) use
About snake equation (1)
给刚入门或者准备转行网络工程师的朋友一些建议
Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
【目标跟踪】|DiMP: Learning Discriminative Model Prediction for Tracking
【SolidWorks】修改工程图格式
nmap工具介绍及常用命令
Redismission source code analysis
Version 2.0 of tapdata, the open source live data platform, has been released
adb工具介绍
Tapdata 的 2.0 版 ,開源的 Live Data Platform 現已發布
How mysql/mariadb generates core files
MySQL查询为什么没走索引?这篇文章带你全面解析
Application of slip ring in direct drive motor rotor