-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab1-optA
More file actions
75 lines (47 loc) · 2.21 KB
/
lab1-optA
File metadata and controls
75 lines (47 loc) · 2.21 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
# Gerardo I. Armenta
# Lab1-optA
# date last modified Sept. 17, 2018
import os
import random
def get_dirs_and_files(path):
#provided method by teacher, not used!
dir_list = [directory for directory in os.listdir(path) if os.path.isdir(path + '/' + directory)]
file_list = [directory for directory in os.listdir(path) if not os.path.isdir(path + '/' + directory)]
return dir_list, file_list
def classify_pic(path): # Provided by teacher, checks path to differentiate dog and cat pics
# To be implemented by Diego: Replace with ML model
if "dog" in path:
return 0.5 + random.random() / 2
print('test')
return random.random() / 2
def process_dir(path): # Method used uses time complexity of O(n)
dir_list, file_list = get_dirs_and_files(path) # provided code by teacher, not used!
cat_list = []
dog_list = []
all_pics = []
list = cat_dog(path, all_pics) # This method lists all paths to cat and dog pics into an array
for i in range(len(list)): # for loop used to order dog_list and cat_list
pic = classify_pic(list[i])
if pic >= 0.5:
dog_list.append(list[i])
else:
cat_list.append(list[i])
print(*dog_list, sep ='\n') # Lines 45 thru 47 tests that dog_list and cat_list are filled correctly // may be erased or marked out
print('\n')
print(*cat_list, sep ='\n')
return cat_list, dog_list
def cat_dog(path, list): # Method used uses time complexity of O(n)
for file in os.listdir(path):
# identifies all files and subdirs in Pictures root dir and creates a for loop the size of all files and sub dirs
dir_path = os.path.join(path, file) # This is the path for one file or sub dir
if dir_path.endswith('jpg'):
# base case, checks if path contains files that are in jpg format and adds them to the array
list.append(dir_path)
elif os.path.isdir(dir_path):
# recursive step, checks if path contais a sub dir and if it does it calls itself with that new path
cat_dog(dir_path, list)
return list
def main():
start_path = './' # current directory
process_dir(start_path)
main()