YOLOV5中获取det结果时*xyxy格式转化普通列表
for det in pred:
if det is not None and len(det):
det[:,:4]=scale_coords(img.shape[2:],det[:,:4],img0.shape).round()
xyxy_point = []
center_point = []
im0s = img0
# im0s = cv2.cvtColor(im0s, cv2.COLOR_BGR2RGB)
for *xyxy,conf,cls in det:
cls_id=int(cls)
label = f'{conf:.2f} {self.names[cls_id]}'
dyd1, dyd2 = (int(xyxy[0]), int(xyxy[1])), (int(xyxy[2]), int(xyxy[3]))
xyxy_point.append([dyd1, dyd2])
plot_one_box(x=xyxy, img=im0s, color=colors[cls_id], label=label, line_thickness=2)
X = (int(xyxy[0]) + int(xyxy[2])) / 2
Y = (int(xyxy[1]) + int(xyxy[3])) / 2
print(f"\n{label}:{X},{Y}")
center_point.append([X, Y])
# xyxy = [(int(xyxy[i]), int(xyxy[i + 1])) for i in range(0, len(xyxy), 2)]#实现上面dyd1dyd2同样功能
# print(xyxy)
annotator.box_label(xyxy, depth_image, label)
# if self.names[int(cls)]=="person" or self.names[int(cls)]=="car" or self.names[int(cls)]=="bicycle":
# label=(f'{conf:.2f} {self.names[cls_id]}')
# annotator.box_label(xyxy, depth_image,label, color=colors(cls_id, True))
result= annotator.result()
return xyxy_point,center_point,result
对xyxy进行print,输出如下:
[tensor(237., device=‘cuda:0’), tensor(4., device=‘cuda:0’), tensor(634., device=‘cuda:0’), tensor(478., device=‘cuda:0’)]
改为普通列表格式,有两种:
dyd1, dyd2 = (int(xyxy[0]), int(xyxy[1])), (int(xyxy[2]), int(xyxy[3]))
xyxy = [(int(xyxy[i]), int(xyxy[i + 1])) for i in range(0, len(xyxy), 2)]
以上两行代码都实现转为普通列表格式:
[[(237, 4), (634, 478)]]