-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_data.py
More file actions
72 lines (58 loc) · 2.27 KB
/
gen_data.py
File metadata and controls
72 lines (58 loc) · 2.27 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
# Pythono3 code to rename multiple
# files in a directory or folder
# importing os module
import os
import cv2
import numpy as np
from skimage import io
import h5py
# Function to generate mnist data from images
def main():
src_dir = "F:\\PycharmProjects\\ai_Lessons\\img_caps"
dst_dir = "F:\\PycharmProjects\\ai_Lessons\\OpenCV_3_KNN_Character_Recognition_Python-master\\data"
data_file = dst_dir + "\\" + "alphan_caps.hdf5"
np_intValidChars = np.asarray(intValidChars)
with h5py.File(data_file, "w") as f:
dset = f.create_dataset("class", data=np_intValidChars)
j = 1
all_images = []
all_labels = []
for dirname in os.listdir(src_dir):
i = 1
# ord(dirname) :
for image_path in os.listdir(src_dir + "\\" + dirname):
img = cv2.imread(src_dir + "\\" + dirname + "\\" + image_path, 0)
all_images.append(img)
all_labels.append(ord(dirname))
print("Directory " + dirname + " Adding file : " + image_path + " out of " + str(len(os.listdir(src_dir + "\\" + dirname))) + " To Array Creating HD5 File ")
# rename() function will
# rename all the files
i += 1
j += 1
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
x_data = np.array(all_images)
y_data = np.array(all_labels)
# tuple_xy = [x_data, y_data]
#tuple_xy = np.array(tuple_xy)
with h5py.File(data_file, "a") as f:
dset_lbl = f.create_dataset("img_labels", data=y_data)
dset_img = f.create_dataset("img_dataset", data=x_data)
# dset_img_lbl = f.create_dataset("img_lbl_ds", data=tuple_xy)
with h5py.File(data_file, 'r') as f:
class_arr = f['class'][:]
labels_arr = f['img_labels'][:]
image_arr = f['img_dataset'][:]
print(class_arr.shape)
print(labels_arr.shape)
print(image_arr.shape)
# Driver Code
if __name__ == '__main__':
# Calling main() function
intValidChars = [ord('0'), ord('1'), ord('2'), ord('3'), ord('4'), ord('5'), ord('6'), ord('7'), ord('8'), ord('9'),
ord('A'), ord('B'), ord('C'), ord('D'), ord('E'), ord('F'), ord('G'), ord('H'), ord('I'), ord('J'),
ord('K'), ord('L'), ord('M'), ord('N'), ord('O'), ord('P'), ord('Q'), ord('R'), ord('S'), ord('T'),
ord('U'), ord('V'), ord('W'), ord('X'), ord('Y'), ord('Z')]
WIDTH = 28
HEIGHT = 28
main()