-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathConvert.py
64 lines (52 loc) · 1.85 KB
/
Convert.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
import os
import cv2
from threading import Thread
class Convert:
# States
file = ''
subFolder = ''
savePath = ''
frames = 0
fps = 0
duration = 0
count = 0
framesCaptured = 0
def __init__(self):
self.count = 1
self.framesCaptured = 0
def items(self):
items = []
with os.scandir(self.savePath) as dir:
for entries in dir:
items.append(entries.name)
return items
def start(self, savePath, file):
self.stopped = False
# File Attributes
self.file = cv2.VideoCapture(file)
self.subFolder = os.path.splitext(file)[0]
self.savePath = os.path.join(savePath, self.subFolder)
self.base = os.path.basename(self.subFolder)
self.frames = int(self.file.get(cv2.CAP_PROP_FRAME_COUNT))
self.fps = int(self.file.get(cv2.CAP_PROP_FPS))
self.duration = int(self.frames / self.fps)
t = Thread(target = self.run)
t.start()
def stop(self):
self.stopped = True
def run(self):
# If subfolder doesnt exist, make directory
if not os.path.exists(self.savePath):
print('making dir', self.savePath)
os.mkdir(self.savePath)
while not self.stopped:
filename = str(self.base).lower() + '-' + str(len(self.items())) + '.png'
fileOut = os.path.join(self.savePath, filename)
ret, frame = self.file.read()
if self.count % self.fps == 0:
cv2.imwrite(fileOut, frame)
self.framesCaptured += 1
# print('Images Saved:', str(self.framesCaptured) + '/' + str(self.duration))
if self.frames == self.count:
self.stopped = True
self.count += 1