当前位置:网站首页>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()
边栏推荐
猜你喜欢
SQLite3 data storage location created by Android
Why does the updated DNS record not take effect?
神经网络与深度学习-5- 感知机-PyTorch
给刚入门或者准备转行网络工程师的朋友一些建议
Introduction to grpc for cloud native application development
QT build with built-in application framework -- Hello World -- use min GW 32bit
【目标跟踪】|DiMP: Learning Discriminative Model Prediction for Tracking
用户之声 | 对于GBase 8a数据库学习的感悟
图解网络:揭开TCP四次挥手背后的原理,结合男女朋友分手的例子,通俗易懂
break net
随机推荐
Tapdata 的 2.0 版 ,開源的 Live Data Platform 現已發布
保姆级教程:Azkaban执行jar包(带测试样例及结果)
子矩阵的和
Reading notes of Clickhouse principle analysis and Application Practice (7)
从cmath文件看名字是怎样被添加到命名空间std中的
cv2-drawline
快速熟知XML解析
QT -- package the program -- don't install qt- you can run it directly
Codeforces Round #649 (Div. 2)——A. XXXXX
《ClickHouse原理解析与应用实践》读书笔记(7)
能力贡献 GBASE三大解决方案入选“金融信创生态实验室-金融信创解决方案(第一批)”
The function of carbon brush slip ring in generator
PHP 计算个人所得税
【SolidWorks】修改工程图格式
Why does the updated DNS record not take effect?
Euler Lagrange equation
Mouse event - event object
Why did MySQL query not go to the index? This article will give you a comprehensive analysis
用户之声 | 冬去春来,静待花开 ——浅谈GBase 8a学习感悟
MySQL查询为什么没走索引?这篇文章带你全面解析