{ "cells": [ { "cell_type": "markdown", "id": "intro", "metadata": {}, "source": "# CounterStrike-1K — Quickstart\n\nEnd-to-end walkthrough of the public preview dataset. **Open in Colab and run all** — the first cell installs the loader.\n\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/AnirudhhRamesh/CounterStrike-1K/blob/main/cs2_release/quickstart.ipynb)\n\n**What you'll see**: 1) browse the manifest, 2) decode one sample, 3) inspect actions/state, 4) watch the video, 5) **HUD overlay to verify action–video alignment**, 6) load all 10 synchronized POVs of one round.\n\nIf you're running locally instead of Colab, `uv add datasets \"counterstrike1k @ git+https://github.com/AnirudhhRamesh/counterstrike1k\"` once and skip the next cell." }, { "cell_type": "code", "id": "2d22fcd3", "source": "# Colab / fresh-kernel install. Skipped automatically if counterstrike1k is\n# already importable (e.g. local `uv add` users) so the cell is silent.\nimport importlib.util\nif importlib.util.find_spec(\"counterstrike1k\") is None:\n %pip install -q \"counterstrike1k @ git+https://github.com/AnirudhhRamesh/counterstrike1k\" datasets pandas matplotlib pillow av", "metadata": {}, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "id": "browse-md", "metadata": {}, "source": [ "## 1. Browse the manifest\n", "\n", "The manifest is a small Parquet file (~25 MB) listing every released POV sample with split, map, weapon, kill counts, and round/match grouping. Read it with pandas — no media is downloaded here." ] }, { "cell_type": "code", "execution_count": null, "id": "browse-code", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from huggingface_hub import hf_hub_download\n", "\n", "manifest = pd.read_parquet(hf_hub_download(\n", " \"ArnieRamesh/CounterStrike-1K\", \"manifest.parquet\", repo_type=\"dataset\",\n", "))\n", "print(f\"{len(manifest):,} POV samples across all splits\")\n", "manifest.head()[[\"sample_key\", \"split\", \"map_slug\", \"round_id\", \"pov_idx\", \"duration_s\", \"frames\"]]" ] }, { "cell_type": "markdown", "id": "filter-md", "metadata": {}, "source": [ "Boolean masks give you fast, vectorized filtering — much faster than `.filter(lambda)`:" ] }, { "cell_type": "code", "execution_count": null, "id": "filter-code", "metadata": {}, "outputs": [], "source": [ "mirage_train = manifest[(manifest[\"map_slug\"] == \"mirage\") & (manifest[\"split\"] == \"train\")]\n", "print(f\"{len(mirage_train):,} Mirage train clips\")\n", "manifest[\"map_slug\"].value_counts()" ] }, { "cell_type": "markdown", "id": "decode-md", "metadata": {}, "source": [ "## 2. Stream one sample\n", "\n", "We stream from the small preview repo (no full download). `decode_sample` turns one shard sample into numpy arrays plus mp4 bytes." ] }, { "cell_type": "code", "execution_count": null, "id": "decode-code", "metadata": {}, "outputs": [], "source": [ "from counterstrike1k import load_sample\n", "\n", "samples = list(load_sample()) # downloads ~2 GB on first call, then cached.\n", "sample = samples[0]\n", "\n", "print(\"key: \", sample[\"key\"])\n", "print(\"actions:\", sample[\"actions\"].shape, sample[\"actions\"].dtype.names)\n", "print(\"state: \", sample[\"state\"].shape, sample[\"state\"].dtype.names[:6], \"...\")\n", "print(\"events: \", len(sample[\"events\"]), \"events\")\n", "print(\"video: \", len(sample[\"video\"]), \"mp4 bytes\")" ] }, { "cell_type": "markdown", "id": "buttons-md", "metadata": {}, "source": [ "## 3. Inspect actions and state\n", "\n", "Buttons are stored as a `uint16` bitmask. `unpack_buttons` expands them into one boolean array per button (`FORWARD`, `FIRE`, `JUMP`, ...)." ] }, { "cell_type": "code", "execution_count": null, "id": "buttons-code", "metadata": {}, "outputs": [], "source": [ "from counterstrike1k import unpack_buttons\n", "\n", "buttons = unpack_buttons(sample[\"actions\"])\n", "pressed = {name: int(values.sum()) for name, values in buttons.items() if values.any()}\n", "print(\"pressed frames per button:\", pressed)\n", "\n", "state = sample[\"state\"]\n", "print(\"\\nFirst frame:\")\n", "print(f\" pos = ({state['pos_x'][0]:.1f}, {state['pos_y'][0]:.1f}, {state['pos_z'][0]:.1f})\")\n", "print(f\" view = pitch {state['pitch'][0]:.1f}°, yaw {state['yaw'][0]:.1f}°\")\n", "print(f\" health = {state['health'][0]}, armor = {state['armor_value'][0]}\")\n", "print(f\" score = T {state['t_score'][0]} : CT {state['ct_score'][0]}\")" ] }, { "cell_type": "markdown", "id": "video-md", "metadata": {}, "source": [ "## 4. Watch the video\n", "\n", "Write the mp4 bytes to a file and embed the player. Audio plays in browsers that allow it." ] }, { "cell_type": "code", "execution_count": null, "id": "video-code", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "from IPython.display import Video\n", "\n", "out = Path(\"data/quickstart\") / f\"{sample['key']}.mp4\"\n", "out.parent.mkdir(parents=True, exist_ok=True)\n", "out.write_bytes(sample[\"video\"])\n", "Video(str(out), embed=False, html_attributes=\"controls\")" ] }, { "cell_type": "markdown", "id": "overlay-md", "metadata": {}, "source": [ "## 5. Verify alignment with the debug overlay\n", "\n", "When working with action-conditioned video, the first thing you want to check is: *do the labels actually align with the video?* `overlay_frame` draws a HUD with the WASD keys, FIRE/JUMP/DUCK chips, mouse delta, HP/armor/money, and current score onto any frame.\n", "\n", "Pick a frame around something interesting — e.g., the first frame where FIRE is pressed:" ] }, { "cell_type": "code", "execution_count": null, "id": "overlay-code-frame", "metadata": {}, "outputs": [], "source": [ "from counterstrike1k import overlay_frame\n", "\n", "fire_frames = buttons[\"FIRE\"].nonzero()[0]\n", "frame_idx = int(fire_frames[0]) if len(fire_frames) else len(sample[\"actions\"]) // 2\n", "print(f\"showing frame {frame_idx}\")\n", "overlay_frame(sample, frame_idx)" ] }, { "cell_type": "markdown", "id": "overlay-video-md", "metadata": {}, "source": [ "For a moving overlay, `overlay_video` writes a debug mp4. This is what to share with collaborators when you're explaining what's in the dataset." ] }, { "cell_type": "code", "execution_count": null, "id": "overlay-code-video", "metadata": {}, "outputs": [], "source": [ "from counterstrike1k import overlay_video\n", "\n", "debug_path = Path(\"data/quickstart\") / f\"{sample['key']}.debug.mp4\"\n", "overlay_video(sample, debug_path, max_frames=192) # ~6 seconds at 32 FPS\n", "Video(str(debug_path), embed=False, html_attributes=\"controls\")" ] }, { "cell_type": "markdown", "id": "round-md", "metadata": {}, "source": [ "## 6. Load all 10 POVs of one synchronized round\n", "\n", "Every round has exactly 10 synchronized POVs sharing a `round_id`. Group them with the manifest, then pull each one." ] }, { "cell_type": "code", "execution_count": null, "id": "round-code", "metadata": {}, "outputs": [], "source": [ "rows = pd.DataFrame([{**s[\"metadata\"], \"frames_actual\": len(s[\"actions\"])} for s in samples])\n", "round_id = rows[\"round_id\"].iloc[0]\n", "round_samples = rows[rows[\"round_id\"] == round_id].sort_values(\"pov_idx\")\n", "print(f\"round {round_id}: {len(round_samples)} POVs\")\n", "round_samples[[\"sample_key\", \"pov_idx\", \"team_side\", \"frames_actual\", \"alive_duration_s\"]]" ] }, { "cell_type": "markdown", "id": "grid-md", "metadata": {}, "source": [ "## 7. (Optional) Display all 10 POVs as a grid\n", "\n", "Decodes the midpoint frame from each POV and stacks them into a 5×2 grid." ] }, { "cell_type": "code", "execution_count": null, "id": "grid-code", "metadata": {}, "outputs": [], "source": [ "import io\n", "import av\n", "from PIL import Image, ImageDraw\n", "\n", "def midpoint_frame(video_bytes):\n", " with av.open(io.BytesIO(video_bytes)) as container:\n", " frames = list(container.decode(video=0))\n", " return frames[len(frames) // 2].to_image()\n", "\n", "by_pov = {int(s[\"metadata\"][\"pov_idx\"]): s for s in samples if s[\"metadata\"][\"round_id\"] == round_id}\n", "tile_w = 320\n", "tiles = []\n", "for pov in sorted(by_pov):\n", " img = midpoint_frame(by_pov[pov][\"video\"])\n", " th = int(round(tile_w * img.height / img.width))\n", " tiles.append((pov, by_pov[pov][\"metadata\"].get(\"team_side\", \"\"), img.resize((tile_w, th))))\n", "\n", "tw, th = tiles[0][2].size\n", "cols, rows_n = 5, 2\n", "grid = Image.new(\"RGB\", (cols * tw + 4, rows_n * (th + 22) + 4), (18, 18, 18))\n", "draw = ImageDraw.Draw(grid)\n", "for i, (pov, side, img) in enumerate(tiles):\n", " r, c = divmod(i, cols)\n", " x, y = c * tw + 2, r * (th + 22) + 2\n", " draw.text((x + 4, y), f\"pov {pov} {side}\", fill=(245, 245, 245))\n", " grid.paste(img, (x, y + 22))\n", "grid" ] }, { "cell_type": "markdown", "id": "next-md", "metadata": {}, "source": [ "## What's next\n", "\n", "- For training: stream the **360p** or **720p** WebDataset shards. See `examples/torch_dataset.py`.\n", "- For full schema details: open the [`schema/` folder](https://huggingface.co/datasets/ArnieRamesh/CounterStrike-1K/tree/main/schema) on the dataset card.\n", "- For deeper exploration (parquet+offset access, smoke checks): see `examples/advanced/`." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.2" } }, "nbformat": 4, "nbformat_minor": 5 }