-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
123 lines (94 loc) · 3.46 KB
/
code.py
File metadata and controls
123 lines (94 loc) · 3.46 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
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import GridSearchCV, train_test_split
import random
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.decomposition import PCA
from sklearn.metrics import plot_confusion_matrix
from sklearn.metrics import accuracy_score
import mahotas
## LOAD AND PREPARE DATASET
dataset_dir = 'dataset'
categories = ['true','false']
dataset = []
for category in categories:
path = os.path.join(dataset_dir,category)
label = categories.index(category)
for img in os.listdir(path):
try:
imgpath = os.path.join(path,img)
# read image
rawimage = cv2.imread(imgpath)
# create pixel features
rawimage_pixels = np.array(rawimage).flatten()
# create haralick features
gray = cv2.cvtColor(rawimage, cv2.COLOR_BGR2GRAY)
rawimage_haralic = mahotas.features.haralick(gray).mean(axis=0)
dataset.append([rawimage_pixels,rawimage_haralic,label])
except:
pass
# GENERATE FEATURES USING PCA AND HARALICK FEATURES
# Shuffle data randomly to generate accurate test and train dataset
random.shuffle(dataset)
pixel_features = []
haralic_features = []
labels = []
for pixel,haralic,label in dataset:
pixel_features.append(pixel)
haralic_features.append(haralic)
labels.append(label)
pca = PCA(n_components=13).fit(pixel_features,labels)
pixel_features = pca.transform(pixel_features)
result_features = np.concatenate((pixel_features, haralic_features), axis=1)
xtrain, xtest, ytrain, ytest = train_test_split(result_features,labels,test_size=0.1)
print('Total Feature Length:',len(result_features[0]))
print("Total Train Size:",len(xtrain))
print("Total Test Size:",len(xtest))
# TRAIN AND CALCULATE ACCURACY
# SVM results in a lower accuracy than random forest
#model = SVC()
# User random forest classifier with 100 estimators
model=RandomForestClassifier(n_estimators=100)
model.fit(xtrain, ytrain)
ypred = model.predict(xtest)
plot_confusion_matrix(model, xtest, ytest,values_format='d')
plt.show()
print("Accuracy:",accuracy_score(ytest,ypred))
# PREDICTING REAL IMAGES
image_path = 'test_images/10.jpg'
test_image = cv2.imread(image_path)
#Resize image
imageScale = test_image.shape[1]/300
dim = (int(test_image.shape[1]/imageScale), int(test_image.shape[0]/imageScale))
test_image = cv2.resize(test_image, dim, interpolation = cv2.INTER_CUBIC)
#Split image into 50x50 pieces and draw circle on positive ones
height, width = test_image.shape[:2]
x_values = range(0,int(width/50)-1)
y_values = range(0,int(height/50))
y=0
x=0
w=50
h=50
for y in y_values:
for x in x_values:
y2 = y*50
x2 = x*50
crop_img = test_image[y2:y2+h, x2:x2+w]
features = np.array(crop_img).flatten()
features = pca.transform([features])[0]
gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
rawimage_haralic = mahotas.features.haralick(gray).mean(axis=0)
image = np.concatenate((features, rawimage_haralic), axis=0)
prediction = model.predict([image])
#print('Prediction is: ', categories[prediction[0]])
#plt.imshow(crop_img)
#plt.show()
if prediction[0] == 0:
#plt.imshow(crop_img)
#plt.show()
test_image = cv2.circle(test_image, (x2+15,y2+15), 30, (0,255,0), 3)
plt.imshow(cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB))
plt.show()