-
Notifications
You must be signed in to change notification settings - Fork 0
/
google crawl img.py
163 lines (140 loc) · 4.83 KB
/
google crawl img.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
'''# install module
import requests
import random
import shutil
import bs4
#Get URL to save
def image(data,num):
#Get URL for Google Image Search
res = requests.get("https://www.google.com/search?hl=jp&q=" + data + "&btnG=Google+Search&tbs=0&safe=off&tbm=isch")
html = res.text #Text conversion
soup = bs4.BeautifulSoup(html,'lxml') #Plastic surgery
links = soup.find_all("img") #Get img element
link = links[num].get("src") #Get numth srcURL
return link
#Download from the corresponding URL
def download(url,file_name):
req = requests.get(url, stream=True)
if req.status_code == 200:
with open(file_name + ".jpg ",'wb') as f: #Export png to file with bin
req.raw.decode_content = True
shutil.copyfileobj(req.raw, f) #Copy png image data to file
#Pick up the name of the child to search for
name = input("What child are you looking for?:")
for i in range(1,5): #20 sheets for convenience
link = image(name,i)
download(link,name + "-"+str(i))
print(link) #Repeat 20 times'''
from pyexpat import XML_PARAM_ENTITY_PARSING_ALWAYS
import numpy as np
import random
import os
import cv2 as cv
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.models import Sequential, load_model
from keras.layers import Conv2D, Dense, Flatten, Rescaling, AveragePooling2D, Dropout
#source:https://towardsdatascience.com/classification-of-traffic-signs-with-lenet-5-cnn-cb861289bd62
images = []
labels = []
classes = 43
current_path = 'C:\\Users\\ADMIN\\Downloads\\GTSRB_Final_Training_Images\\GTSRB\\Final_Training\\Images\\00002'
for i in range(classes):
# path = os.path.join(current_path, str(str(i).zfill(5)))
img_folder = os.listdir(current_path)
for j in img_folder:
try:
image = cv.imread(str(current_path+'\\'+j))
image = cv.resize(image, (32, 32))
image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
image = np.array(image)
images.append(image)
label = np.zeros(classes)
label[i] = 1.0
labels.append(label)
except:
pass
images = np.array(images)
images = images/255
labels = np.array(labels)
print('Images shape:', images.shape)
print('Labels shape:', labels.shape)
images = np.array(images)
images = images/255
labels = np.array(labels)
print('Images shape:', images.shape)
print('Labels shape:', labels.shape)
X = images.astype(np.float32)
y = labels.astype(np.float32)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)
print('X_train shape:', X_train.shape)
print('y_train shape:', y_train.shape)
print('X_test shape:', X_test.shape)
print('y_test shape:', y_test.shape)
plt.figure(figsize=(12, 12))
start_index = 0
for i in range(25):
plt.subplot(5, 5, i+1)
plt.grid(False)
plt.xticks([])
plt.yticks([])
label = np.argmax(y_train[start_index+i])
plt.xlabel('i={}, label={}'.format(start_index+i, label))
plt.imshow(X_train[start_index+i], cmap='gray')
plt.show()
# Building the model
model = Sequential([
Rescaling(1, input_shape=(32, 32, 1)),
Conv2D(filters=6, kernel_size=(5, 5), activation='relu'),
AveragePooling2D(pool_size=(2, 2)),
Conv2D(filters=16, kernel_size=(5, 5), activation='relu'),
AveragePooling2D(pool_size=(2, 2)),
Conv2D(filters=120, kernel_size=(5, 5), activation='relu'),
Dropout(0.2),
Flatten(),
Dense(units=120, activation='relu'),
Dense(units=43, activation='softmax')
])
# Compilation of the model
model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy']
)
# Model architecture
model.summary()
history = model.fit(X_train, y_train, epochs=50,
validation_data=(X_test, y_test))
val_loss, val_acc = model.evaluate(X_test, y_test, verbose=2)
print('\nValifdation accuracy:', val_acc)
print('\nValidation loss:', val_loss)
plt.figure(0)
plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.8, 1])
plt.legend(loc='lower right')
plt.figure(1)
plt.plot(history.history['loss'], label='loss')
plt.plot(history.history['val_loss'], label = 'val_loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.ylim([0, 0.2])
plt.legend(loc='lower right')
preds = model.predict(X_test)
plt.figure(figsize=(12, 12))
start_index = random.randint(0, 7800)
for i in range(25):
plt.subplot(5, 5, i+1)
plt.grid(False)
plt.xticks([])
plt.yticks([])
pred = np.argmax(preds[start_index+i])
gt = np.argmax(y_test[start_index+i])
col = 'g'
if pred != gt:
col = 'r'
plt.xlabel('i={}, pred={}, gt={}'.format(start_index+i, pred, gt), color=col)
plt.imshow(X_test[start_index+i], cmap='gray')
plt.show()