-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvaluateTESTFractureYolov10.py
More file actions
306 lines (230 loc) · 10.6 KB
/
EvaluateTESTFractureYolov10.py
File metadata and controls
306 lines (230 loc) · 10.6 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# -*- coding: utf-8 -*-
"""
Created on Jun 2024
@author: Alfonso Blanco
"""
#######################################################################
# PARAMETERS
######################################################################
dir=""
dirname= "testFractureOJumbo1\\images"
dirnameLabels="testFractureOJumbo1\\labels"
#dirnameYolo ="C:\\Fracture.v1i_Reduced_Yolov10\\runs\\train\\exp4\\weights\\best.pt"
#dirnameYolo ="C:\\Fracture.v1i_Reduced_Yolov10\\runs\\train\\exp4\\weights\\last.pt"
dirnameYolo="last114epoch0603.pt"
import cv2
import time
Ini=time.time()
""" This gives error after upgrading ultralytics
from ultralytics import YOLOv10
model = YOLOv10(dirnameYolo)
"""
# change for
from ultralytics import YOLO
model = YOLO(dirnameYolo)
class_list = model.model.names
print(class_list)
import numpy as np
import os
import re
import imutils
########################################################################
def loadimages(dirname):
#########################################################################
# adapted from:
# https://www.aprendemachinelearning.com/clasificacion-de-imagenes-en-python/
# by Alfonso Blanco García
########################################################################
imgpath = dirname + "\\"
images = []
TabFileName=[]
print("Reading imagenes from ",imgpath)
NumImage=-2
Cont=0
for root, dirnames, filenames in os.walk(imgpath):
NumImage=NumImage+1
for filename in filenames:
if re.search("\.(jpg|jpeg|png|bmp|tiff)$", filename):
filepath = os.path.join(root, filename)
image = cv2.imread(filepath)
#print(filepath)
#print(image.shape)
images.append(image)
TabFileName.append(filename)
Cont+=1
return images, TabFileName
########################################################################
def loadlabels(dirnameLabels):
#########################################################################
# adapted from:
# https://www.aprendemachinelearning.com/clasificacion-de-imagenes-en-python/
# by Alfonso Blanco García
########################################################################
imgpath = dirnameLabels + "\\"
Labels = []
TabFileLabelsName=[]
Tabxyxy=[]
ContLabels=0
ContNoLabels=0
print("Reading labels from ",imgpath)
for root, dirnames, filenames in os.walk(imgpath):
for filename in filenames:
filepath = os.path.join(root, filename)
f=open(filepath,"r")
Label=""
xyxy=""
for linea in f:
indexFracture=int(linea[0])
Label=class_list[indexFracture]
xyxy=linea[2:]
Labels.append(Label)
if Label=="":
ContLabels+=1
else:
ContNoLabels+=1
TabFileLabelsName.append(filename)
Tabxyxy.append(xyxy)
return Labels, TabFileLabelsName, Tabxyxy, ContLabels, ContNoLabels
def unconvert(width, height, x, y, w, h):
xmax = int((x*width) + (w * width)/2.0)
xmin = int((x*width) - (w * width)/2.0)
ymax = int((y*height) + (h * height)/2.0)
ymin = int((y*height) - (h * height)/2.0)
return xmin, ymin, xmax, ymax
# ttps://medium.chom/@chanon.krittapholchai/build-object-detection-gui-with-yolov8-and-pysimplegui-76d5f5464d6c
def DetectBoneFractureWithYolov10 (img):
TabcropBoneFracture=[]
y=[]
yMax=[]
x=[]
xMax=[]
Tabclass_name=[]
# https://blog.roboflow.com/yolov10-how-to-train/
results = model(source=img)
for i in range(len(results)):
# may be several plates in a frame
result=results[i]
xyxy= result.boxes.xyxy.numpy()
confidence= result.boxes.conf.numpy()
class_id= result.boxes.cls.numpy().astype(int)
#print(class_id)
out_image = img.copy()
for j in range(len(class_id)):
con=confidence[j]
label=class_list[class_id[j]] + " " + str(con)
box=xyxy[j]
cropBoneFracture=out_image[int(box[1]):int(box[3]),int(box[0]):int(box[2])]
TabcropBoneFracture.append(cropBoneFracture)
y.append(int(box[1]))
yMax.append(int(box[3]))
x.append(int(box[0]))
xMax.append(int(box[2]))
# Tabclass_name only contains confidence, there is only a class and the name is not interesting
Tabclass_name.append(label)
return TabcropBoneFracture, y,yMax,x,xMax, Tabclass_name
###########################################################
# MAIN
##########################################################
Labels, TabFileLabelsName, TabxyxyTrue, ContLabels, ContNoLabels= loadlabels(dirnameLabels)
#print("Number of images to test : " + str(len(Labels)))
#print("Number of files without labels : " + str(ContNoLabels))
#print("Number of files with labels : " + str(ContLabels))
imagesComplete, TabFileName=loadimages(dirname)
print("Number of images to test: " + str(len(imagesComplete)))
ContError=0
ContHit=0
ContNoDetected=0
for i in range (len(imagesComplete)):
if TabFileLabelsName[i][:len(TabFileLabelsName[i])-4] != TabFileName[i][:len(TabFileName[i])-4]:
print("ERROR SEQUENCING IMAGES AN LABELS " + TabFileLabelsName[i][:len(TabFileLabelsName[i])-4] +" --" + TabFileName[i][:len(TabFileName[i])-4])
break
# no se consideran las que no vienen labeladas
if Labels[i] == "": continue
gray=imagesComplete[i]
imgTrue=imagesComplete[i]
xyxyTrue=TabxyxyTrue[i].split(" ")
yTrue=float(xyxyTrue[1])* float(imgTrue.shape[0])
yMaxTrue=float(xyxyTrue[3])* float(imgTrue.shape[0])
xTrue=float(xyxyTrue[0])* float(imgTrue.shape[1])
xMaxTrue=float(xyxyTrue[2])* float(imgTrue.shape[1])
start_pointTrue=(int(xTrue),int(yTrue))
end_pointTrue=(int(xMaxTrue),int( yMaxTrue))
# Put text
text_locationTrue = (int(xMaxTrue),int(yMaxTrue))
text_colorTrue = (255,255,255)
XcenterYcenterWH=TabxyxyTrue[i].split(" ")
width=float(imgTrue.shape[0])
height=float(imgTrue.shape[1])
x=float(XcenterYcenterWH[0])
y=float(XcenterYcenterWH[1])
w=float(XcenterYcenterWH[2])
h=float(XcenterYcenterWH[3])
xTrue,yTrue,xMaxTrue,yMaxTrue=unconvert(width, height, x, y, w, h)
start_pointTrue=(int(xTrue),int(yTrue))
end_pointTrue=(int(xMaxTrue),int( yMaxTrue))
colorTrue=(0,0,255)
# Using cv2.rectangle() method
# Draw a rectangle with green line borders of thickness of 2 px
imgTrue = cv2.rectangle(imgTrue, start_pointTrue, end_pointTrue,(0,255,0), 2)
# Put text
text_locationTrue = (int(xMaxTrue),int(yMaxTrue))
text_colorTrue = (255,255,255)
#cv2.putText(imgTrue, Labels[i] ,text_locationTrue
# , cv2.FONT_HERSHEY_SIMPLEX , 1
# , text_colorTrue, 2 ,cv2.LINE_AA)
cv2.putText(imgTrue, "" ,text_locationTrue
, cv2.FONT_HERSHEY_SIMPLEX , 1
, text_colorTrue, 2 ,cv2.LINE_AA)
#cv2.imshow('True', imgTrue)
#cv2.waitKey(0)
#"""
TabImgSelect, y, yMax, x, xMax, Tabclass_name =DetectBoneFractureWithYolov10(gray)
#print(gray.shape)
if TabImgSelect==[]:
print(TabFileName[i] + " NON DETECTED")
ContNoDetected=ContNoDetected+1
continue
else:
#ContDetected=ContDetected+1
print(TabFileName[i] + " DETECTED ")
for z in range(len(TabImgSelect)):
#if TabImgSelect[z] == []: continue
gray1=TabImgSelect[z]
#cv2.waitKey(0)
start_point=(x[z],y[z])
end_point=(xMax[z], yMax[z])
color=(255,0,0)
# Using cv2.rectangle() method
# Draw a rectangle with blue line borders of thickness of 2 px
img = cv2.rectangle(gray, start_point, end_point,(255,0,0), 2)
# Put text
text_location = (x[z], y[z])
text_color = (255,255,255)
if Tabclass_name[z][:len(Labels[i])] !=Labels[i]:
#print(len(Tabclass_name[z]))
#print(len(Labels[i]))
print("ERROR " + TabFileName[i] + "Predicted "+ Tabclass_name[z] + " true is " + Labels[i])
ContError+=1
else:
#print("HIT " + TabFileName[i] + "Predicted "+ Tabclass_name[z] )
ContHit+=1
cv2.putText(img, str(Tabclass_name[z][len(Labels[i]):]) ,text_location
, cv2.FONT_HERSHEY_SIMPLEX , 1
, text_color, 2 ,cv2.LINE_AA)
cv2.putText(gray1, str(Tabclass_name[z][len(Labels[i]):]) ,text_location
, cv2.FONT_HERSHEY_SIMPLEX , 1
, text_color, 2 ,cv2.LINE_AA)
#cv2.imshow('Bone Fracture', gray1)
#cv2.waitKey(0)
break
#
#show_image=cv2.resize(img,(1000,700))
#cv2.imshow('Frame', show_image)
cv2.imshow('Frame', img)
cv2.waitKey(0)
print("")
print("NO detected=" + str(ContNoDetected))
#print("Errors=" + str(ContError))
#print("Hits=" + str(ContHit))
print("")
print( " Time in seconds "+ str(time.time()-Ini))