我已经训练了一个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
和图像数组的形状来创建一个文本文件,该文件的格式为Yolov4
,x,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_
型
2条答案
按热度按时间vwhgwdsa1#
我想我已经接近解决
x
和y
是不是绝对的,而是矩形框的中心为described by AlexyAB in this answer。所以我跟进了code for LabelImg,找到了一个代码,并将其修改为我的用例。字符串
所有你需要的是边界框和图像形状
zyfwsgd62#
有一种更直接的方法可以使用pybboxes来完成这些事情。安装时,
字符串
你的情况
型
请注意,转换为YOLO格式需要图像的宽度和高度以进行缩放。