-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create_Resized_File_With_Predicted_Xcenter_SVR.py
153 lines (98 loc) · 4.75 KB
/
Create_Resized_File_With_Predicted_Xcenter_SVR.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
# Author Alfonso Blanco
import cv2
import numpy as np
import os
import re
import pickle #to save, load the model
dirname= "bone-fracture-2\\test\\images"
dirnameLabels="bone-fracture-2\\test\\labels"
imageSize=640
########################################################################
def loadimages(dirname):
#########################################################################
# adapted from:
# https://www.aprendemachinelearning.com/clasificacion-de-imagenes-en-python/
# by Alfonso Blanco García
########################################################################
imgpath = dirname + "\\"
imgpathlabels = dirnameLabels + "\\"
images = []
TabFileName=[]
TabImagesCV=[]
print("Reading imagenes from ",imgpath)
cont=0
contRejected=0
Yxmidpoint=[]
Yymidpoint=[]
Ywmidpoint=[]
Yhmidpoint=[]
for root, dirnames, filenames in os.walk(imgpath):
for filename in filenames:
if re.search("\.(jpg|jpeg|png|bmp|tiff)$", filename):
filepath = os.path.join(root, filename)
image = cv2.imread(filepath)
filenameLabel=filename[0:len(filename)-3]+ "txt"
filenameLabel=imgpathlabels + filenameLabel
#print( filenameLabel)
f=open(filenameLabel,"r")
xywh=""
SwEmpty=0
for linea in f:
cont= cont +1
#print(cont)
xywh=linea[2:]
#print(len(xywh))
#print(xywh)
SwEmpty=1
xywh=xywh.split(" ")
Yxmidpoint.append(xywh[0])
Yymidpoint.append(xywh[1])
Ywmidpoint.append(xywh[2])
Yhmidpoint.append(xywh[3])
break
if SwEmpty==0 :
contRejected=contRejected+1
print(" REJECTED HAS NO LABELS " + filename)
continue
result = cv2.resize(image, (imageSize,imageSize), interpolation = cv2.INTER_AREA)
TabImagesCV.append(result)
# TO REDUCE MEMORY PROBLEMS, CONVERT TO GRAY
cv2.imwrite("pptest.jpg", result)
result= cv2.imread("pptest.jpg", cv2.IMREAD_GRAYSCALE)
result=result.flatten()
#print(len(image))
images.append(result)
TabFileName.append(filename)
cont+=1
return TabImagesCV, images, TabFileName, Yxmidpoint, Yymidpoint
###########################################################
# MAIN
##########################################################
#TabFileLabelsName, Yxmidpoint, Yymidpoint, Ywmidpoint, Yhmidpoint= loadlabels(dirnameLabels)
imagesCV, X_test, TabFileName, Yxmidpoint, Yymidpoint=loadimages(dirname)
print("Number of images to test : " + str(len(X_test)))
#imagesCV, X_test, TabFileName=loadimages(dirname)
#print("Number of images to test : " + str(len(TabFileLabelsName)))
# https://medium.com/@niousha.rf/support-vector-regressor-theory-and-coding-exercise-in-python-ca6a7dfda927
from sklearn.preprocessing import StandardScaler
### When using StandardScaler(), fit() method expects a 2D array-like input
scaler = StandardScaler().fit(X_test)
X_test_scaled = scaler.transform(X_test)
model_svr_lin_Yxmidpoint= pickle.load( open("svr_lin_Yxmidpoint.pickle", 'rb'))
import numpy as np
from sklearn import metrics
#### Test dataset - metrics ####
y_test_pred_Yxmidpoint = model_svr_lin_Yxmidpoint.predict(X_test_scaled)
print("predicted values for Xcenter:")
print(y_test_pred_Yxmidpoint)
print("true values for Xcenter:")
print(Yxmidpoint)
print("===")
with open( "Predicted_True_Xcenter.txt" ,"w") as w:
for i in range (len(y_test_pred_Yxmidpoint)):
lineaw=[]
lineaw.append(y_test_pred_Yxmidpoint[i])
lineaw.append(Yxmidpoint[i])
lineaWrite =','.join(lineaw)
lineaWrite=lineaWrite + "\n"
w.write(lineaWrite)