-
Notifications
You must be signed in to change notification settings - Fork 0
/
useless.py
102 lines (86 loc) · 3.78 KB
/
useless.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
from PIL import Image
import tensorflow as tf
import numpy as np
import os
import random
# def scale_images():
# height = 255
# width = 255
# for folder in ['battery', 'biological', 'brown-glass', 'cardboard', 'clothes', 'green-glass', 'metal', 'paper', 'plastic', 'shoes', 'trash', 'white-glass']:
# num_files = len(os.listdir(f"train_data/{folder}")) + 1
# train_number = 1
# index = 1
# print(f"Folder {folder} has {num_files} images.")
# for file in os.listdir(f'train_data/{folder}'):
# path = f'train_data/{folder}/{file}'
# im = Image.open(path)
# im = im.save(f'train_data/{folder}/{folder}{index}.jpg')
# print(file)
# index += 1
# # chance = random.randint(1,20)
# # if chance >= 18:
# # im.close()
# # os.replace(path, f'test_data/{folder}/{folder}{str(test_number)}.jpg')
# # test_number += 1
# # else:
# # im = im.save(f'train_data/{folder}/{folder}{str(train_number)}.jpg')
# # train_number += 1
# num_files = len(os.listdir(f"train_data/{folder}")) + 1
# scale_images()
# # print(f"Folder {folder} has {num_files} images.")
# # scale_images()
# # path = f'train_data/{folder}/{folder}{str(i)}.jpg'
# # im = Image.open(path)
# # im = im.save
# # chance = random.randint(1,20)
# # if chance >= 18:
# # im.close()
# # os.replace(path, f'test_data/{folder}/{folder}{str(test_number)}.jpg')
# # test_number += 1
# # else:
# # im = im.save(f'train_data/{folder}/{folder}{str(train_number)}.jpg')
# # train_number += 1
# # num_files = len(os.listdir(f"train_data/{folder}")) + 1
# # print(f"Folder {folder} has {num_files} images.")
# def pre_process(path):
# im = Image.open(path)
# pxs = im.load()
# im_list = []
# for i in range(255):
# im_list.append([])
# for j in range(255):
# im_list[i].append(pxs[i, j])
# return (im_list[0])
# x_train = []
# y_train = []
# for folder in ['battery', 'biological', 'brown-glass', 'cardboard', 'clothes', 'green-glass', 'metal', 'paper', 'plastic', 'shoes', 'trash', 'white-glass']:
# num_files = len(os.listdir(f"train_data/{folder}")) + 1
# print(f"Folder {folder} has {num_files} images.")
# for i in range(1, num_files):
# path = f'train_data/{folder}/{folder}_{str(i)}.jpg'
# x_train.append(pre_process(path))
# y_train.append(folder)
# with open("x_train.txt", "w") as file:
# file.write(str(x_train))
# with open("y_train.txt", "w") as file:
# file.write(str(y_train))
with open("x_train.txt", "r") as file:
content = file.read()
x_train = eval(content)
with open("y_train.txt", "r") as file:
content = file.read()
y_train = eval(content)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(255, 3)))
#model.add(layers.Conv2D(255, (3, 3), activation='relu', input_shape=(255, 255, 3)))
# model.add(layers.MaxPooling2D((2, 2)))
# model.add(layers.Conv2D(64, (3, 3), activation='relu'))
# model.add(layers.MaxPooling2D((2, 2)))
# model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(tf.keras.layers.Dense(128, activation="relu"))
model.add(tf.keras.layers.Dense(256, activation="relu"))
model.add(tf.keras.layers.Dense(128, activation="relu"))
model.add(tf.keras.layers.Dense(12, activation="softmax"))
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(x_train, y_train, epochs=20)
model.save('handwritten.model')