-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognize.py
More file actions
131 lines (104 loc) · 3.51 KB
/
recognize.py
File metadata and controls
131 lines (104 loc) · 3.51 KB
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
from localbinarypatterns import LocalBinaryPatterns
from sklearn.svm import LinearSVC
from PIL import Image
from sklearn.externals import joblib
import os
from Tools import Tools
from imutils import paths
import argparse
import cv2
tools = Tools()
desc = LocalBinaryPatterns(20, 1)
data = []
labels = []
learningSetPath = "Resources/MajorProjectResources/LearningSet"
testingSetPath = "Resources/MajorProjectResources/TestingSet"
ClassList=(tools.getClassNames("Resources/MajorProjectResources/LearningSet"))
# print(ClassList)
for cl in ClassList:
clLearningFolder = learningSetPath+'/'+cl
#tools.getAllImagesResized(clLearningFolder)
clPathListLearning = tools.getImagePaths(clLearningFolder)
for i in range(90):
image = cv2.imread(clPathListLearning[i])
gry = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
hist = desc.describe(gry)
labels.append(cl)
data.append(hist)
print(data)
model = LinearSVC(C=100.0, random_state=42)
model.fit(data, labels)
joblib.dump(model, 'model.pkl')
accuracyCount=0
totalCount = 0
for cl in ClassList:
clTestingFolder = testingSetPath+'/'+cl
tools.getAllImagesResized(clTestingFolder)
clPathListTesting = tools.getImagePaths(clTestingFolder)
for i in range(0,len(clPathListTesting)):
image = cv2.imread(clPathListTesting[i])
gry = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
hist = desc.describe(gry)
hist = hist.reshape(1,-1)
prediction = model.predict(hist)[0]
print(prediction)
if prediction==cl:
accuracyCount+=1
print("hit")
else: print("miss")
totalCount+=1
print("acc Count " + str(accuracyCount))
print("total Count " + str(totalCount))
print(float(accuracyCount/totalCount))
# catsPathListLearning = (tools.getImagePaths(learningSet1))
# dogsPathListLearning = (tools.getImagePaths(learningSet2))
# allPathListTesting = (tools.getImagePaths(testingSet))
# # catsPathListTraining = (tools.getImagePaths("Resources/SubResources/TrainingSet/CatPhotos"))
# # carsPathListTraining = (tools.getImagePaths("Resources/SubResources/TrainingSet/CarPhotos"))
#
# #resizing photos
# tools.getAllImagesResized(learningSet1)
# tools.getAllImagesResized(learningSet2)
# tools.getAllImagesResized(testingSet)
#
# ############### LEARNING ##############
#
# for i in range(0,len(catsPathListLearning)):
# image = cv2.imread(catsPathListLearning[i])
# gry = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# hist = desc.describe(gry)
#
# labels.append("cat")
# data.append(hist)
#
#
# for i in range(0, len(dogsPathListLearning)):
# image = cv2.imread(dogsPathListLearning[i])
# gry = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# hist = desc.describe(gry)
# labels.append("dog")
# data.append(hist)
#
# # outfile.close()
#
# model = LinearSVC(C=100.0, random_state=42)
# model.fit(data, labels)
#
#
# ############### TESTING ##############
#
#
# for i in range(0,len(allPathListTesting)):
# image = cv2.imread(allPathListTesting[i])
# gry = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# hist = desc.describe(gry)
# hist = hist.reshape(1,-1)
# # feature = "-1 "
# # for j in range(0,hist.size):
# # feature += str(j+1) + ":" + str(hist[j]) + " "
# # outfile.write(feature +"\n")
# prediction = model.predict(hist)[0]
# # cv2.putText(image, prediction, (10, 30), cv2.FONT_HERSHEY_SIMPLEX,1.0, (0, 0, 255), 3)
# # cv2.imshow("Image", image)
# # cv2.waitKey(0)
# print(allPathListTesting[i], ' ' , prediction)