-
Notifications
You must be signed in to change notification settings - Fork 1
/
images_to_arrays.py
210 lines (164 loc) · 5.29 KB
/
images_to_arrays.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# -*- coding: utf-8 -*-
"""Fruits_vegetables_model_converting_images_to_arrays.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1_FdqLHcm5EF8BbxsJDvI62KblgDvsPhL
"""
#import tensorflow as tf
import cv2
from tqdm import tqdm
#import matplotlib.pyplot as plt
import os
"""importing necessary libraries"""
"""connecting to kaggle courtesy-[this website](https://https://www.analyticsvidhya.com/blog/2021/06/how-to-load-kaggle-datasets-directly-into-google-colab/
)
"""
#!kaggle datasets download raghavrpotdar/fresh-and-stale-images-of-fruits-and-vegetables
"""dataset from [kaggle](https://www.kaggle.com/datasets/raghavrpotdar/fresh-and-stale-images-of-fruits-and-vegetables"""
#Tried different resolutions,settled at 45*45
""" **Labels** Fresh apple-01 Fresh bannana-02 Fresh bitter guard-03 fresh capsicum-04 fresh orange-05 fresh tomato-06
Stale apple-7 Stale banana-8 Stale bitter guard-9 stale capsicum-10 Stale orange-11 Stale tomato-12
old
new subtract one from each ie
Fresh apple-0 fresh banana -1 and so on
"""
path="/home/shreyas/fresh_apple"
training_data=[]
resolution=(45,45)
from rembg import remove
for img in tqdm(os.listdir(path)):
try:
img_array=cv2.imread(os.path.join(path,img))
img_array=remove(img_array)
new_array=cv2.resize(img_array,resolution)
training_data.append([new_array,1])
except:
pass
path="/home/shreyas/fresh_banana"
for img in tqdm(os.listdir(path)):
try:
img_array=cv2.imread(os.path.join(path,img))
img_array=remove(img_array)
new_array=cv2.resize(img_array,resolution)
training_data.append([new_array,2])
except:
pass
path="/home/shreyas/fresh_bitter_gourd"
for img in tqdm(os.listdir(path)):
try:
img_array=cv2.imread(os.path.join(path,img))
img_array=remove(img_array)
new_array=cv2.resize(img_array,resolution)
training_data.append([new_array,3])
except:
pass
path="/home/shreyas/fresh_capsicum"
for img in tqdm(os.listdir(path)):
try:
img_array=cv2.imread(os.path.join(path,img))
img_array=remove(img_array)
new_array=cv2.resize(img_array,resolution)
training_data.append([new_array,4])
except:
pass
path="/home/shreyas/fresh_orange"
for img in tqdm(os.listdir(path)):
try:
img_array=cv2.imread(os.path.join(path,img))
img_array=remove(img_array)
new_array=cv2.resize(img_array,resolution)
training_data.append([new_array,5])
except:
pass
path="/home/shreyas/fresh_tomato"
for img in tqdm(os.listdir(path)):
try:
img_array=cv2.imread(os.path.join(path,img))
img_array=remove(img_array)
new_array=cv2.resize(img_array,resolution)
training_data.append([new_array,6])
except:
pass
path="/home/shreyas/stale_apple"
for img in tqdm(os.listdir(path)):
try:
img_array=cv2.imread(os.path.join(path,img))
img_array=remove(img_array)
new_array=cv2.resize(img_array,resolution)
training_data.append([new_array,7])
except:
pass
path="/home/shreyas/stale_banana"
for img in tqdm(os.listdir(path)):
try:
img_array=cv2.imread(os.path.join(path,img))
img_array=remove(img_array)
new_array=cv2.resize(img_array,resolution)
training_data.append([new_array,8])
except:
pass
path="/home/shreyas/stale_bitter_gourd"
for img in tqdm(os.listdir(path)):
try:
img_array=cv2.imread(os.path.join(path,img))
img_array=remove(img_array)
new_array=cv2.resize(img_array,resolution)
training_data.append([new_array,9])
except:
pass
path="/home/shreyas/stale_capsicum"
for img in tqdm(os.listdir(path)):
try:
img_array=cv2.imread(os.path.join(path,img))
img_array=remove(img_array)
new_array=cv2.resize(img_array,resolution)
training_data.append([new_array,10])
except:
pass
path="/home/shreyas/stale_orange"
for img in tqdm(os.listdir(path)):
try:
img_array=cv2.imread(os.path.join(path,img))
img_array=remove(img_array)
new_array=cv2.resize(img_array,resolution)
training_data.append([new_array,11])
except:
pass
path="/home/shreyas/stale_tomato"
for img in tqdm(os.listdir(path)):
try:
img_array=cv2.imread(os.path.join(path,img))
img_array=remove(img_array)
new_array=cv2.resize(img_array,resolution)
training_data.append([new_array,12])
except:
pass
"""Making a list of all images(converted to arrays of numbers) and their labels as shown above
Actually labels can be only from 0 to 11 ,not 1 to 12 changed y-=1 in second notebook
"""
print(len(training_data))
"""The above dataset is not balanced ,since some category has just 300 images while some has more than 2000 ,thus we might have to pass weights while training the model to balance the dataset.
Also we have to shuffle the model otherwise it will first pass through all oranges and so on at a time
"""
import random
random.shuffle(training_data)
for i in range(1,10):
print(training_data[i][1])
X=[]
y=[]
for i in training_data:
X.append(i[0])
y.append(i[1])
import numpy as np
X=np.array(X).reshape(-1,45,45,3)
print(X)
"""above ,we Converted image data to array form which will be used to train model """
import pickle
pickle_out=open("X.pickle","wb")
pickle.dump(X,pickle_out)
pickle_out.close()
pickle_out=open("y.pickle","wb")
pickle.dump(y,pickle_out)
pickle_out.close()
"""Saving data as .pickle file
"""