安裝好python ,配置好opencv-python
# 匯入所有必要的庫
import cv2
import os
# 從指定的路徑讀取視頻
cam = cv2.VideoCapture("F:/video1.mp4")
try:
# 創建名為data的檔案夾
if not os.path.exists('data'):
os.makedirs('data')
# 如果未創建,則引發錯誤
except OSError:
print('Error: Creating directory of data')
# 定義保存圖片函式
# image:要保存的圖片名字
# addr;圖片地址與相片名字的前部分
# num: 相片,名字的后綴,int 型別
def save_image(image, addr, num):
address = addr + str(num) + '.jpg'
cv2.imwrite(address, image)
# reading from frame
ret, frame = cam.read() # ret為布林值 frame保存著視頻中的每一幀影像 是個三維矩陣
i = 0
timeF = 15 # 設定要保存影像的間隔 15為每隔15幀保存一張影像
j = 0
while ret:
i = i + 1
# 如果視頻仍然存在,繼續創建影像
if i % timeF == 0:
# 呈現輸出圖片的數量
j = j + 1
save_image(frame, './data/', j)
print('save image:', j)
ret, frame = cam.read()
# 一旦完成釋放所有的空間和視窗
cam.release()
cv2.destroyAllWindows()
效果如下:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/281261.html
標籤:python
上一篇:Java程式的邏輯控制和方法
