numpy 如何将cv2.rectangle边框转换为YoloV4注解格式(相对x,y,w,h)?

tgabmvqs  于 2023-08-05  发布在  其他
关注(0)|答案(2)|浏览(95)

我已经训练了一个Yolo4网络,它给我的边界框是:

img_array = cv2.cvtColor(cv2.imread('image.png'), cv2.COLOR_BGR2RGB)
classes, scores, bboxes = model.detect(img_array, CONFIDENCE_THRESHOLD, NMS_THRESHOLD)

box = bboxes[0]

(x, y) = (box[0], box[1])
(w, h) = (box[2], box[3])

字符串
当我使用cv2.rectangle将图像保存为:

cv2.rectangle(img_array, (x, y), (x + w, y + h), (127,0,75), 1)

cv2.imwrite('image.png',img_array)


它给了我一个非常好的边界框绘制。我想使用这个box和图像数组的形状来创建一个文本文件,该文件的格式为Yolov4x,y,w,h浮动值在0和1之间相对于图像大小
让我们假设我的价值观是:

img_array.shape -> (443, 1265, 3)
box -> array([489, 126, 161, 216], dtype=int32)


所以它给了我

(x, y) = (box[0], box[1]) -> (489, 126)
(w, h) = (box[2], box[3]) -> (161, 216)


此外,我在文本文件中使用LabelImg创建的边界框如下

0.453125 0.538462 0.132212 0.509615 # 0 is the class


如何使用这些坐标以Yolov4获取?这有点令人困惑。我用了很多代码从this answer似乎不工作。
我也试过使用这个代码,但我不知道这是否正确。即使这是正确的,我也不知道如何获得x_, y_

def yolov4_format(img_shape,box):
    x_img, y_img, c = img_shape
    (x, y) = (box[0], box[1])
    (w, h) = (box[2], box[3])
    
    x_, y_ = None # logic for these?
    w_ = w/x_img
    h_ = h/y_img
    return x_,y_, w_, h_

vwhgwdsa

vwhgwdsa1#

我想我已经接近解决xy不是绝对的,而是矩形框的中心为described by AlexyAB in this answer。所以我跟进了code for LabelImg,找到了一个代码,并将其修改为我的用例。

def bnd_box_to_yolo_line(box,img_size):
        (x_min, y_min) = (box[0], box[1])
        (w, h) = (box[2], box[3])
        x_max = x+w
        y_max = y+h
        
        x_center = float((x_min + x_max)) / 2 / img_size[1]
        y_center = float((y_min + y_max)) / 2 / img_size[0]

        w = float((x_max - x_min)) / img_size[1]
        h = float((y_max - y_min)) / img_size[0]

        return x_center, y_center, w, h

字符串
所有你需要的是边界框和图像形状

zyfwsgd6

zyfwsgd62#

有一种更直接的方法可以使用pybboxes来完成这些事情。安装时,

pip install pybboxes

字符串
你的情况

import pybboxes as pbx

voc_bbox = (489, 126, 161, 216)
W, H = 443, 1265  # WxH of the image
pbx.convert_bbox(voc_bbox, from_type="coco", to_type="yolo", image_width=W, image_height=H)
>>> (1.2855530474040633, 0.18498023715415018, 0.36343115124153497, 0.1707509881422925)


请注意,转换为YOLO格式需要图像的宽度和高度以进行缩放。

相关问题