Use yolov5 for target detection and crop the detected target

Use yolov5 for target detection and crop the detected target

Written in the front: About the debugging and operation of yolov5, I will not go into details here. For the debugging and operation of yolov5, please see: https://www.bilibili.com/video/BV1tf4y1t7ru/spm_id_from=333.999.0.0&vd_source=043dc71f3eaf6a0ccb6dada9dbd8be37
This article mainly explains cutting.

Requirements: Identify the characters in the picture and cut them out

If you only need to recognize people, you only need to set parameters in yolov5, for example, when using the command line to run: python --classes 0
that is, set the parameters to only recognize people. In addition, the detected target needs to be cut out, and the coordinates of the center point of the target and the width and height of the target are required, so the information of the target needs to be saved. Realize person detection and save the detected target information. The running command is: python --classes0 --save-txt
In the tx file that saves the target information, for example, the following figure: 0 0.682078 0.495935 0.613014 0.99187, which represent from left to right: target type (0 represents person), The x value (x_center) of the coordinates of the center point of the target, the y value (y_center) of the coordinates of the center point of the target, the width (width) of the target, and the height (height) of the target.
insert image description here
insert image description here
The condition for clipping is to get x1, x2, y1, y2. obviously

x1=x_center-width/2
x2=x_center+width/2
y1=y_center-height/2
y2=y_center+height/2

However, since the x_center, y_center, width, and height stored in the txt file of yolov5 are all normalized, the x1, x2, y1, and y2 values ​​​​calculated in the above formula are all normalized. value, what we want is the original value, so

x1=(x_center-width/2)*整张图片的宽度(注:是整张图片的宽度不是目标的宽度,这里的宽度就是像素宽度)
x2=(x_center+width/2)*整张图片的宽度
y1=(y_center-height/2)*整张图片的高度(注:是整张图片的高度不是目标的高度,这里的高度就是像素高度)
y2=(y_center+height/2)*整张图片的高度

Get the four values ​​of x1, x2, y1, y2, then cut x1---->x2 in the horizontal direction, and cut y1---->y2 in the vertical direction.
Code:

import os
import cv2

def main():
	#图片路径
    img_path = './data/images/hg.jpg'
    #txt文件路径
    label_path = './runs/detect/exp23/labels/hg.txt'
    # 读取图片,结果为三维数组
    img = cv2.imread(img_path)
    # 图片宽度(像素)
    w = img.shape[1]
    # 图片高度(像素)
    h = img.shape[0]
    # 打开文件,编码格式'utf-8','r+'读写
    f = open(label_path, 'r+', encoding='utf-8')  
    # 读取txt文件中的第一行,数据类型str
    line = f.readline()
    # 根据空格切割字符串,最后得到的是一个list
    msg = line.split(" ")
    x1 = int((float(msg[1]) - float(msg[3]) / 2) * w)  # x_center - width/2
    y1 = int((float(msg[2]) - float(msg[4]) / 2) * h)  # y_center - height/2
    x2 = int((float(msg[1]) + float(msg[3]) / 2) * w)  # x_center + width/2
    y2 = int((float(msg[2]) + float(msg[4]) / 2) * h)  # y_center + height/2
    print(x1, ",", y1, ",", x2, ",", y2)
    #裁剪
    img_roi = img[y1:y2,x1:x2]
    save_path='./cutpictures/hg.jpg'
    cv2.imwrite(save_path,img_roi)

if __name__ == '__main__':  
    main() 

Effect display:
Target detection
Please add a picture description
and cropping
Please add a picture description
are written later: In the above code implementation, because I already know that there is only one person in the picture, there is only one line in the txt file, so I only read one line, f.readlin()if there are multiple targets person, then f.readlines()read multiple lines, and then use the for loop to cut line by line.

Correction: In the follow-up operation, I want to cut out the farthest person in the picture, which is the red frame in the picture below. Then I only need to compare y_center and cut out the largest y_center. According to this idea, what I cut out is is a green frame.
insert image description here
Later verification found that the coordinate system in yolov is as follows:
insert image description here
but it does not affect the understanding of the above related processes.

Guess you like

Origin blog.csdn.net/weixin_44747173/article/details/127277989