使用 OpenCV 从网络摄像头保存操作视频
OpenCV是一个庞大的库,有助于为图像和视频操作提供各种功能。使用 OpenCV,我们可以对输入视频进行操作。 OpenCV 还允许我们保存该操作的视频以供进一步使用。为了保存图像,我们使用 cv2.imwrite() 将图像保存到指定的文件位置。但是,为了保存录制的视频,我们创建了一个 Video Writer 对象。
首先,我们指定fourcc 变量。 FourCC 是一个 4 字节的代码,用于指定视频编解码器。代码列表可以在 FourCC 的 Video Codecs 获得。 Windows 的编解码器是DIVX ,OSX 的编解码器是 avc1、h263。 FourCC 代码作为MJPG 的 cv2.VideoWriter_fourcc(*'MJPG')和 DIVX的 cv2.VideoWriter_fourcc(*'XVID') 传递。
然后,使用 cv2.VideoWriter()函数。
cv2.VideoWriter( filename, fourcc, fps, frameSize )
参数是:
- filename:指定输出视频文件的名称。
- Fourcc:(用于录制)定义编解码器
- fps:输出视频流的定义帧率
- frameSize:视频帧的大小
# Python program to illustrate
# saving an operated video
# organize imports
import numpy as np
import cv2
# This will return video from the first webcam on your computer.
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))
# loop runs if capturing has been initialized.
while(True):
# reads frames from a camera
# ret checks return at each frame
ret, frame = cap.read()
# Converts to HSV color space, OCV reads colors as BGR
# frame is converted to hsv
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# output the frame
out.write(hsv)
# The original input frame is shown in the window
cv2.imshow('Original', frame)
# The window showing the operated video stream
cv2.imshow('frame', hsv)
# Wait for 'a' key to stop the program
if cv2.waitKey(1) & 0xFF == ord('a'):
break
# Close the window / Release webcam
cap.release()
# After we release our webcam, we also release the output
out.release()
# De-allocate any associated memory usage
cv2.destroyAllWindows()
输出:
输出屏幕显示两个窗口。名为“原始”的窗口显示输入帧,而“帧”窗口显示操作的视频序列。
此外,视频会以预定义的帧速率和帧大小以名称“输出”记录并保存在同一文件位置。
它通常是 .avi 格式。保存的视频是这样的:输出视频
输入视频也可以在其他颜色空间中操作,例如灰度
# Python program to illustrate
# saving an operated video
# organize imports
import numpy as np
import cv2
# This will return video from the first webcam on your computer.
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))
# loop runs if capturing has been initialized.
while(True):
# reads frames from a camera
# ret checks return at each frame
ret, frame = cap.read()
# Converts to grayscale space, OCV reads colors as BGR
# frame is converted to gray
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# output the frame
out.write(gray)
# The original input frame is shown in the window
cv2.imshow('Original', frame)
# The window showing the operated video stream
cv2.imshow('frame', gray)
# Wait for 'a' key to stop the program
if cv2.waitKey(1) & 0xFF == ord('a'):
break
# Close the window / Release webcam
cap.release()
# After we release our webcam, we also release the out-out.release()
# De-allocate any associated memory usage
cv2.destroyAllWindows()
输出:
该操作视频的视频文件保存在与我们在上面看到的相同的文件位置。
这种方法可以帮助我们为项目/模型中的训练数据创建自己的数据集,从我们的网络摄像头记录并进行必要的操作,还可以创建不同色彩空间的视频。
请参考此链接以可视化不同颜色空间中的内容:
https://www.geeksforgeeks.org/python-visualizing-image-in-different-color-spaces/
参考:
- https://docs.opencv.org/3.4/dd/d9e/classcv_1_1VideoWriter.html
- https://docs.opencv.org/3.1.0/dd/d43/tutorial_py_video_display.html
- https://en.wikipedia.org/wiki/FourCC
- https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html