Python-OpenCV 处理视频(一): 输入输出
admin
2023-07-31 01:52:46
0

视频的处理和图片的处理类似,只不过视频处理需要连续处理一系列图片。

一般有两种视频源,一种是直接从硬盘加载视频,另一种是获取摄像头视频。

0x00. 本地读取视频

核心函数:

cv.CaptureFromFile()

代码示例:

import cv2.cv as cv

capture = cv.CaptureFromFile(\'myvideo.avi\')

nbFrames = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT))

#CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream
#CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream

fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)

wait = int(1/fps * 1000/1)

duration = (nbFrames * fps) / 1000

print \'Num. Frames = \', nbFrames
print \'Frame Rate = \', fps, \'fps\'
print \'Duration = \', duration, \'sec\'

for f in xrange( nbFrames ):
    frameImg = cv.QueryFrame(capture)
    print cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_POS_FRAMES)
    cv.ShowImage(\"The Video\", frameImg)
    cv.WaitKey(wait)

cv2

import numpy as np
import cv2

cap = cv2.VideoCapture(\'vtest.avi\')

while(cap.isOpened()):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow(\'frame\',gray)
    if cv2.waitKey(1) & 0xFF == ord(\'q\'):
        break

cap.release()
cv2.destroyAllWindows()

0x01. 摄像头视频读取

核心函数:

cv.CaptureFromCAM()

示例代码:

import cv2.cv as cv

capture = cv.CaptureFromCAM(0)

while True:
    frame = cv.QueryFrame(capture)
    cv.ShowImage(\"Webcam\", frame)
    c = cv.WaitKey(1)
    if c == 27: #Esc on Windows
        break

cv2

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow(\'frame\',gray)
    if cv2.waitKey(1) & 0xFF == ord(\'q\'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

0x02. 写入视频

摄像头录制视频

import cv2.cv as cv

capture=cv.CaptureFromCAM(0)
temp=cv.QueryFrame(capture)
writer=cv.CreateVideoWriter(\"output.avi\", cv.CV_FOURCC(\"D\", \"I\", \"B\", \" \"), 5, cv.GetSize(temp), 1)
#On linux I used to take \"M\",\"J\",\"P\",\"G\" as fourcc

count=0
while count<50:
    print count
    image=cv.QueryFrame(capture)
    cv.WriteFrame(writer, image)
    cv.ShowImage(\'Image_Window\',image)
    cv.WaitKey(1)
    count+=1

从文件中读取视频并保存

import cv2.cv as cv
capture = cv.CaptureFromFile(\'img/mic.avi\')

nbFrames = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT))
width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))
fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)
codec = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FOURCC)

wait = int(1/fps * 1000/1) #Compute the time to wait between each frame query

duration = (nbFrames * fps) / 1000 #Compute duration

print \'Num. Frames = \', nbFrames
print \'Frame Rate = \', fps, \'fps\'

writer=cv.CreateVideoWriter(\"img/new.avi\", int(codec), int(fps), (width,height), 1) #Create writer with same parameters

cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_POS_FRAMES,80) #Set the number of frames

for f in xrange( nbFrames - 80 ): #Just recorded the 80 first frames of the video

    frame = cv.QueryFrame(capture)

    print cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_POS_FRAMES)

    cv.WriteFrame(writer, frame)

    cv.WaitKey(wait)

cv2

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*\'XVID\')
out = cv2.VideoWriter(\'output.avi\',fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

        cv2.imshow(\'frame\',frame)
        if cv2.waitKey(1) & 0xFF == ord(\'q\'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
项目管理和工程管理的区别 项目管理 项目管理,顾名思义就是专注于开发和完成项目的管理,以实现目标并满足成功标准和项目要求。 工...