-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlbp.py
99 lines (94 loc) · 2.29 KB
/
lbp.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
import cv2
from random import randint
import numpy as np
import os
from skimage.feature import local_binary_pattern
# To calculate a normalized histogram
from scipy.stats import itemfreq
from sklearn.preprocessing import normalize
# Utility package -- use pip install cvutils to install
from itertools import repeat
d = [[] for i in repeat(None, 70)]
i=0
#print d
from sklearn import svm
a=[]
locations=[]
X1=[]
y1=[]
loc=[]
mat=np.matrix(X1)
for x in os.listdir("images"):
locations.append(x)
for x in os.listdir("images"):
for y in os.listdir("images/" + x):
im=cv2.imread("images/"+x+"/"+y)
img = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
img_scaled = cv2.resize(img, (1000, 1000), interpolation=cv2.INTER_AREA)
radius = 32
# Number of points to be considered as neighbourers
no_points = 32 * radius
# Uniform LBP is used
lbp = local_binary_pattern(img_scaled, no_points, radius, method='uniform')
# Calculate the histogram
ab = itemfreq(lbp.ravel())
# Normalize the histogram
hist = ab[:, 1] / sum(ab[:, 1])
#a.append(hist)
#print a
hist = np.asarray(hist)
print len(hist)
#print len(hist)
#X1=np.stack((X1,hist))
#hist=np.asarray(hist)
X1.append(hist)
y1.append(i)
i=i+1
loc.append(x)
#print hist.shape
#X1.append(a)
print len(X1)
print X1
# X1=np.reshape(X1,(70,130))
# X1 = np.array(X1)
# #print X1
# #X1=X1.transpose()
# print X1
#
# for i in range(70):
# for j in range(130):
# X1[i][j]=float(X1[i][j])
#
# #print X1
#
# #X1=X1.vstack(X1)
# #X1=X1.tolist()
# #print X1.shape
# #X1=np.hstack(X1)
# #print X1
#
#
# clf=svm.LinearSVC()
# clf.fit(X1,y1)
#
#
# im=cv2.imread("4.jpg")
# img = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
# img_scaled = cv2.resize(img, (1000, 1000), interpolation=cv2.INTER_AREA)
# radius = 4
# # Number of points to be considered as neighbourers
# no_points = 32 * radius
# # Uniform LBP is used
# lbp = local_binary_pattern(img_scaled, no_points, radius, method='uniform')
# # Calculate the histogram
# ab = itemfreq(lbp.ravel())
# # Normalize the histogram
# hist = ab[:, 1] / sum(ab[:, 1])
# hist = np.asarray(hist)
# ans=clf.predict(hist)
# print hist
# print ans
# print loc
# print loc[ans[0]]
#
# #