Print information on the YOLOv5 real-time detection screen

       In order to make the results more intuitive, the counting results, frame rate and other information can be printed on the real-time detection screen of the camera. This article takes the real-time detection of a single camera as an example to introduce the implementation method.

Implementation

       Operate in the detect.py file and find the following location (your number of lines may be different from mine, just press Ctrl+F to find cv2.imshow)

        The cv2.imshow() function is used to print the screen, so just add the following code before it.

#############################在窗口中显示标签#############################
text = 'count = %d, FPS=30, Albert_yeager'%(count)
cv2.putText(im0, text, (40, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 4)
# 要绘制的图像(im0)
# 要绘制的文本字符串(text)
# 文本的位置(x, y),窗口左上角为(0,0)
# 要使用的字体类型(font),这里用OpenCV的内嵌字体
# 字体大小(font_scale),在此处为1
# 字体颜色(font_color),在此处为红色(0, 0, 255)
# 字体线宽(thickness),在此处为4
#########################################################################

       In fact, there are only two lines of code added. The first line is to define the content to be printed, and the format can be in the standard output format. The count here is the count of the target. For detailed implementation methods, please refer to my previous blog:

 YOLOv5 achieves target count_Albert_yeager's Blog-CSDN Blog

       The main codes involved in the counting function (both in previous blogs) are as follows:

# Write results+计数
    count = 0
    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()  
        line = (cls, *xywh, conf) if opt.save_conf else (cls, *xywh) 
        with open(txt_path + '.txt', 'a') as f:
            f.write(('%g ' * len(line)).rstrip() % line + '\n')

        if save_img or view_img:  # Add bbox to image
            c = int(cls)# integer class分类数
            label = f'{names[int(cls)]} {conf:.2f}'
            plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
            count += 1

       After the FPS is the frame rate. Here I directly set the value for the code, but in fact, the frame rate will change during the real-time detection process, so you need to consider printing while calculating the frame rate. Leave a hole here, and then come repair. ( ̄ε(# ̄)☆╰╮o( ̄dish ̄///)

       The second line of adding code is to use the text output function that comes with OpenCV. Note that several parameters are as follows:

  1. The image to be drawn is consistent with the first parameter of cv2.imshow(), indicating that the drawing operation is performed on the captured, recognized, and framed image
  2. The text string to draw (i.e. text)
  3. The position (x, y) of the text on the screen, note that the upper left corner of the window is the origin of the coordinates, the right direction is the positive direction of x, and the downward direction is the positive direction of y (the regulation of OpenCV)
  4. The font type (font) to be used, here use OpenCV's embedded font cv2.FONT_HERSHEY_SIMPLEX
  5. Font size (font_scale), here is 1 (you can also try other numbers, it will be beautiful)
  6. Font color (font_color), here red (0, 0, 255)
  7. Font line thickness (thickness), here is 4

Effect

       Counting, frame rate and printing of labels are enabled as shown. Hope to help everyone.

Tried it in the library, it works fine

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

Guess you like

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