forked from ultralytics/ultralytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yolov8.py
250 lines (217 loc) · 9.55 KB
/
yolov8.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
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
from ultralytics import YOLO
import cv2
import time
import numpy as np
import sys
import similarity
import os
from keras.utils import image_utils as image
from keras.applications.imagenet_utils import preprocess_input
import numpy as np
import cv2
from image_similarity import *
from skimage.metrics import structural_similarity as compare_ssim
# 计算旋转变换矩阵
def handle_rotate_val(x, y, rotate):
cos_val = np.cos(np.deg2rad(rotate))
sin_val = np.sin(np.deg2rad(rotate))
return np.float32([
[cos_val, sin_val, x * (1 - cos_val) - y * sin_val],
[-sin_val, cos_val, x * sin_val + y * (1 - cos_val)]
])
def zhengqiang2(img):
img_t = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(img_t)
v2 = np.clip(cv2.add(2 * v, 20), 0, 255)
img2 = np.uint8(cv2.merge((h, s, v2)))
img2 = cv2.cvtColor(img2, cv2.COLOR_HSV2BGR)
return img2
# 图像旋转(以任意点为中心旋转)
def image_rotate(src, rotate=0):
h, w, c = src.shape
M = handle_rotate_val(w // 2, h // 2, rotate)
img = cv2.warpAffine(src, M, (w, h))
return img
def yuxianpinggu(x, y):
ans = 0
for i in range(0, 3):
ans = ans + (x[i] - y[i])**2
return ans
def zengqiangduibi(img0):
a = 2
b = 0.8
dst = img0
for i in range(img0.shape[0]):
for j in range(img0.shape[1]):
for c in range(3):
color = img0[i, j][c] * a + b
if color > 255:
dst[i, j][c] = 255
elif color < 0:
dst[i, j][c] = 0
return dst
def zengqiangduibi1(img0):
clahe = cv2.createCLAHE(clipLimit=10, tileGridSize=(20, 20))
# convert from BGR to LAB color space
lab = cv2.cvtColor(img0, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab) # split on 3 different channels
l2 = clahe.apply(l) # apply CLAHE to the L-channel
lab = cv2.merge((l2, a, b)) # merge channels
img0 = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR) # convert from LAB to BGR
return img0
class getbqujieguo():
def __init__(self, images):
# Load a model
# load an official model
model = YOLO("/home/zzb/ultralytics/weights/cqu.pt")
# Predict with the model
# img0=cv2.imread(file_pathname+filename)
img0 = images
# img0=zhengqiang2(img0)
img0 = zengqiangduibi1(img0)
# img0=log_transfor(img0,-100)
time.sleep(0.1)
res = model(images, conf=0.55, iou=0.2) # predict on an image
res = list(res)[0] # get result from generator
up = []
down = []
for i in res.boxes.numpy():
# print(i.xyxy[0].tolist())
if i.xyxy[0][3] > 350:
down.append(i.xyxy[0])
else:
up.append(i.xyxy[0])
res_plotted = res.plot()
# self.plot_image= res_plotted
upjieguo = []
simjieguo = []
self.fabujieguo = ''
up.sort(key=lambda x: x[0])
if len(up) == 3:
juhe = []
for i in range(3):
tmp0 = img0[int(up[i][1]):int(up[i][3]),
int(up[i][0]):int(up[i][2])]
# cv2.imwrite("/home/zzb/test1/" + filename[0:len(filename) - 4] + str(i) + str(1) + ".jpg", tmp0)
tmp1 = cv2.mean(tmp0)
juhe.append(tmp1)
for i in range(2, -1, -1):
tmp0 = img0[int(up[i][1]):int(up[i][3]),
int(up[i][0]):int(up[i][2])]
tmp1 = img0[int(up[i - 1][1]):int(up[i - 1][3]),
int(up[i - 1][0]):int(up[i - 1][2])]
tmp0 = cv2.resize(tmp0, (224, 224),
interpolation=cv2.INTER_LINEAR)
tmp1 = cv2.resize(tmp1, (224, 224),
interpolation=cv2.INTER_LINEAR)
jieguo = similarity.runAllImageSimilaryFun(tmp0, tmp1)
xiangsidu = yuxianpinggu(juhe[i], juhe[i - 1])
tmp2 = np.hstack((tmp0, tmp1))
if i == 0:
upjieguo.append(min(jieguo))
else:
upjieguo.append(min(jieguo))
simjieguo.append(xiangsidu)
print(juhe)
print(simjieguo)
print(upjieguo)
# jieguozong=[]
# jieguozong.append(simjieguo,upjieguo,[0,2,1])
print("sim ssssss up:", max(simjieguo) - min(simjieguo))
zidian = {0: 0, 1: 2, 2: 1}
if max(simjieguo) - min(simjieguo) > 650 and max(upjieguo) - min(upjieguo) > 0.05:
print("666")
for i in range(2, -1, -1):
tmp0 = img0[int(up[i][1]):int(up[i][3]),
int(up[i][0]):int(up[i][2])]
tmp1 = img0[int(up[i - 1][1]):int(up[i - 1][3]),
int(up[i - 1][0]):int(up[i - 1][2])]
tmp0 = cv2.resize(tmp0, (224, 224),
interpolation=cv2.INTER_AREA)
tmp1 = cv2.resize(tmp1, (224, 224),
interpolation=cv2.INTER_AREA)
tmp2 = np.hstack((tmp0, tmp1))
if min(simjieguo) == simjieguo[2 - i]:
k = zidian[2 - i]
print("shangcengcuod shi" + str(k) + "ge")
self.fabujieguo = str(k) + "/"
# cv2.rectangle(img0,(500,100),(1000,500),(0,255,0),3)
cv2.rectangle(img0, (int(up[k][0]), int(up[k][1])),
(int(up[k][2]), int(up[k][3])), (0, 255, 0), 2)
# cv2.imwrite("/home/zzb/xiangsi/"+filename[0:len(filename)-4]+str(i)+str(1)+".jpg",tmp2)
else:
pass
# cv2.imwrite("/home/zzb/dif/"+filename[0:len(filename)-4]+str(i)+str(1)+".jpg",tmp2)
else:
print("yiyang")
# cv2.imwrite("/home/zzb/xiangsi/" + filename[0:len(filename) - 4] + str(i) + str(1) + ".jpg", img0)
downjieguo = []
simjieguo = []
down.sort(key=lambda x: x[0])
if len(down) == 3:
juhe = []
for i in range(3):
tmp0 = img0[int(down[i][1]):int(down[i][3]),
int(down[i][0]):int(down[i][2])]
tmp1 = cv2.mean(tmp0)
juhe.append(tmp1)
for i in range(2, -1, -1):
tmp0 = img0[int(down[i][1]):int(down[i][3]),
int(down[i][0]):int(down[i][2])]
tmp1 = img0[int(down[i - 1][1]):int(down[i - 1][3]),
int(down[i - 1][0]):int(down[i - 1][2])]
tmp0 = cv2.resize(tmp0, (224, 224),
interpolation=cv2.INTER_LINEAR)
tmp1 = cv2.resize(tmp1, (224, 224),
interpolation=cv2.INTER_LINEAR)
jieguo = similarity.runAllImageSimilaryFun(tmp0, tmp1)
xiangsidu = yuxianpinggu(juhe[i], juhe[i - 1])
tmp2 = np.hstack((tmp0, tmp1))
if i == 0:
downjieguo.append(min(jieguo))
else:
downjieguo.append(min(jieguo))
simjieguo.append(xiangsidu)
print(juhe)
print(simjieguo)
print(upjieguo)
# jieguozong=[]
# jieguozong.append(simjieguo,upjieguo,[0,2,1])
zidian = {0: 0, 1: 2, 2: 1}
print("sim ssssss:", max(simjieguo) - min(simjieguo))
if max(simjieguo) - min(simjieguo) > 650 and max(downjieguo) - min(downjieguo) > 0.05:
print("666")
for i in range(2, -1, -1):
tmp0 = img0[int(down[i][1]):int(down[i][3]),
int(down[i][0]):int(down[i][2])]
tmp1 = img0[int(down[i - 1][1]):int(down[i - 1][3]),
int(down[i - 1][0]):int(down[i - 1][2])]
tmp0 = cv2.resize(tmp0, (224, 224),
interpolation=cv2.INTER_AREA)
tmp1 = cv2.resize(tmp1, (224, 224),
interpolation=cv2.INTER_AREA)
tmp2 = np.hstack((tmp0, tmp1))
if min(simjieguo) == simjieguo[2 - i]:
k = zidian[2 - i]
print("xiacengcuod shi" + str(k) + "ge")
if self.fabujieguo == '':
self.fabujieguo = "/"+self.fabujieguo
self.fabujieguo = self.fabujieguo + str(k)
# cv2.rectangle(img0,(500,100),(1000,500),(0,255,0),3)
cv2.rectangle(img0, (int(down[k][0]), int(down[k][1])),
(int(down[k][2]), int(down[k][3])), (0, 255, 0), 2)
# cv2.imwrite("/home/zzb/xiangsi/"+filename[0:len(filename)-4]+str(i)+str(1)+".jpg",tmp2)
else:
pass
# cv2.imwrite("/home/zzb/dif/"+filename[0:len(filename)-4]+str(i)+str(1)+".jpg",tmp2)
else:
print("yiyang")
# cv2.imwrite("/home/zzb/xiangsi/"+filename[0:len(filename)-4]+str(i)+str(1)+".jpg",img0)
self.plot_image = img0
self.det_image = res_plotted
def get_jieguo(self):
return self.fabujieguo
def get_plot_image(self):
return self.plot_image
def get_det_image(self):
return self.det_image