-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfileops.py
95 lines (68 loc) · 2.25 KB
/
fileops.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
import os,sys
import numpy as np
import csv, re
def get_keyframeslist(clip_dir, clip_path):
clip = clip_path.split('/')[-1]
clip_name = clip.split('.')[0]
keyframes_list = []
source = sorted(os.listdir(clip_dir))
for file in source:
if file.endswith(".jpg") and file.startswith(clip_name + '_keyframe'):
image = clip_dir + os.path.basename(file)
keyframes_list.append(image)
return keyframes_list
def atoi(text):
return int(text) if text.isdigit() else text
def natural_sorting(item):
return [atoi(c) for c in re.split('(\d+)', item)]
def get_pyframeslist(clip_dir, clip_name):
pyframes_list = []
source = sorted(os.listdir(clip_dir))
for file in source:
if file.endswith("IN.jpg") or file.endswith('OUT.jpg'):
image = clip_dir + os.path.basename(file)
pyframes_list.append(image)
pyframes_list.sort(key = natural_sorting)
return pyframes_list
def rename_frames(clip_dir, timestamps, keyframes, extra_timestamps, pyframes):
for timestamp, keyframe in zip(timestamps, keyframes):
timestamp = "{0:.3f}".format(float(timestamp))
os.rename(keyframe, clip_dir + timestamp + '.jpg')
for extra_timestamp, pyframe in zip(extra_timestamps, pyframes):
extra_timestamp = "{0:.3f}".format(float(extra_timestamp))
os.rename(pyframe, clip_dir + extra_timestamp + '.jpg')
image_files = []
new_times = []
source = sorted(os.listdir(clip_dir))
for file in source:
if file.endswith(".jpg"):
image = clip_dir + os.path.basename(file)
image_files.append(image)
fname = os.path.basename(file)
fname = fname.rsplit('.',1)[0]
fname = float(fname)
new_times.append(fname)
new_times.sort()
image_files.sort(key = natural_sorting)
return image_files, new_times
def save_features(filename, features):
np.savetxt(filename, features, fmt = '%.6f', delimiter=',')
def get_video_filename(clip_dir):
source = os.listdir(clip_dir)
mp4_flag = 0
for file in source:
if file.endswith(".mp4"):
if mp4_flag == 0:
clip_name = os.path.basename(file)
mp4_flag = 1
else:
print "Multiple mp4 files! Quitting..."
exit(0)
return clip_name
def get_cropped(clip_dir):
source = sorted(os.listdir(clip_dir))
images = []
for file in source:
if file.endswith('.jpg'):
images.append(clip_dir + file)
return images