-
Notifications
You must be signed in to change notification settings - Fork 9
/
filesystem_changes.py
142 lines (107 loc) · 4.21 KB
/
filesystem_changes.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
import re
import os
import glob
import shutil
import string_manipulation as sm
# CREATING A FOLDER THAT WILL STORE SUB-FOLDER DATA
def create_root_save_directory():
path = os.path.join('./', 'Project Saves')
try:
os.mkdir(path)
except OSError as oe:
print(oe)
# CREATING SUB-FOLDERS WITH DATE AND TIME AS A NAME
def create_sub_save_folder():
date, time = sm.get_date_time()
path = os.path.join('./Project Saves/', f'Date {date}')
try:
os.mkdir(path)
except OSError as oe:
print(oe)
path = os.path.join(f'./Project Saves/Date {date}/', f'Time {time}')
try:
os.mkdir(path)
except OSError as oe:
print(oe)
return path
# RETURNS ALL CSV LINEUPS FROM LAST CREATED FOLDER
def find_last_folder_lineups():
all_files = os.listdir(sm.double_backslash_to_slash(find_last_created_folder()))
lineups = []
for file in all_files:
if re.search(r'\blineup\b', file):
lineups.append(file)
return lineups
# CHECKING IF THE GIVEN PATH IS A VALID FILE
def check_if_file_exists(file):
return os.path.isfile(file)
# RETURNS THE PATH FOR THE FOLDER THAT WAS CREATED LAST INSIDE A GIVEN DIRECTORY
def find_last_created_folder():
last_date_path = max(glob.glob(os.path.join('./Project Saves/', '*/')), key=os.path.getmtime)
last_time_path = max(glob.glob(os.path.join(last_date_path, '*/')), key=os.path.getmtime)
return last_time_path
# RETURNS LAST CREATED WEIGHTS FOLDER FOR THE YOLO-V5 WEIGHTS
def find_yolo_weight_folder():
os.chdir('./player_detection')
weights_folder = max(glob.glob(os.path.join('yolov5/runs/train/exp55', '*/*.pt')), key=os.path.getmtime)
os.chdir('..')
return weights_folder
# RETURNING THE DOWNLOADS
def downloads_path():
home = os.path.expanduser('~')
download_path = os.path.join(home, 'Downloads')
return download_path
# DELETE FILE
def delete_file(file_path, file_name):
os.remove(file_path + file_name)
# DELETE ALL FILES FROM FOLDER AND THEN THE FOLDER ITSELF
def delete_files_and_folder(folder_path):
try:
for file_name in os.listdir(folder_path):
file = os.path.join(folder_path, file_name)
try:
if os.path.isfile(file) or os.path.islink(file):
os.unlink(file)
elif os.path.isdir(file):
shutil.rmtree(file)
except FileNotFoundError as fe:
print(fe)
os.rmdir(folder_path)
except FileNotFoundError as fe:
print(fe)
# CREATE FOLDER FOR ZOOMED VIDEO VERSIONS
def create_zoom_video_folder():
try:
os.mkdir('Zoomed-in Video')
except FileExistsError as fe:
print(fe)
# CREATE FOLDER WITH NAME EXPORTED FRAMES
def create_exported_frames_folder():
os.mkdir('Exported Frames')
os.mkdir('Exported Frames/zoomed images')
# FIND LAST CREATED RUNS FOLDER AND GET THE VIDEO FROM THERE
def find_last_detection_video():
chdir_to_content_root()
relative_path = max(glob.glob(os.path.join('./player_detection/runs/track/exp*', '*mp4')), key=os.path.getmtime)
return relative_path
# FIND LAST CREATED RUNS FOLDER AND GET THE TXT FILE FROM THERE
def find_last_detection_text_file():
chdir_to_content_root()
try:
relative_path = max(glob.glob(os.path.join('./player_detection/runs/track/exp*', '*txt')), key=os.path.getmtime)
except IndexError as ie:
relative_path = glob.glob(os.path.join('./player_detection/runs/track/exp*', '*txt'))
print(relative_path)
print(ie)
return relative_path
# CHANGE DIRECTORY TO CONTENT ROOT
def chdir_to_content_root():
relative_cwd = sm.get_after_last_slash(sm.double_backslash_to_slash(os.getcwd()))
flag = True
while flag:
# the first one is on my local computer, the other when you clone the GitHub repository
if relative_cwd == 'SportsAnalysisSoftware' or relative_cwd == 'Sports-Analysis-Software':
flag = False
else:
os.chdir('..')
relative_cwd = sm.get_after_last_slash(sm.double_backslash_to_slash(os.getcwd()))