-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoToPicture.py
102 lines (79 loc) · 2.37 KB
/
VideoToPicture.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import cv2
from os import makedirs, path
# 影片檔名
VideoName = '153749'
# 影片全路徑
VideoPath = f'./VideoToPicture/20210325/videoCam0/{VideoName}.avi'
# 取樣幀數間隔
FrameDelta = 150
# 圖像resize尺寸
SizeW = 640
SizeH = 480
# 存檔資料夾
SaveFolder = './VideoToPicture/JPEGImages/'
# 存檔名稱開頭
SaveFileNameHeader = f'NN_{VideoName}_'
# 存檔名稱起始編號
SaveNumStart = 0
# 存檔名稱數字位數 (zfill)
ZfillNum = 6
def GetImagesFromVideo(VideoPath, deltaF):
frameNum = 0
saveNum = 0
video_images = []
# 確認存檔資料夾存在
if CheckDir(SaveFolder) == False:
return
# 判斷是否開啟影片
vc = cv2.VideoCapture(VideoPath)
if vc.isOpened():
rval = True
else:
rval = False
# 擷取視頻至結束
while rval:
rval, video_frame = vc.read()
# 每一幀+1
frameNum = frameNum + 1
# 讀取不到影像則跳過
if rval == False:
continue
# 每隔幾幀進行擷取
if(frameNum % deltaF == 0):
# 每保存一張saveNum+1
saveNum = saveNum + 1
# 存檔檔名
saveFileName = SaveFolder + SaveFileNameHeader + str(SaveNumStart + saveNum).zfill(ZfillNum)+ '.jpg'
# 圖像格式化
video_frame_format = cv2.resize(video_frame, (SizeW,SizeH))
# 保存圖像
saveImage(video_frame_format, saveFileName)
video_images.append(video_frame_format)
# 顯示圖像
# cv2.imshow('windows', video_images[i])
# cv2.waitKey(100)
vc.release()
cv2.destroyAllWindows
return video_images
def saveImage(image,saveFilePath):
print('save image: ',saveFilePath)
cv2.imwrite(saveFilePath,image)
def CheckDir(dirPath):
try:
if path.exists(dirPath):
return True
else:
print("路徑不存在。")
print(f"建立路徑:{dirPath}")
makedirs(dirPath)
return True
except FileExistsError:
print("路徑已存在")
return True
except PermissionError:
# 權限不足的例外處理
print("權限不足")
return False
if __name__ =="__main__":
# 讀取影片並轉成圖片
video_images = GetImagesFromVideo(VideoPath, FrameDelta)