-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfinals.py
157 lines (111 loc) · 3.16 KB
/
finals.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
import numpy as np
import matplotlib.pyplot as plt
import cv2
from skimage.feature import greycomatrix, greycoprops
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
# In[2]:
i=0
array=[]
array1=[]
def click1(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
ix,iy=x,y
font = cv2.FONT_HERSHEY_SIMPLEX
s=""
s=s+str(ix)+","+str(iy)
cv2.putText(img,s,(ix,iy), font, 0.5,(0,0,0),2)
cv2.imshow('image',img)
value=[(x,y)]
array1.append(value)
def click(event, x, y, flags, param):
global i
if event == cv2.EVENT_LBUTTONDOWN:
ix,iy=x,y
font = cv2.FONT_HERSHEY_SIMPLEX
s=""
s=s+str(ix)+","+str(iy)
cv2.putText(img,s,(ix,iy), font, 0.5,(0,0,0),2)
cv2.imshow('image',img)
value=[(x,y),i/15]
i=i+1
array.append(value)
img=cv2.imread("image_gray.jpg",0)
cv2.namedWindow('image')
cv2.imshow('image',img)
while(1):
#cv2.namedWindow('image')
cv2.setMouseCallback('image',click)
if cv2.waitKey(0) & 0xFF == 27:
break
print array
cv2.destroyAllWindows()
# glcm loops
# click window open for test
# data frames
# no csv
# trainig function
# traintestsplit()
# In[2]:
PATCH_SIZE = 20
x=[]
y=[]
xs=[]
image = cv2.imread("image_gray.jpg",0)
#plt.imshow(image ,cmap='gray')
#plt.show()
# In[ ]:
def feature(img):
xs=[]
glcm = greycomatrix(img, [5], [0], 256, symmetric=True, normed=True)
xs.append(greycoprops(glcm, 'contrast')[0,0])
xs.append(greycoprops(glcm, 'dissimilarity')[0, 0])
xs.append(greycoprops(glcm, 'homogeneity')[0, 0])
xs.append(greycoprops(glcm, 'ASM')[0, 0])
xs.append(greycoprops(glcm, 'energy')[0, 0])
xs.append(greycoprops(glcm, 'correlation')[0, 0])
return xs
for loc in array :
patches=(image[loc[0][1]:loc[0][1] + PATCH_SIZE,loc[0][0]:loc[0][0] + PATCH_SIZE])
x.append(feature(patches))
y.append(loc[1])
# In[ ]:
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=42)
# In[11]:
knn = KNeighborsClassifier(n_neighbors=3, weights='distance')
knn.fit(X_train,y_train)
scores = knn.score(X_test, y_test, sample_weight=None)
print "score ",scores
img=cv2.imread("image_gray.jpg",0)
cv2.namedWindow('image')
cv2.imshow('image',img)
while(1):
cv2.setMouseCallback('image',click1)
if cv2.waitKey(0) & 0xFF == 27:
break
print array1
cv2.destroyAllWindows()
for loc in array1:
patches=(image[loc[0][1]:loc[0][1] + PATCH_SIZE,loc[0][0]:loc[0][0] + PATCH_SIZE])
xs.append(feature(patches))
print xs
b=knn.predict(xs)
print b
#a=""
for i in b:
if i in [0]:
a="sky"
img1=cv2.imread("image_gray.jpg",0)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img1,a,(array1[0][0][0],array1[0][0][1]), font, 1,(0,0,0),2)
cv2.imshow('image',img1)
cv2.waitKey(0)
print a
if i in [1]:
a="grass"
img2=cv2.imread("image_gray.jpg",0)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img2,a,(array1[0][0][0],array1[0][0][1]), font, 1,(0,0,0),2)
cv2.imshow('image',img2)
cv2.waitKey(0)
print a