Repository navigation
Expand file tree
/
Copy pathimagereader.py
More file actions
78 lines (52 loc) · 2.09 KB
/
Copy pathimagereader.py
File metadata and controls
78 lines (52 loc) · 2.09 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
import numpy
import os
import pandas as pd
import glob
import cv2
labels = [] #list of labels 0 - 9 for the images (10 categories, 0 -9)
images = [] #image pixels list
def read_image(data_dir, folder, label) :
'''
Takes in data_dir, individual folder, and a count (for the label) and flattens each image data and stores it into image list.
'''
# retrieving folder path by joining the data directory and folder
folder_path = os.path.join(data_dir, folder)
# Retreiving a list of images in the folder
image_list = os.listdir(folder_path)
# looping through the list of images in the folder
for img_jpg in image_list:
image_path = os.path.join(folder_path, img_jpg)
#read the images
read_img = cv2.imread(image_path, cv2.IMREAD_COLOR)
#resize the images
resized_image = cv2.resize(read_img, (64,64))
#resized_img_flatten = resized_image.reshape(-1)
#print(resized_img_flatten.shape)
#append the index of the label into the labels list
labels.append(label)
flatten_img = resized_image.flatten()
#print(flatten_img.shape);
#append the flattened image to the images list
images.append(flatten_img)
def main():
print("Reading data")
label_index = 0; #counter variable for the category
data = "/Users/isabellewang/Downloads/Google-Aftershoot-BTTAI-Project/EuroSATdataset"
folders = os.listdir(data)
folders.remove(".DS_Store")
#folder_paths = [os.path.join(data, folder) for folder in folders]
#looping into each folder/category to read the image
for folder in folders:
print(folder)
read_image(data, folder, label_index)
label_index+=1
# makes the dataframe for the images with pixels as column names
df = pd.DataFrame(data = images, columns = [i for i in range(0, images[0].shape[0])])
#append the label to dataframe
df['label'] = labels
print(df['label'].unique())
#convert the dataframe to a csv file
df.to_csv("Eurodataset.csv", index = "image_name");
print("Data stored in a csv complete!")
if __name__ == '__main__':
main()