-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBallTrackNet.py
133 lines (115 loc) · 5.9 KB
/
BallTrackNet.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import cv2
import torch.nn as nn
import numpy as np
import torch
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, pad, bias=True, bn=True):
super().__init__()
if bn:
self.block = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size, padding=pad, bias=bias),
nn.ReLU(),
nn.BatchNorm2d(out_channels)
)
else:
self.block = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size, padding=pad, bias=bias),
nn.ReLU()
)
def forward(self, x):
return self.block(x)
class BallTrackerNet(nn.Module):
"""
Deep network for ball detection
"""
def __init__(self, out_channels=256, bn=True):
super().__init__()
self.out_channels = out_channels
# Encoder layers
layer_1 = ConvBlock(in_channels=9, out_channels=64, kernel_size=3, pad=1, bias=True, bn=bn)
layer_2 = ConvBlock(in_channels=64, out_channels=64, kernel_size=3, pad=1, bias=True, bn=bn)
layer_3 = nn.MaxPool2d(kernel_size=2, stride=2)
layer_4 = ConvBlock(in_channels=64, out_channels=128, kernel_size=3, pad=1, bias=True, bn=bn)
layer_5 = ConvBlock(in_channels=128, out_channels=128, kernel_size=3, pad=1, bias=True, bn=bn)
layer_6 = nn.MaxPool2d(kernel_size=2, stride=2)
layer_7 = ConvBlock(in_channels=128, out_channels=256, kernel_size=3, pad=1, bias=True, bn=bn)
layer_8 = ConvBlock(in_channels=256, out_channels=256, kernel_size=3, pad=1, bias=True, bn=bn)
layer_9 = ConvBlock(in_channels=256, out_channels=256, kernel_size=3, pad=1, bias=True, bn=bn)
layer_10 = nn.MaxPool2d(kernel_size=2, stride=2)
layer_11 = ConvBlock(in_channels=256, out_channels=512, kernel_size=3, pad=1, bias=True, bn=bn)
layer_12 = ConvBlock(in_channels=512, out_channels=512, kernel_size=3, pad=1, bias=True, bn=bn)
layer_13 = ConvBlock(in_channels=512, out_channels=512, kernel_size=3, pad=1, bias=True, bn=bn)
self.encoder = nn.Sequential(layer_1, layer_2, layer_3, layer_4, layer_5, layer_6, layer_7, layer_8, layer_9,
layer_10, layer_11, layer_12, layer_13)
# Decoder layers
layer_14 = nn.Upsample(scale_factor=2)
layer_15 = ConvBlock(in_channels=512, out_channels=256, kernel_size=3, pad=1, bias=True, bn=bn)
layer_16 = ConvBlock(in_channels=256, out_channels=256, kernel_size=3, pad=1, bias=True, bn=bn)
layer_17 = ConvBlock(in_channels=256, out_channels=256, kernel_size=3, pad=1, bias=True, bn=bn)
layer_18 = nn.Upsample(scale_factor=2)
layer_19 = ConvBlock(in_channels=256, out_channels=128, kernel_size=3, pad=1, bias=True, bn=bn)
layer_20 = ConvBlock(in_channels=128, out_channels=128, kernel_size=3, pad=1, bias=True, bn=bn)
layer_21 = nn.Upsample(scale_factor=2)
layer_22 = ConvBlock(in_channels=128, out_channels=64, kernel_size=3, pad=1, bias=True, bn=bn)
layer_23 = ConvBlock(in_channels=64, out_channels=64, kernel_size=3, pad=1, bias=True, bn=bn)
layer_24 = ConvBlock(in_channels=64, out_channels=self.out_channels, kernel_size=3, pad=1, bias=True, bn=bn)
self.decoder = nn.Sequential(layer_14, layer_15, layer_16, layer_17, layer_18, layer_19, layer_20, layer_21,
layer_22, layer_23, layer_24)
self.softmax = nn.Softmax(dim=1)
self._init_weights()
def forward(self, x, testing=False):
batch_size = x.size(0)
features = self.encoder(x)
scores_map = self.decoder(features)
output = scores_map.reshape(batch_size, self.out_channels, -1)
# output = output.permute(0, 2, 1)
if testing:
output = self.softmax(output)
return output
def _init_weights(self):
for module in self.modules():
if isinstance(module, nn.Conv2d):
nn.init.uniform_(module.weight, -0.05, 0.05)
if module.bias is not None:
nn.init.constant_(module.bias, 0)
elif isinstance(module, nn.BatchNorm2d):
nn.init.constant_(module.weight, 1)
nn.init.constant_(module.bias, 0)
def inference(self, frames: torch.Tensor):
self.eval()
with torch.no_grad():
if len(frames.shape) == 3:
frames = frames.unsqueeze(0)
if next(self.parameters()).is_cuda:
frames.cuda()
# Forward pass
output = self(frames, True)
output = output.argmax(dim=1).detach().cpu().numpy()
if self.out_channels == 2:
output *= 255
x, y = self.get_center_ball(output)
return x, y
def get_center_ball(self, output):
"""
Detect the center of the ball using Hough circle transform
:param output: output of the network
:return: indices of the ball`s center
"""
output = output.reshape((360, 640))
# cv2 image must be numpy.uint8, convert numpy.int64 to numpy.uint8
output = output.astype(np.uint8)
# reshape the image size as original input image
heatmap = cv2.resize(output, (640, 360))
# heatmap is converted into a binary image by threshold method.
ret, heatmap = cv2.threshold(heatmap, 127, 255, cv2.THRESH_BINARY)
# find the circle in image with 2<=radius<=7
circles = cv2.HoughCircles(heatmap, cv2.HOUGH_GRADIENT, dp=1, minDist=1, param1=50, param2=8, minRadius=2,
maxRadius=7)
# check if there have any tennis be detected
if circles is not None:
# if only one tennis be detected
if len(circles) == 1:
x = int(circles[0][0][0])
y = int(circles[0][0][1])
return x, y
return None, None