I have trained a Yolo4 network and it is giving me bounding boxes as:
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])
When I save the image by using cv2.rectangle
as:
cv2.rectangle(img_array, (x, y), (x + w, y + h), (127,0,75), 1)
cv2.imwrite('image.png',img_array)
IT gives me a very good bounding box plotted. I want to use this box
and shape of image array to create a text file which is in the Yolov4
format as x,y,w,h
floating values between 0 and 1 relative to image size.
Let us suppose I have my values as:
img_array.shape -> (443, 1265, 3)
box -> array([489, 126, 161, 216], dtype=int32)
So it gives me
(x, y) = (box[0], box[1]) -> (489, 126)
(w, h) = (box[2], box[3]) -> (161, 216)
Also the Bounding Boxes created by me using LabelImg
in the Text file are as
0.453125 0.538462 0.132212 0.509615 # 0 is the class
How can I use these coordinates to get in Yolov4
format? It is a bit confusing. I have used many codes from this answer does not seem to work.
Also, I tried using this code but I don't know if that's right or not. Even if that's right, I have no idea how to get 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_