-
Notifications
You must be signed in to change notification settings - Fork 0
/
yolo_to_normal.py
45 lines (34 loc) · 1.15 KB
/
yolo_to_normal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import cv2, os, shutil
"""
--> This function takes each line of txt file
-- > returns actual bbox and class from that line. (Converted from Yolo format to OpenCV format)
--> cls is the integer form of yolo class, bbox is (x1,y1,x2,y2) where (x1,y1) represents top left position of image and (x2,y2) represents bottom right position.
"""
def convert_to_normal(dt,dh,dw):
# Split string to float
cls = int(dt.split(' ')[0])
x = float(dt.split(' ')[1])
y = float(dt.split(' ')[2])
w = float(dt.split(' ')[3])
h = float(dt.split(' ')[4])
l = int((x - w / 2) * dw)
r = int((x + w / 2) * dw)
t = int((y - h / 2) * dh)
b = int((y + h / 2) * dh)
if l < 0:
l = 0
if r > dw - 1:
r = dw - 1
if t < 0:
t = 0
if b > dh - 1:
b = dh - 1
return cls, (l,t,r,b)
im = cv2.imread(os.path.join('test.png'))
dh, dw, _ = im.shape
with open(os.path.join('test.txt'),'r') as f:
for lines in f.readlines():
cls, bbox = convert_to_normal(lines, dh,dw)
print('class index -->', cls)
print('Bounding box -->',bbox)
# img_crop = im[bbox[1]:bbox[3], bbox[0]:bbox[2]]