-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths5-computer-vision.Rmd
397 lines (255 loc) · 8.41 KB
/
s5-computer-vision.Rmd
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
---
title: "Session 5: Computer vision in R"
author: "Ben Raymond, Adrien Ickowicz"
date: "<div id='logo_footer'></div>"
output:
xaringan::moon_reader:
lib_dir: libs
seal: false
self_contained: false
css: ["extra/extra.css"]
chakra: extra/remark-latest.min.js
nature:
highlightStyle: github
highlightLines: true
highlightSpans: true
mathjax: null
---
layout: true
<div class="my-footer">
<div class="my-footer-box"><a href="https://openvolley.org/"><img style="display:inline;" src="extra/ovoutline-w.png"/>openvolley.org</a></div>
<div class="my-footer-box"><a href="https://https://volleyball.ca/"><img src="extra/vc-w-wide.png"/></a></div>
<div class="my-footer-box"><a href="https://untan.gl/"><img src="extra/su_title-w.png"/></a></div>
</div>
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
options(knitr.kable.NA="")
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE, cache = FALSE, dpi = 120, comment = "", fig.height = 4.5)
library(dplyr)
library(knitr)
library(ggplot2)
library(datavolley)
options(tibble.width = 65, tibble.print_max = 10, tibble.print_min = 10, width = 80)
xaringanExtra::use_clipboard()
```
class: inverse, logo, center
<img src="extra/3logo2.png" style="width:65%; margin-bottom:50px;" />
## Session 5: Computer vision in R
### Ben Raymond, Adrien Ickowicz
##### with valuable contributions from many others...
---
## Session 5 setup
Update your copy of the workshop repo:
If you are in the 'R_Workshop_2022' project, then from the RStudio menu:
`Tools` -> `Version Control` -> `Pull Branches`
Or from the command line in your 'R_Workshop_2022' directory: `git pull`
<br />
Install the ovml package if you don't already have it:
```{r eval = FALSE}
install.packages("ovml",
repos = c("https://openvolley.r-universe.dev",
"https://cloud.r-project.org"))
```
---
## Limitations of scout data
- only the player playing the ball
- doesn't capture all details
- unavoidable subjectivity
- manually intensive
---
## A brief history of computer vision, AI and 'deep learning'
#### AI, deep learning:
1940s–1980s: artificial neural networks, backpropagation
1990s–2000s: use of GPUs, quantities of data
Around 2010: "modern" computer vision approaches
<br />
<hr />
See also: [Peter Norvig - The Unreasonable Effectiveness of Data](https://www.youtube.com/watch?v=yvDCzhbjYWs)
---
## A biased perspective
For a prospective user of these approaches:
- developing new models is hard
- there are models available (with code), many to choose from
- but they likely don't do quite what (or everything) you want
- training networks is fiddly, time consuming, and requires lots of data ... but likely feasible
- beyond trivial usage, most require domain-specific coding (Python)
---
## Openvolley aims
- to make these approaches more accessible
- in R
- not necessarily the most up-to-date or best (from a ML perspective)
- not necessarily the most efficient implementation
- BUT integrated with all the other openvolley tools
---
## The ovml package
```{r}
library(ovml)
dn <- ovml_yolo()
image_file <- ovml_example_image()
res <- ovml_yolo_detect(dn, image_file)
res
```
---
```{r}
ovml_ggplot(image_file, res)
```
---
## The ovml package
Helpers — extracting video frames
```{r}
my_video_file <- ovdata::ovdata_example_video("190301_kats_beds")
my_video_file
library(ovideo)
image_file <- ov_video_frame(my_video_file, t = 3.2)
image_file
```
---
## The ovml package
Helpers — converting to court coordinates
Also in the ovideo package:
- `ov_shiny_court_ref` to define the transformation function
- `ov_transform_points` to transform from image coordinates to court coordinates and vice-versa
---
## The ovml package
Helpers — converting to court coordinates
```{r eval = FALSE}
ref <- ov_shiny_court_ref(image_file)
ref$court_ref
```
```{r echo = FALSE}
crt <- data.frame(image_x = c(0.05397063, 0.95402573, 0.75039756, 0.28921230),
image_y = c(0.02129301, 0.02294600, 0.52049712, 0.51884413),
court_x = c(0.5, 3.5, 3.5, 0.5),
court_y = c(0.5, 0.5, 6.5, 6.5))
ref <- list(court_ref = crt)
ref$court_ref
```
---
## The ovml package
Helpers — putting those together
```{r}
res <- ovml_yolo_detect(dn, image_file)
head(res)
```
---
## The ovml package
Helpers — putting those together
```{r}
library(magick)
image_meta <- image_info(image_read(image_file))
res <- res %>% mutate(x = (xmin + xmax) / 2 / image_meta$width,
y = ymin / image_meta$height)
head(res)
```
---
## The ovml package
```{r}
court_xy <- ov_transform_points(res[, c("x", "y")],
ref = ref$court_ref,
direction = "to_court")
res <- bind_cols(res, setNames(court_xy, c("court_x", "court_y")))
head(res)
```
---
## The ovml package
Helpers — putting those together
```{r eval = FALSE}
ggplot(res %>% filter(court_y < 7 & court_x < 4),
aes(x = court_x, y = court_y)) +
ggcourt(labels = NULL) +
geom_point()
```
```{r echo = FALSE, fig.width = 6, fig.height = 3}
library(patchwork)
p1 <- ovml_ggplot(image_file, res, label_geom = NULL)
p2 <- ggplot(res %>% filter(court_y < 7 & court_x < 4),
aes(x = court_x, y = court_y)) +
ggcourt(labels = NULL) +
geom_point(color = "blue")
p2 + p1 + plot_layout(ncol = 2)
```
---
## ovml — Application ideas
#### Court positioning
Recall from yesterday, the volleydef app output:
![](extra/defensive_zones.png)
- uses one defender position per dug attack
- what can we get via ovml?
---
## ovml — Court positioning
```{r}
library(datavolley)
library(ovdata)
dv <- ovdata_example("190301_kats_beds-clip", as = "parsed")
vt <- plays(dv) %>%
filter(attack_code %in% c("X6", "V6") & team == "MKS Będzin") %>%
pull(video_time)
image_files <- ov_video_frame(my_video_file, t = vt)
res <- ovml_yolo_detect(dn, image_files)
res <- res %>% mutate(x = (xmin + xmax) / 2 / image_meta$width,
y = ymin / image_meta$height)
court_xy <- ov_transform_points(res[, c("x", "y")],
ref = ref$court_ref, direction = "to_court")
res <- bind_cols(res, setNames(court_xy, c("court_x", "court_y")))
res <- res %>% filter(class == "person" & court_y >= 3.5 &
court_y < 7 & court_x > 0 & court_x < 4)
```
---
## ovml — Court positioning
```{r fig.height = 3.5}
ggplot(res, aes(x = court_x, y = court_y)) +
ggcourt(labels = NULL, court = "upper") +
geom_point(color = "blue")
```
---
## ovml — Court positioning
Positions vs left-side attacks:
<img src="extra/x5-v5-def.png" style="max-height:45vh;" />
---
## ovml — Court positioning (beach)
Images courtesy Tyler Widdison
<img src="extra/tw-image1.png" style="float:left; clear:none; width:60%;" />
<img src="extra/tw-image2.png" style="float:right; clear:none; max-height:50vh;" />
---
## Next steps
- tracking of players, and the ball
- identify individuals, follow their movements
- fill in gaps (occlusions, missed detections)
- some unique challenges
---
## Next steps — tracking
<img src="extra/players-tracked.png" style="max-height:55vh;" />
---
## Next steps — tracking
<video src="extra/kats3_tracked.mp4" controls style="margin-top:-20vh;"/>
---
## Next steps — height
<img src="`r ovdata::ovdata_example_image(choice = "190301_kats_beds")`" style="max-height:45vh;" />
- multiple cameras?
---
class: logo
<img src="extra/leal_two_views.png" />
---
## Next steps — two camera example
<img src="extra/leal_apparent_positions.png" />
---
## Next steps — two camera example
<img src="extra/leal_resolved_positions.png" style="max-height:100vh; margin-top:-20px;" />
Estimated height: 58cm
---
## Next steps — two camera example
<video id="vid2c" src="extra/demo_ball_tracking.mp4" controls onplay="var vid = document.getElementById('vid2c'); vid.playbackRate = 0.5; "/>
---
## Next steps — two camera example
Estimated contact heights:
- serve height (Isac, Brazil #12) is 3.49m (top of the ball)
- back-row spike by Alan (Brazil #21) at 3.43m
(See https://untan.gl/multicamera-volleyball-tracking.html)
---
class: center, middle
## Enough with the machine learning already
---
## openvolley
- GitHub issues
- analytics snippets: https://openvolley.github.io/volley-analytics-snippets