-
Notifications
You must be signed in to change notification settings - Fork 0
/
PR_Final.py
148 lines (127 loc) · 3.86 KB
/
PR_Final.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
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 30 03:17:04 2018
@author: NaSiF
"""
from sklearn.svm import SVC
import cv2
import numpy as np
import glob
# from scipy import misc
def lbp_calculated_pixel(img, x, y):
'''
1 | 2 | 4
----------------
128 | 0 | 8
----------------
64 | 32 | 16
'''
tem = np.zeros((3, 3))
p=0
for i in range (x,x+3):
q=0
for j in range(y,y+3):
tem[p][q]=img[i][j]
q=q+1
p=p+1
val=0
if tem[0][0]>=tem[1][1]:
val=val+1
else:
val=val+0
if tem[0][1]>=tem[1][1]:
val=val+2
else:
val=val+0
if tem[0][2]>=tem[1][1]:
val=val+4
else:
val=val+0
if tem[1][2]>=tem[1][1]:
val=val+8
else:
val=val+0
if tem[2][2]>=tem[1][1]:
val=val+16
else:
val=val+0
if tem[2][1]>=tem[1][1]:
val=val+32
else:
val=val+0
if tem[2][0]>=tem[1][1]:
val=val+64
else:
val=val+0
if tem[1][0]>=tem[1][1]:
val=val+128
else:
val=val+0
return val
data=[]
labels=[]
def main():
n=-1
while 1 :
n=n+1
filename = input("Enter the file name in which images are present = ")
for img in glob.glob(filename+'/*.*'):
try :
img_rgb= cv2.imread(img)
# img_rgb=misc.imresize(img_rgb,(126,126))
img_rgb=cv2.resize(img_rgb, dsize=(126,126), interpolation=cv2.INTER_CUBIC)
height, width, channel = img_rgb.shape
img_gray=cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
lst= np.zeros((int((height*width)/9)), np.uint8)
i=0
p=0
while i<height:
j=0
while j<width:
lst[p]=lbp_calculated_pixel(img_gray, i, j)
j=j+3
p=p+1
i=i+3
data.append(lst)
labels.append(n)
except Exception as e:
print (e)
user_input = input("do you want to read another folder = ")
if user_input == 'no':
break
clf = SVC(gamma = 0.0000001, C=100)
clf.fit(data,labels)
print('\ndata:' , data)
print('\nlabels:' , labels)
filename = input("Enter the file name in which images are present = ")
for im in glob.glob(filename+'/*.*'):
try :
img=cv2.imread(im)
# img_rgb=misc.imresize(img,(126,126))
img_rgb=cv2.resize(img, dsize=(126,126), interpolation=cv2.INTER_CUBIC)
height, width, channel = img_rgb.shape
img_gray=cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
lst= ~np.zeros((int((height*width)/9)), np.uint8)
i=0
p=0
while i<height:
j=0
while j<width:
lst[p]=lbp_calculated_pixel(img_gray, i, j)
j=j+3
p=p+1
i=i+3
print('\nll:',lst)
print('Prediction:',clf.predict(lst.reshape(1,-1)))
prediction= clf.predict(lst.reshape(1,-1))
font = cv2.FONT_HERSHEY_SIMPLEX
# img=misc.imresize(img,(256,256))
img=cv2.resize(img, dsize=(126,126), interpolation=cv2.INTER_CUBIC)
cv2.putText(img, str(prediction[0]),(10,70), font, 3, (0,255,0), 3, cv2.LINE_AA)
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
except Exception as e:
print (e)
if __name__ == '__main__':
main()