-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_sort.py
More file actions
115 lines (83 loc) · 3.53 KB
/
file_sort.py
File metadata and controls
115 lines (83 loc) · 3.53 KB
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
import os
import shutil
import time
import datetime
import string
'''
The purpose of this program is to take a list of files and
sort them into their respective folders. Start at the directory outside
the new_data folder and run this file sorting algorithm to organize it into
their respective buckets
'''
'''
name folder of capture as
'''
valid_types = ['RPI']
def camera_type(name):
spl = name.split("_")
spl = str(spl[1])
return spl.strip()
input_directory = input("What is the filepath of your new data folder?\n")
input_name = input("What is the name of your plant? Example: Tomato_RPI_1 is the first Tomato you are recording.\n")
while camera_type(input_name) not in valid_types:
print('Type the input name properly! Plantname_camera_1, for example. Valid types are currently [\'RPI\']\n')
input_name = input("What is the name of your plant? Example: Tomato_RPI_1 is the first Tomato you are recording.\n")
while not os.path.exists(input_directory):
print('Is that a valid directory??\n')
input_directory = input("What is the filepath of your new data folder?\n")
folder_names = ["python_files","top_down_images","side_view_images","txt_files"]
top_dir = './top_down_images/'
side_dir = './side_view_images/'
py_dir = './python_files/'
txt_dir = './txt_files/'
def folder(name):
if not os.path.exists('./'+name):
os.makedirs('./'+name)
print ('Created:', name)
#This sorts raspberri pi data into top and side directories
#Takes filename and new folder name as input
def sort_capture_rpi(filepath, name):
img_files = os.listdir(filepath)
if not os.path.exists('./top_down_images/'+name):
os.makedirs('./top_down_images/'+name)
print ('Created:', name)
if not os.path.exists('./side_view_images/'+name):
os.makedirs('./side_view_images/'+name)
print ('Created:', name)
if not os.path.exists('./redundant_images/'+name):
os.makedirs('./redundant_images/'+name)
print('Created: ', name)
for j in range(int(len(img_files))):
str1 = img_files[j]
if('ContinuousTop' in str1):
if os.path.exists('./top_down_images/'+name):
if not os.path.exists('./top_down_images/'+name+ '/'+ str(img_files[j])):
shutil.move(filepath + '/' + str(img_files[j]),'./top_down_images/'+name)
if('ContinuousSide' in str1):
if os.path.exists('./side_view_images/'+name):
if not os.path.exists('./side_view_images/'+name+ '/'+ str(img_files[j])):
shutil.move(filepath + '/' + str(img_files[j]),'./side_view_images/'+name)
def sort(current, name_of_plant):
files = os.listdir(current)
src = input_directory
for file in files:
print("Sorting..."+str(file) + '\n')
for name in folder_names:
folder(name)
for file in os.listdir(current):
print(str(file))
if file.endswith('.txt'):
dest = './txt_files'
shutil.move(src,dest)
if file.endswith('.py'):
dest = './python_files'
shutil.move(src,dest)
if os.path.isdir(src + '/'+str(file)):
cam_type = str(camera_type(name_of_plant))
if cam_type == 'RPI':
sort_capture_rpi(src + '/'+str(file), name_of_plant)
def clean(current):
for file in os.scandir(current):
os.rmdir(file.path)
sort(input_directory, input_name)
print("Sorting the files...")