File size: 4,243 Bytes
390e994
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# FiveHundredShots visualizer

`visualize.py` replays the clips on the G1 + racket model in the MuJoCo viewer, or renders them
to a video file without a display. `fivehundredshots.py` is the loader it uses; it needs only
numpy and is the place to look for the clip layout.

## Install

```bash
pip install -r scripts/requirements.txt    # numpy, mujoco, imageio[ffmpeg]
```

Python 3.9 or newer. `imageio[ffmpeg]` is only needed for `--record`.

## Quick start

Run from the dataset folder:

```bash
python scripts/visualize.py                            # browse all 500 shots
python scripts/visualize.py --stroke backhand          # only backhands
python scripts/visualize.py --shot shot_0458 shot_0122 # only these shots
python scripts/visualize.py --shot shot_0458 --speed 0.25   # quarter speed
```

On macOS the interactive viewer has to be started with `mjpython` instead of `python`
(a MuJoCo requirement).

## What you see

![forehand](../assets/forehand_strip.jpg)

| Marker | Meaning |
|---|---|
| Yellow sphere | Centre of the racket string bed in the current frame |
| Red sphere | Where that centre is at ball contact (`sweetspot_hit_world`) |
| Green arrow | Direction the racket face points at contact (`hit_direction_world`) |

The yellow sphere reaches the red one exactly at the contact frame. The camera follows the
pelvis and is placed relative to each shot's hit direction, so every stroke is seen from the
same side; the mouse still orbits and zooms. The top-left label shows the shot id, stroke,
current frame and contact frame.

## Keyboard

| Key | Action |
|---|---|
| `SPACE` | Pause / resume |
| `RIGHT` or `N` | Next shot |
| `LEFT` or `B` | Previous shot |
| `R` | Restart the shot |
| `H` | Jump to the contact frame and pause |
| `V` | Show / hide the markers |
| `UP` / `DOWN` | Faster / slower playback |

Each shot loops until you move on. Close the window to quit.

## Record a video

```bash
python scripts/visualize.py --shot shot_0458 shot_0122 --record strokes.mp4
python scripts/visualize.py --shot shot_0458 --record slow.mp4 --speed 0.25
python scripts/visualize.py --shot shot_0458 --record small.gif --every 2 --width 480 --height 360
```

`--record` needs `--shot` and renders the listed shots one after another, with the same
camera and markers as the viewer. Every rendered frame is a frame of the dataset: `--speed`
only changes the frame rate written to the file, and `--every N` keeps every N-th frame.

On a machine without a display, select an offscreen backend first:

```bash
MUJOCO_GL=egl python scripts/visualize.py --shot shot_0458 --record out.mp4
```

## Options

| Option | Default | Meaning |
|---|---|---|
| `--shot ID [ID ...]` | all | Play only these shots |
| `--stroke forehand\|backhand` | both | Play only this stroke type |
| `--speed X` | `1.0` | Playback speed factor |
| `--azimuth DEG` | `135` | Camera yaw relative to the hit direction |
| `--elevation DEG` | `-15` | Camera elevation |
| `--distance M` | `3.2` | Camera distance to the pelvis |
| `--no-markers` | off | Hide the spheres and the arrow |
| `--record FILE` | off | Render to `.mp4` or `.gif` instead of opening a window |
| `--every N` | `1` | When recording, keep every N-th frame |
| `--width`, `--height` | `1280`, `720` | Recording size in pixels |
| `--scene XML` | `robot/scene_tennis.xml` | Another MuJoCo model with the same 29 joint names |
| `--root DIR` | this dataset | Dataset folder |

With `--scene` pointing at a stock G1 model the motion still plays; the yellow marker needs
the `racket_sweetspot` site and is skipped when the model has none.

## Use the loader in your own code

```python
import sys
sys.path.insert(0, "scripts")
from fivehundredshots import JOINT_NAMES, load_clip, load_metadata

shots = load_metadata()                      # list of dicts, one per shot
clip = load_clip(shots[0]["file"])           # named (T, ...) arrays
print(clip.joint_pos.shape, JOINT_NAMES[0])  # (176, 29) left_hip_pitch_joint
```

MuJoCo orders the joints differently from the dataset, so place them by name:

```python
qadr = [model.joint(name).qposadr[0] for name in JOINT_NAMES]
data.qpos[:3], data.qpos[3:7], data.qpos[qadr] = clip.root_pos[t], clip.root_quat[t], clip.joint_pos[t]
```