-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_bounding_boxes.py
46 lines (39 loc) · 2.1 KB
/
get_bounding_boxes.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
"""
getBoundingBoxes(vid, i) returns the ground-truth bounding boxes for the "ith" annotated frame of
video "vid", where "vid" is an EgoHands video metadata structure.
Boxes is a 4x4 matrix, where each row corresponds to a hand bounding box in the format [x y width
height], where x and y mark the top left corner of the box. The rows from top to bottom contain
the bounding boxes for "own left", "own right", "other left", and "other right" hand respectively.
If a hand is not in the frame, the values are set to 0.
For full dataset details, see the
<a href="matlab: web('http://vision.soic.indiana.edu/egohands')">EgoHands project website</a>.
See also getFramePath, getMetaBy, getSegmentationMask, showLabelsOnFrame
"""
import numpy as np
def get_bounding_boxes(video, i):
"""get_bounding_boxes returns the bounding boxes for the hands in the queried video and frame"""
boxes = np.zeros([4, 4])
if np.any(video.loc['labelled_frames'][0][i][1]):
box = segmentation2box(np.int32(video.loc['labelled_frames'][0][i][1]))
boxes[0, :] = box
if np.any(video.loc['labelled_frames'][0][i][2]):
box = segmentation2box(np.int32(video.loc['labelled_frames'][0][i][2]))
boxes[1, :] = box
if np.any(video.loc['labelled_frames'][0][i][3]):
box = segmentation2box(np.int32(video.loc['labelled_frames'][0][i][3]))
boxes[2, :] = box
if np.any(video.loc['labelled_frames'][0][i][4]):
box = segmentation2box(np.int32(video.loc['labelled_frames'][0][i][4]))
boxes[3, :] = box
return boxes
def segmentation2box(shape):
"""segmentation2box returns a bounding box for a hand in the queried video and frame"""
box_xyxy = np.round(np.array([np.min(shape[:, 0]), np.min(shape[:, 1]),
np.max(shape[:, 0]), np.max(shape[:, 1])]))
box_xyxy[0] = max(1, box_xyxy[0])
box_xyxy[1] = max(1, box_xyxy[1])
box_xyxy[2] = min(1280, box_xyxy[2])
box_xyxy[3] = min(720, box_xyxy[3])
box_xywh = np.array([box_xyxy[0], box_xyxy[1], box_xyxy[2]-box_xyxy[0]+1,
box_xyxy[3]-box_xyxy[1]+1])
return box_xywh