I have Yolo
format bounding box annotations of objects saved in a .txt
files. Now I want to load those coordinates and draw it on the image using OpenCV
, but I don’t know how to convert those float values into OpenCV
format coordinates values
I tried this post but it didn’t help, below is a sample example of what I am trying to do
Code and output
import matplotlib.pyplot as plt
import cv2
img = cv2.imread(<image_path>)
dh, dw, _ = img.shape
fl = open(<label_path>, 'r')
data = fl.readlines()
fl.close()
for dt in data:
_, x, y, w, h = dt.split(' ')
nx = int(float(x)*dw)
ny = int(float(y)*dh)
nw = int(float(w)*dw)
nh = int(float(h)*dh)
cv2.rectangle(img, (nx,ny), (nx+nw,ny+nh), (0,0,255), 1)
plt.imshow(img)
Actual Annotations and Image
0 0.286972 0.647157 0.404930 0.371237
0 0.681338 0.366221 0.454225 0.418060