Build An AI - ML Tennis Analysis System With YOLO, PyTorch, and Key Point Extraction (English (Auto-Generated) )
Build An AI - ML Tennis Analysis System With YOLO, PyTorch, and Key Point Extraction (English (Auto-Generated) )
install Ultra
time on your
import
model.
the
in
um results of of 0 do
to look at
confidence
which is going to be
video.
be back when it
open it
the
tennis ball
detector
detector um training.
tennis court
and
install ultral
and
have
going to
detection then
the
validation
to be uh this one.
specify the
YOLO s YOLO 5-
last last.
when it's
the
track right
come back
it um
own
tennis
court key
points
training.
set
import
import um
from
torch
import
Transformers
this and
device
going to put
imported in the
have
of the uh
the
that is
we can import
um a
transformation um a transformation
as
transforms
equal uh
pil image do
to P image
make it um 2 224 by
this into a
that we read
going
take an
index and
can write
image uh
the
self. image
directory then
this
BGR
KPS um
into a numpy
from 1D to
2D and at the end we just want it to be
so we can
type as np. FL
end uh or end
specifying key
can do
224
[Music]
we can
like this uh
data set
directory which is
it's um
the uh Val
um um so
reset
trained on other
U model uh um
uh on this
linear
layer which is just going to be a flat
layer
with
of the neural
to Cuda or
Epoch in range
and key
things step by
loader
that is
backward
points
model.
paste
tennis analyzer
write it right
run it
um um like frame by
frames
TS okay
import
uh
output
video
we want is going to
be
MJ PG
per
from
input
input video.
the
real
again uh outputed to
tracker
going to use
results of
throughout this
CLS and. to
object
of one
player
output
from
so um
uh
player
tracker as
the video
are
detecting
bounding
track
bounding
RGB
uh borders of
what id is it so we can
going to be
is minimum
to
output uh video
tracker
um
it's
run it
run is
over
correct and
yeah I think I'm missing one so yeah
right here
stops
whips
frames uh
function uh we can
be
be none as
default um so yeah if
then if we have
the
uh inside this
from
stop is true um and the stop path is not
is going to be stop
to
to be tracker
stops
slash
then player
it right
to a ball
tracker like
uh the class
delete this uh if
just predict it
fine
going to be ball
think this is
ball
Dash um slash
last do
pt and we are going to detect also the
as
uh save it again
won't crash
again um just give it a minute make sure
Court line
Court line
use it for
going to import uh
torch
uh
write the
cour line
init
function and we are going to take the
also
28 uh key points
um so FC is equal
to um
torch do
the 14 *
going to map it to a
it
to
image
is that we do it like
this
outputs
dot
squeezed
it um we can uh move it to
numpy like
original
and uh do it like
starting from
one each
be
I +1
um and X um
font
put a
be 5
it
this
but on
fine so
expose
here um
call TR
detector like
court key
points
detector
dot draw
key
points on
unnecessary
in the pandas uh
the
ball ball
start here so
Define
interpolate ball
positions
ball
there's no
import
pandas as
pd. uh data
it's going to be um X
then y1 then X2
uh
that we
be
from so um we have
DF
dot uh DF uh ball
positions dot 2
make it into a
it by
the ball
the bulb
goes
frame
color uh like
let's run it
right
the
called
Define
player
detections
player
detection
player
the bounding
utils.py
Define um
get um get
Center of BB
the
have from
dot BB box
utils
bounding
basically
minimum
distance um is equal to
each key
key
distances do
minimum
a minimum
distance
sort them
ascending
chosen
the chosen
filtered
player
choose
court uh
dimensions
center
called No Man's
players in the
constants
an init
width
um actually it's
double Aly
difference and it is
it's
player one
height in uh
meters
um it's going to be
pixels to
called
Mini
uh
dopy and
sis and we
can uh
from um we can sis. pad. append and we
from
constants
mini
function
of it so
drawing
rectangle
going to be uh 250
pixels so uh this white rectangle is
rectangle uh height
that you
the white uh
going to be uh 20
pixels um
set canvas
background box
takes a
it self Dot
y minus the
the
Court
self do padding
also
Define uh
set put
drawing
right
takes in the
all at
drawing key
01 or 0 Z to be
create
drawing key
point
quarts
going to be cross
in the the
conversions conversions.
to uh pixel
convert
pixel to a
distance to
from
simple cross
the
reference
height in
meters then
reference
height in
convert
meters to pixel
is should be constants
add from
utils um we should import
convert this
meter distance
so
point of
Court um start
have this
meters to
be constants do
the name it is
to give it the
convert
meters um
to
let's uh let's do
we
it again for
point4 and uh
4.4 we have other uh another uh another
back
self.
lines
copy paste
the
draw
the
do shapes like
just write
going to be negative
shapes as type
the
so CVT
color
on one frame
Define
draw mini
cord then
frames we then
uh have the
for frame in
pen
us a little bit
outside of this
mini
court
that so draw
mini
okay so we have an
something so let me
I think is going to be
you int
8 so let me run
again
RGB
give me a
point so in
and
valuable uh
circle on the
frame
give it the
let's call
it um there is something
missing
height of
yeah
we can make it in
red um but we can also expand the
be for example uh
500 so that it
yeah
in self.
um so
start
point
one so uh line
self.
drawing key
um like this
we can have um
results
good
functions
Min
uh self do
Court start
X self.
court
and
um
uh start whoops uh
Court start
get uh width
and uh
continue
the
um this
little
frames
folder um that is
called um
um
ball
analysis.
to also
M plot
tracker uh
detections do
threaded
put it right
here
window of
five and uh we are also going to set a
minimum
center it
so Center equal
mean um
afterwards uh we
out
it's it's
coordinates is being
positions Delta
each
ball
is all set to
and like
this
DF um ball
positions
within a certain
1.2
negative position
greater than
became
positive
change frame in uh
range I +
1 till I
following
the following
frame and
frame and if
the following
minimum
so let's do it like
here
continue on from
frame
nums
with ball
uh paste it in right
Define
get ball
shot
positions
function is
detect ball
ball
detector ball
tracker Dot
shot
not
let me run it
is
player
the
whole logic of
this um so let
convert
convert
bounding boxes
to mini
cour uh
coordinates and we are going to take the
um
player uh
ball
take the
player
one uh height in
for player
two like
this
frame number
boxes right
now
have it uh
have the
get
foot
position and it takes in a bounding box
uh we can do it like
uh expose it uh in the
position
of the BB
position
get the
in pixels
closest key
Point
foot position
uh for
close
closest key
closest uh
key
key
Point
candidates uh
keypoint
indices
points
of key Point
index times
measure the
distance which is going to be uh only in
distance so
just return
also this
player height in
can go up to uh up to 50 frames
the
um
say BB boxes
Heights in
bounding box of
bounding box uh
the object
position uh
object
closest uh key
closest keypoint
index and it's also going to take the
player height in
easily uh seen
and
point
so we can say
closest
um key
Point
from
keyo
X pixel
Point y y
measure
XY
let's uh
convert
pixel
distance to
met
equal um convert
position like
that
size
mini cord X
distance in
it so we can say
closest
mini
qu key
point is equal
to um
self.
drawing
of
player
position is equal
that is
Court player
position
when output
frame
off uh BB
player ID to the
the minimum of
the
player uh BB box do
keys
where
which is if the
closest
ball position
also being
let's call it
um output ball BB
box um so yeah um
both
dictionary and
the output
to uh
convert the
mini cord
detections
core
equal to
come back to
back to
it um
draw uh
points on Mini
qut
um um a default
frame
number and
the
color
and it's
like this
position and we
X and
then
draw
points on Mini
the
balls uh so we can call the ball uh mini
then
uh run
here
yet on the
to
on your output
it's going to
calculating
over each
this
it in uh U measure it um in the in
meters
chot into
to
frame
to be uh
bullshot
time in
covered
by ball in
measure the
distance function
back the
so speed of the
ball
opponent uh
player
positions do
Lambda uh player
to be players
um
going to be
pixels so
ball
opponent in
of it
speed
of
in kilm per
dictionary uh stats uh
player
player one uh
total uh shot
the
calculate it
total
well
we have um like uh
easier to um
import
frame
number
player average
uh shot
going
of
and player
one uh short
the correct
go
um so in the
function
uh another
file
called
player
stats
dra utils.py
the main
draw player
output uh video
player
last shot
add those um
um
statistics
above above it
want to Al to have it
to
shape
of uh 1 minus uh
um which is going to
it and uh
to be the
the rest of
video frame of
again so uh from
dot
main right
it
stats
now we
just run
just want
the
miniart
width of
width
have this
going to be the
be speed not
again
and now it's done so you can go back to
500 not
be a little bit
going to find it to
going
and instead of
distance to meters
and and
vision and
day