# 提起整個訓練集的視頻,並每隔5幀將圖片保存起來
import cv2
import os
TIMESEP = 25 # 讀取的幀數為:25幀
def capture(root, timeSep):
# 視頻文件大概是5秒,一秒為28幀左右,一個視頻共有130幀,最終讀取25幀
i = 0 # 進行計數
c = 0 # 圖片名稱
counter = 0 # 讀取的幀數
# success = False # 讀取圖片是否成功
cap = cv2.VideoCapture(root)
if cap.isOpened():
success, frame = cap.read()
# 保存圖片到指定文件夾
cv2.imwrite('/home/wbx/photoes/' + str(c) + '.jpg', frame)
c += 1
i += 1
counter += 1
while success:
success, frame = cap.read()
if i % 5 == 0: # 每5幀讀取一次
cv2.imwrite('/home/wbx/photoes/' + str(c) + '.jpg', frame)
c += 1
counter += 1
if counter == timeSep:
break
i += 1
cv2.waitKey(1)
Myroot = "/home/wbx/RLVD/Violence/V_90.mp4"
main_dir = "/Users/wbx/Downloads/RLVD"
savePath = "/home/wbx/RLVD"
def findRoot(main_dir):
rootList = []
flagList = []
for x in os.listdir(main_dir):
print(type(x))
if x != ".DS_Store":
td = main_dir + '/' + x + '/'
print(td)
for file in os.listdir(td):
# print(file)
filePath = os.path.join(td, file) # 獲取視頻的完整路徑
# print(filePath)
rootList.append(filePath)
flag = file.split('_')
print(type(flag[0])) # flag[0]為:NV 或者 V 可以用來判斷是否是暴力行為
flagList.append(flag[0])
return rootList, flagList
capture(root=Myroot, timeSep=TIMESEP)
rootList, flagList = findRoot(main_dir=main_dir)
print(len(rootList))