当前位置:网站首页>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()
边栏推荐
- Sum of submatrix
- body有8px的神秘边距
- MySQL查询为什么没走索引?这篇文章带你全面解析
- QT build with built-in application framework -- Hello World -- use min GW 32bit
- Uniapp one click Copy function effect demo (finishing)
- 碳刷滑环在发电机中的作用
- GBASE观察 | 数据泄露频发 信息系统安全应如何守护
- QT -- package the program -- don't install qt- you can run it directly
- Codeforces Round #649 (Div. 2)——A. XXXXX
- common commands
猜你喜欢
Why does the updated DNS record not take effect?
为什么更新了 DNS 记录不生效?
burpsuite
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
Optimization of ecological | Lake Warehouse Integration: gbase 8A MPP + xeos
Working principle of stm32gpio port
burpsuite
ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
Remote Sensing投稿經驗分享
nmap工具介绍及常用命令
随机推荐
adb工具介绍
Remote sensing contribution experience sharing
发现值守设备被攻击后分析思路
[target tracking] |dimp: learning discriminative model prediction for tracking
Cross modal semantic association alignment retrieval - image text matching
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
Redisson distributed lock unlocking exception
碳刷滑环在发电机中的作用
Optimization of ecological | Lake Warehouse Integration: gbase 8A MPP + xeos
Why does the updated DNS record not take effect?
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
C language - modularization -clion (static library, dynamic library) use
Summary of log feature selection (based on Tianchi competition)
Urban land use distribution data / urban functional zoning distribution data / urban POI points of interest / vegetation type distribution
cv2读取视频-并保存图像或视频
鼠标事件-事件对象
PHP 计算个人所得税
ROS problems (topic types do not match, topic datatype/md5sum not match, MSG XXX have changed. rerun cmake)
Uniapp one click Copy function effect demo (finishing)
由排行榜实时更新想到的数状数值