-
Notifications
You must be signed in to change notification settings - Fork 0
/
bounding_box.py
83 lines (66 loc) · 3.02 KB
/
bounding_box.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# This class will hold the required mappings from image id to image name and bounding box information
import cv2
class BoundingBox:
def __init__(self, folder_path, img_file, bbox_file, height, width):
self.folder_path = folder_path
self.img_file = img_file
self.bbox_file = bbox_file
self.height = height
self.width = width
# image names will be in all lower()
self.imgNameToId = {}
# put the scaled x1, y1, x2, y2 values for bounding box
self.idToBbox = {}
with open(self.img_file, 'r') as read_file:
for line in read_file:
image_id, image_name = line.strip().split(' ')
image_name = image_name.split('/')[-1].lower()
self.imgNameToId[image_name] = image_id
with open(self.bbox_file, 'r') as read_file:
for line in read_file:
image_id, x, y, width, height = line.strip().split(' ')
self.idToBbox[image_id] = (x, y, width, height)
def get_bbox(self, image_name):
img = cv2.imread(self.folder_path + '/' + image_name)
height, width, channels = img.shape
image_id = self.imgNameToId[image_name.lower()]
x, y, w, h = self.idToBbox[image_id]
x, y, w, h = float(x), float(y), float(w), float(h)
x_scale = self.width / width
y_scale = self.height / height
x_scaled = x * x_scale
y_scaled = y * y_scale
w_scaled = w * x_scale
h_scaled = h * y_scale
x1, y1, x2, y2 = int(x_scaled), int(y_scaled), int(x_scaled + w_scaled), int(y_scaled + h_scaled)
return x1, y1, x2, y2
def get_bbox_unscaled(self, image_name):
image_id = self.imgNameToId[image_name.lower()]
x, y, w, h = self.idToBbox[image_id]
x, y, w, h = float(x), float(y), float(w), float(h)
x1, y1, x2, y2 = int(x), int(y), int(x + w), int(y + h)
return x1, y1, x2, y2
def get_bbox_from_path(self, img_path):
img = cv2.imread(img_path)
height, width, channels = img.shape
image_name = img_path.rsplit('/', 1)[-1]
image_id = self.imgNameToId[image_name.lower()]
x, y, w, h = self.idToBbox[image_id]
x, y, w, h = float(x), float(y), float(w), float(h)
x_scale = self.width / width
y_scale = self.height / height
x_scaled = x * x_scale
y_scaled = y * y_scale
w_scaled = w * x_scale
h_scaled = h * y_scale
x1, y1, x2, y2 = int(x_scaled), int(y_scaled), int(x_scaled + w_scaled), int(y_scaled + h_scaled)
return x1, y1, x2, y2
def get_bbox_from_path_unscaled(self, img_path):
img = cv2.imread(img_path)
height, width, channels = img.shape
image_name = img_path.rsplit('/', 1)[-1]
image_id = self.imgNameToId[image_name.lower()]
x, y, w, h = self.idToBbox[image_id]
x, y, w, h = float(x), float(y), float(w), float(h)
x1, y1, x2, y2 = int(x), int(y), int(x + w), int(y + h)
return x1, y1, x2, y2