在Python中从视频中提取图像
OpenCV 带有许多强大的视频编辑功能。在当前场景中,图像扫描、人脸识别等技术可以使用 OpenCV 完成。
图像分析是计算机视觉领域中一个非常常见的领域。它是从视频或图像中提取有意义的信息。 OpenCv 库可用于对视频执行多种操作。
需要的模块:
import cv2
import os
使用的函数:
read() : Read data depending upon the type of object that calls imwrite(filename, img[, params]) : Saves an image to a specified file.VideoCapture(File_path) : Read the video(.mp4 format)
下面是实现:
# Importing all necessary libraries
import cv2
import os
# Read the video from specified path
cam = cv2.VideoCapture("C:\\Users\\Admin\\PycharmProjects\\project_1\\openCV.mp4")
try:
# creating a folder named data
if not os.path.exists('data'):
os.makedirs('data')
# if not created then raise error
except OSError:
print ('Error: Creating directory of data')
# frame
currentframe = 0
while(True):
# reading from frame
ret,frame = cam.read()
if ret:
# if video is still left continue creating images
name = './data/frame' + str(currentframe) + '.jpg'
print ('Creating...' + name)
# writing the extracted images
cv2.imwrite(name, frame)
# increasing counter so that it will
# show how many frames are created
currentframe += 1
else:
break
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
输出:
所有提取的图像都将保存在系统上名为“data”的文件夹中。