当前位置:网站首页>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()
边栏推荐
- Apache multiple component vulnerability disclosure (cve-2022-32533/cve-2022-33980/cve-2021-37839)
- Chapter 7 behavior level modeling
- MySQL数据库(2)
- 从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
- cv2读取视频-并保存图像或视频
- 用户之声 | 对于GBase 8a数据库学习的感悟
- 2022国内十大工业级三维视觉引导企业一览
- [target tracking] |atom
- Get familiar with XML parsing quickly
- MATLAB R2021b 安装libsvm
猜你喜欢

The foreach map in JS cannot jump out of the loop problem and whether foreach will modify the original array

Sum of submatrix

MATLAB R2021b 安装libsvm
![[target tracking] |atom](/img/33/529b483a0a848e0e4263ba24462d5c.png)
[target tracking] |atom

Why did MySQL query not go to the index? This article will give you a comprehensive analysis

ClickHouse原理解析与应用实践》读书笔记(8)

The function of carbon brush slip ring in generator

从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值

How to make the conductive slip ring signal better

Get familiar with XML parsing quickly
随机推荐
Android 创建的sqlite3数据存放位置
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
nmap工具介紹及常用命令
如何制作企业招聘二维码?
Qt - - Packaging Programs - - Don't install Qt - can run directly
Chapter 7 behavior level modeling
About snake equation (1)
《ClickHouse原理解析与应用实践》读书笔记(7)
regular expression
mysql/mariadb怎样生成core文件
Redux使用
DataWorks值班表
剑指 Offer II 041. 滑动窗口的平均值
【目标跟踪】|atom
Is NPDP recognized in China? Look at it and you'll see!
How mysql/mariadb generates core files
Redisson distributed lock unlocking exception
液压旋转接头的使用事项
系统测试的类型有哪些,我给你介绍
Cross modal semantic association alignment retrieval - image text matching