-
Notifications
You must be signed in to change notification settings - Fork 0
/
kaggle.py
172 lines (137 loc) · 5.4 KB
/
kaggle.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
import os
import cv2
import xml.etree.ElementTree as ET
import numpy as np
img_rows, img_cols = 28, 28
num_classes = 10
def prep_data(raw, train_size, val_size):
y = raw[:, 0]
out_y = keras.utils.to_categorical(y, num_classes)
x = raw[:,1:]
num_images = raw.shape[0]
out_x = x.reshape(num_images, img_rows, img_cols, 1)
out_x = out_x / 255
return out_x, out_y
def prepare():
for filename in os.listdir('allimages'):
if filename.endswith(".JPEG"):
if not os.path.exists('allimages/'+filename.split('.')[0]+str('.xml')):
os.remove('allimages/'+filename)
print('deleted'+filename)
def converttonumpy():
ctr=0
imarray=np.zeros((796,400,400))
outputarray=np.zeros((796,4))
for filename in os.listdir('imagescopy1'):
if filename.endswith(".JPEG") or filename.endswith(".PNG"):
im = cv2.imread('imagescopy1/'+filename)
im=im[:,:,0]
imarray[ctr]=im
if(os.path.exists('imagescopy1/'+filename.split('.')[0]+str('.xml'))):
# print(filename)
tree = ET.parse('imagescopy1/'+str(filename.split('.')[0]+'.xml'))
root = tree.getroot()
if root[2].tag=='source':
img_x_size=int(root[3][0].text)
img_y_size=int(root[3][1].text)
outputarray[ctr][0]=str(int(int(root[5][4][0].text)/img_x_size*400))
outputarray[ctr][1]=str(int(int(root[5][4][1].text)/img_y_size*400))
outputarray[ctr][2]=str(int(int(root[5][4][2].text)/img_x_size*400))
outputarray[ctr][3]=str(int(int(root[5][4][3].text)/img_y_size*400))
else:
img_x_size=int(root[4][0].text)
img_y_size=int(root[4][1].text)
outputarray[ctr][0]=str(int(int(root[6][4][0].text)/img_x_size*400))
outputarray[ctr][1]=str(int(int(root[6][4][1].text)/img_y_size*400))
outputarray[ctr][2]=str(int(int(root[6][4][2].text)/img_x_size*400))
outputarray[ctr][3]=str(int(int(root[6][4][3].text)/img_y_size*400))
# outputarray[ctr][0]=root[6][4][0].text#=str(int(int(root[5][4][0].text)/img_x_size*100))
# outputarray[ctr][1]=root[6][4][1].text#=str(int(int(root[5][4][1].text)/img_y_size*100))
# outputarray[ctr][2]=root[6][4][2].text#=str(int(int(root[5][4][2].text)/img_x_size*100))
# outputarray[ctr][3]=root[6][4][3].text#=str(int(int(root[5][4][3].text)/img_y_size*100))
else:
img_x_size=im.height
img_y_size=im.width
outputarray[ctr][0]=0
outputarray[ctr][1]=0
outputarray[ctr][2]=400
outputarray[ctr][3]=400
ctr+=1
print(filename)
return outputarray,imarray
#outarr,inarr=converttonumpy()
# print(outarr)
#np.save('400x400_innpy.npy',inarr)
#np.save('400x400_outnpy.npy', outarr)
# im.append()
# print(im.shape)
# print(im[3][3])
# while(1):
# continue
def makedata():
for filename in os.listdir('allimages'):
if filename.endswith(".JPEG"):
im = cv2.imread('allimages/'+filename)
gray_image = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.imwrite('allimages/'+filename.split('.')[0]+str('.jpg'),gray_image)
def makedata1():
for filename in os.listdir('allimages'):
if filename.endswith(".JPEG"):
os.remove('allimages/'+filename)
def resize():
# image=cv2.imread('shipcheck.jpg')
# print(str(image.shape[:2])+' '+str(filename))
for filename in os.listdir('resizedall'):
if filename.endswith(".JPEG"):
image=cv2.imread('resizedall/'+filename)
# print(str(image.shape[:2])+' '+str(filename))
def findImagesWithnoXml():
for filename in os.listdir('imagescopy1'):
if filename.endswith(".JPEG"):
if not os.path.exists('imagescopy1/'+filename.split('.')[0]+str('.xml')):
os.remove('imagescopy1/'+filename)
print('deleted '+filename)
findImagesWithnoXml()
# resize()
#pic=pic[:][:][0]
#print(pic.shape)
# filename='n04530566_182.jpg'#'3D-Matplotlib.png'#
# im = cv2.imread('allimages/'+filename)
# cv2.imshow('frame',im)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# from tensorflow.python import keras
# from tensorflow.python.keras.models import Sequential
# from tensorflow.python.keras.layers import Dense, Flatten, Conv2D
# # # Your Code Here
# fashion_model = Sequential()
# fashion_model.add(Conv2D(12,kernel_size=(3,3),activation='relu',input_shape=((img_rows, img_cols, 1))))
# fashion_model.add(Conv2D(2,kernel_size=(3,3),activation='relu'))
# fashion_model.add(Flatten())
# fashion_model.add(Dense(100,activation='relu'))
# fashion_model.add(Dense(num_classes,activation='softmax'))
# fashion_model.compile(loss=bb_intersection_over_union,
# optimizer='adam',
# metrics=['accuracy'])
# fashion_model.fit(x, y,
# batch_size=100,
# epochs=4,
# validation_split = 0.2)
# def bb_intersection_over_union(y_true, y_pred):
# # determine the (x, y)-coordinates of the intersection rectangle
# xA = max(y_true[0], y_pred[0])
# yA = max(y_true[1], y_pred[1])
# xB = min(y_true[2], y_pred[2])
# yB = min(y_true[3], y_pred[3])
# # compute the area of intersection rectangle
# interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
# # compute the area of both the prediction and ground-truth
# # rectangles
# y_trueArea = (y_true[2] - y_true[0] + 1) * (y_true[3] - y_true[1] + 1)
# y_predArea = (y_pred[2] - y_pred[0] + 1) * (y_pred[3] - y_pred[1] + 1)
# # compute the intersection over union by taking the intersection
# # area and dividing it by the sum of prediction + ground-truth
# # areas - the interesection area
# iou = interArea / float(y_trueArea + y_predArea - interArea)
# # return the intersection over union value
# return iou