Real-time coordinate printing of YOLOv5 recognition target

introduction

This function may seem tasteless, but it is of great significance for UAV target recognition and tracking. Through the coordinate position of the target in the camera field of view, the position of the UAV relative to the target can be calculated, so as to correct the position of the UAV. Therefore, adding code to print coordinates is not the main purpose, the key is to find the position of coordinate information in the project.

accomplish

Add the following three lines of code under the plot_one_box function in plot.py in the utils folder to output the five-point coordinates of the target box.

print("左上点的坐标为:(" + str(c1[0]) + "," + str(c1[1]) + "),右上点的坐标为(" + str(c2[0]) + "," + str(c1[1]) + ")")
print("左下点的坐标为:(" + str(c1[0]) + "," + str(c2[1]) + "),右下点的坐标为(" + str(c2[0]) + "," + str(c2[1]) + ")")
print("中心点的坐标为:(" + str((c2[0] - c1[0]) / 2 + c1[0]) + "," + str((c2[1] - c1[1]) / 2 + c1[1]) + ")")

Note that the coordinate origin is captured to the upper left corner of the screen

Give the complete code of the modified plot_one_box() function:

def plot_one_box(x, img, color=None, label=None, line_thickness=3):
    # Plots one bounding box on image img
    tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1  # line/font thickness
    color = color or [random.randint(0, 255) for _ in range(3)]
    c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
    cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
    ######################################打印坐标#############################
    print("左上点的坐标为:(" + str(c1[0]) + "," + str(c1[1]) + "),右上点的坐标为(" + str(c2[0]) + "," + str(c1[1]) + ")")
    print("左下点的坐标为:(" + str(c1[0]) + "," + str(c2[1]) + "),右下点的坐标为(" + str(c2[0]) + "," + str(c2[1]) + ")")
    print("中心点的坐标为:(" + str((c2[0] - c1[0]) / 2 + c1[0]) + "," + str((c2[1] - c1[1]) / 2 + c1[1]) + ")")
    ##########################################################################    
    if label:
        tf = max(tl - 1, 1)  # font thickness
        t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
        c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
        cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA)  # filled
        cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)

Effect

  1. run image recognition

You can see that the location information of each target is printed out in the running results

  1. video recognition

Print out the target position in each frame of the video.

  1. Single camera real-time detection

Print out the target position for each frame at a frame rate of 30FPS.

  1. Multi-camera real-time detection

Coordinates can still be printed effectively during multi-thread recognition.

On the way to school, you and I encourage each other (๑•̀ㅂ•́)و✧

Guess you like

Origin blog.csdn.net/Albert_yeager/article/details/128839453