forked from ArtLabss/tennis-tracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
61 lines (50 loc) · 1.98 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
import argparse
import cv2
import torch
import numpy as np
def crop_center(image):
# crop the center of an image and matching the height with the width of the image
shape = image.shape[:-1]
max_size_index = np.argmax(shape)
diff1 = abs((shape[0] - shape[1]) // 2)
diff2 = shape[max_size_index] - shape[1 - max_size_index] - diff1
return image[:, diff1: -diff2] if max_size_index == 1 else image[diff1: -diff2, :]
def get_dtype():
dev = 'cuda' if torch.cuda.is_available() else 'cpu'
device = torch.device(dev)
if dev == 'cuda':
dtype = torch.cuda.FloatTensor
else:
dtype = torch.FloatTensor
print(f'Using device {device}')
return dtype
def get_video_properties(video):
# Find OpenCV version
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
# get videos properties
if int(major_ver) < 3:
fps = video.get(cv2.cv.CV_CAP_PROP_FPS)
length = int(video.get(cv2.cv.CAP_PROP_FRAME_COUNT))
v_width = int(video.get(cv2.cv.CAP_PROP_FRAME_WIDTH))
v_height = int(video.get(cv2.cv.CAP_PROP_FRAME_HEIGHT))
else:
fps = video.get(cv2.CAP_PROP_FPS)
length = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
v_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
v_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
return fps, length, v_width, v_height
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def get_stickman_line_connection():
# stic kman line connection with keypoints indices for R-CNN
line_connection = [
(7, 9), (7, 5), (10, 8), (8, 6), (6, 5), (15, 13), (13, 11), (11, 12), (12, 14), (14, 16), (5, 11), (12, 6)
]
return line_connection