-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.py
79 lines (60 loc) · 1.93 KB
/
Utils.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
from PIL import Image, ImageDraw
import math
def openImage(imgPath):
'''Opens PIL image given path.
:param imgPath: Path to image
:type imgPath: String
:return: Image read from path
:rtype: PIL image
'''
im = Image.open(imgPath)
return im
def calcBoundingBoxParams(bbox, anchorBBox):
'''Calculate the regressor parameters, given a bounding box
The 4 params, tx, ty, tw and th are not the same as the bbox.
The params are as given in the Faster RCNN paper.
:param bbox: bbox values, (x, y, w, h)
:type bbox: Tuple of floats
:param anchorBBox: Anchor bbox values (xa, ya, wa, ha)
:type anchorBBox: Tuple of floats
:return: set of 4 parameters
:rtype: 4 tuple of floats
'''
x, y, w, h = bbox
xa, ya, wa, ha = anchorBBox
tx = (x - xa) / float(wa)
ty = (y - ya) / float(ha)
tw = math.log(float(w / wa))
th = math.log(float(h / ha))
return (tx, ty, tw, th)
def getBoundingBoxes(annotations):
'''Parse bounding boxes from given annotations
:param annotations: annotations containing bounding boxes
:type annotations: dict
:return: bounding box coords
:rtype: list
'''
bbox = []
for annotation in annotations:
bbox.append(annotation['bbox'])
return bbox
def drawBoundingBox(img, bboxList):
'''Draw bounding boxes around objects in given image. Done in place.
:param img: Image which contains objects
:type img: PIL Image
:param bboxList: list of bounding boxes
:type bboxList: list of 4 tuples
:return: Image with bounding boxes drawn on it.
:rtype: PIL Image
'''
draw = ImageDraw.Draw(img)
x1, y1, x2, y2 = 0, 0, 0, 0
for bbox in bboxList:
x1 = int(bbox[0])
y1 = int(bbox[1])
x2 = int(bbox[2]) + x1
y2 = int(bbox[3]) + y1
draw.rectangle([(x1, y1), (x2, y2)])
## Good practice?
del draw
return img