-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_set_preparation.py
66 lines (57 loc) · 1.88 KB
/
data_set_preparation.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
# Adding modules
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
from tqdm import tqdm
import random
import pickle
# Directory in which individual categories are located
DATADIR = "C:/Users/KILE/Desktop/"
categories = ["glass", "plastic", "cans"]
# Image width
IMG_W = int(640/2)
# Image height
IMG_H = int(480/2)
data_set = []
def creat_data_set():
"""Save images to data_set list"""
# Iterate through the categories list
for category in categories:
# A directory that contains images for a specific category
path = os.path.join(DATADIR,category)
# Index of the current category in the categories list
class_num = categories.index(category)
# Names of individual images
for image in tqdm(os.listdir(path)):
try:
# Image upload
img_array = cv2.imread(os.path.join(path,image),cv2.IMREAD_GRAYSCALE)
# Image resizing
new_array = cv2.resize(img_array, (IMG_W, IMG_H))
# Adding an image and category index to the data_set list
data_set.append([new_array, class_num])
# Ignoring mistakes
except Exception as e:
pass
# Executing the creat_data_set() function
creat_data_set()
# Random order of the data_set list
random.shuffle(data_set)
# Create an empty list for X and Y
X = []
Y = []
# Iterate through the data_set list, and extract data from it
for features, label in data_set:
# Adding features to X list.
X.append(features)
# Adding labels to Y list.
Y.append(label)
# Saving X list to X.pickle
pickle_out = open("C:/Users/KILE/Desktop/X.pickle", "wb")
pickle.dump(X, pickle_out, protocol=4)
pickle_out.close()
# Saving Y list to Y.pickle
pickle_out = open("C:/Users/KILE/Desktop/Y.pickle", "wb")
pickle.dump(Y, pickle_out, protocol=4)
pickle_out.close()