赞
踩
主要是把测距部分加在了画框附近,首先提取边框的像素点坐标,然后计算边框像素点高度,在根据 公式 D = (F*W)/P 计算目标距离
for \*xyxy, conf, cls in reversed(det): if save_txt: # Write to file xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh line = (cls, \*xywh, conf) if save_conf else (cls, \*xywh) # label format with open(txt_path + '.txt', 'a') as f: f.write(('%g ' \* len(line)).rstrip() % line + '\n') if save_img or save_crop or view_img: # Add bbox to image x1 = int(xyxy[0]) #获取四个边框坐标 y1 = int(xyxy[1]) x2 = int(xyxy[2]) y2 = int(xyxy[3]) h = y2-y1 if names[int(cls)] == "person": c = int(cls) # integer class 整数类 1111111111 label = None if hide_labels else ( names[c] if hide_conf else f'{names[c]} {conf:.2f}') # 111 dis_m = person_distance(h) # 调用函数,计算行人实际高度 label += f' {dis\_m}m' # 将行人距离显示写在标签后 txt = '{0}'.format(label) annotator.box_label(xyxy, txt, color=colors(c, True)) if names[int(cls)] == "car": c = int(cls) # integer class 整数类 1111111111 label = None if hide_labels else ( names[c] if hide_conf else f'{names[c]} {conf:.2f}') # 111 dis_m = car_distance(h) # 调用函数,计算汽车实际高度 label += f' {dis\_m}m' # 将汽车距离显示写在标签后 txt = '{0}'.format(label) annotator.box_label(xyxy, txt, color=colors(c, True)) if save_crop: save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True)
到上述步骤就已经实现了单目测距过程,下边是一些小细节修改,可以不看
为了实时显示画面,对运行的py文件点击编辑配置,在形参那里输入–view-img --save-txt
但实时显示画面太大,我们对显示部分做了修改,这部分也可以不要,具体是把代码
if view_img:
cv2.imshow(str(p), im0)
cv2.waitKey(1) # 1 millisecond
替换成
if view_img:
cv2.namedWindow("Webcam", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Webcam", 1280, 720)
cv2.moveWindow("Webcam", 0, 100)
cv2.imshow("Webcam", im0)
cv2.waitKey(1)
# YOLOv5 声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/977074
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。