-
Notifications
You must be signed in to change notification settings - Fork 35
/
clean.py
187 lines (158 loc) · 6.54 KB
/
clean.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'''
AAA lllllll lllllll iiii
A:::A l:::::l l:::::l i::::i
A:::::A l:::::l l:::::l iiii
A:::::::A l:::::l l:::::l
A:::::::::A l::::l l::::l iiiiiii eeeeeeeeeeee
A:::::A:::::A l::::l l::::l i:::::i ee::::::::::::ee
A:::::A A:::::A l::::l l::::l i::::i e::::::eeeee:::::ee
A:::::A A:::::A l::::l l::::l i::::i e::::::e e:::::e
A:::::A A:::::A l::::l l::::l i::::i e:::::::eeeee::::::e
A:::::AAAAAAAAA:::::A l::::l l::::l i::::i e:::::::::::::::::e
A:::::::::::::::::::::A l::::l l::::l i::::i e::::::eeeeeeeeeee
A:::::AAAAAAAAAAAAA:::::A l::::l l::::l i::::i e:::::::e
A:::::A A:::::A l::::::ll::::::li::::::ie::::::::e
A:::::A A:::::A l::::::ll::::::li::::::i e::::::::eeeeeeee
A:::::A A:::::A l::::::ll::::::li::::::i ee:::::::::::::e
AAAAAAA AAAAAAAlllllllllllllllliiiiiiii eeeeeeeeeeeeee
_____ _ _ ___ ______ _____
/ __ \ | (_) / _ \ | ___ \_ _| _
| / \/ | ___ __ _ _ __ _ _ __ __ _ / /_\ \| |_/ / | | (_)
| | | |/ _ \/ _` | '_ \| | '_ \ / _` | | _ || __/ | |
| \__/\ | __/ (_| | | | | | | | | (_| | | | | || | _| |_ _
\____/_|\___|\__,_|_| |_|_|_| |_|\__, | \_| |_/\_| \___/ (_)
__/ |
|___/
_ _ _ _
| | | (_) | |
| | | |_ __| | ___ ___
| | | | |/ _` |/ _ \/ _ \
\ \_/ / | (_| | __/ (_) |
\___/|_|\__,_|\___|\___/
This section of Allie's API cleans folders of video files
using the default_video_cleaners.
Usage: python3 clean.py [folder] [cleantype]
All cleantype options include:
["clean_alignfaces", "clean_videostabilize"]
Read more @ https://github.com/jim-schwoebel/allie/tree/master/cleaning/video_cleaning
'''
################################################
## IMPORT STATEMENTS ##
################################################
import json, os, sys, time, random, uuid
import numpy as np
# import helpers.transcribe as ts
# import speech_recognition as sr
from tqdm import tqdm
def prev_dir(directory):
g=directory.split('/')
dir_=''
for i in range(len(g)):
if i != len(g)-1:
if i==0:
dir_=dir_+g[i]
else:
dir_=dir_+'/'+g[i]
# print(dir_)
return dir_
################################################
## Helper functions ##
################################################
def video_clean(cleaning_set, videofile, basedir):
# long conditional on all the types of features that can happen and featurizes accordingly.
if cleaning_set == 'clean_alignfaces':
clean_alignfaces.clean_alignfaces(videofile, basedir)
elif cleaning_set == 'clean_videostabilize':
clean_videostabilize.clean_videostabilize(videofile)
################################################
## Load main settings ##
################################################
# directory=sys.argv[1]
basedir=os.getcwd()
settingsdir=prev_dir(basedir)
settingsdir=prev_dir(settingsdir)
settings=json.load(open(settingsdir+'/settings.json'))
os.chdir(basedir)
video_transcribe=settings['transcribe_video']
default_video_transcribers=settings['default_video_transcriber']
try:
# assume 1 type of feature_set
cleaning_sets=[sys.argv[2]]
except:
# if none provided in command line, then load deafult features
cleaning_sets=settings['default_video_cleaners']
################################################
## Import According to settings ##
################################################
# only load the relevant featuresets for featurization to save memory
if 'clean_alignfaces' in cleaning_sets:
import clean_alignfaces
if 'clean_videostabilize' in cleaning_sets:
import clean_videostabilize
################################################
## Get featurization folder ##
################################################
foldername=sys.argv[1]
os.chdir(foldername)
listdir=os.listdir()
random.shuffle(listdir)
cur_dir=os.getcwd()
help_dir=basedir+'/helpers/'
# get class label from folder name
labelname=foldername.split('/')
if labelname[-1]=='':
labelname=labelname[-2]
else:
labelname=labelname[-1]
################################################
## REMOVE JSON AND DUPLICATES ##
################################################
deleted_files=list()
# rename files appropriately
for i in range(len(listdir)):
os.rename(listdir[i],listdir[i].replace(' ',''))
# remove duplicates / json files
for i in tqdm(range(len(listdir)), desc=labelname):
file=listdir[i]
listdir2=os.listdir()
#now sub-loop through all files in directory and remove duplicates
for j in range(len(listdir2)):
try:
if listdir2[j]==file:
pass
elif listdir2[j]=='.DS_Store':
pass
else:
if filecmp.cmp(file, listdir2[j])==True:
print('removing duplicate: %s ____ %s'%(file,listdir2[j]))
deleted_files.append(listdir2[j])
os.remove(listdir2[j])
else:
pass
except:
pass
print('deleted the files below')
print(deleted_files)
listdir=os.listdir()
for i in tqdm(range(len(listdir))):
# remove .JSON files
if listdir[i].endswith('.json'):
os.remove(listdir[i])
# now rename files with UUIDs
listdir=os.listdir()
for i in range(len(listdir)):
file=listdir[i]
os.rename(file, str(uuid.uuid4())+file[-4:])
################################################
## NOW CLEAN!! ##
################################################
listdir=os.listdir()
random.shuffle(listdir)
# featurize all files accoridng to librosa featurize
for i in tqdm(range(len(listdir)), desc=labelname):
if listdir[i][-4:] in ['.mp4']:
filename=[listdir[i]]
for j in range(len(cleaning_sets)):
for k in range(len(filename)):
cleaning_set=cleaning_sets[j]
filename=video_clean(cleaning_set, filename[k], basedir)