dsh-watch-together 0.2.10 → 0.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +110 -0
- package/README.md +15 -5
- package/docs/INTERFACES.zh.md +29 -12
- package/dsh.plugin.json +12 -12
- package/examples/floating-chat/glass_capture.py +286 -0
- package/examples/floating-chat/glass_cpu.py +174 -0
- package/examples/floating-chat/glass_geometry.py +288 -0
- package/examples/floating-chat/glass_gpu.py +509 -0
- package/examples/floating-chat/glass_window.py +4 -7
- package/examples/floating-chat/glass_window_gpu.py +14 -516
- package/examples/floating-chat/requirements-glass.txt +5 -0
- package/examples/floating-chat/run-glass.cmd +5 -1
- package/examples/floating-chat/window.py +1210 -1932
- package/examples/floating-chat/window_widgets.py +288 -0
- package/package.json +115 -115
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,116 @@
|
|
|
3
3
|
All notable changes to **dsh-watch-together** are documented here.
|
|
4
4
|
This package starts its own version line at `0.0.7` — it is not a continuation of `dsh-video-storyboard`.
|
|
5
5
|
|
|
6
|
+
## [0.2.11] - 2026-11-03
|
|
7
|
+
|
|
8
|
+
> 悬浮窗从头拆了一遍(单文件 1931 行 → 五层),顺带修掉 GPU 路径三个真 bug。
|
|
9
|
+
|
|
10
|
+
### Changed — 玻璃拆成四层,去掉全部模块级可变全局
|
|
11
|
+
|
|
12
|
+
`window.py` 原来 1931 行,`W / H / SHAPES / MASK / MASK_HARD / OVERLAY /
|
|
13
|
+
OVERLAY_2X / WARP_X / WARP_Y / BOX_MAPS / HW / HH / BOX_UNION_2X / MIC_POS`
|
|
14
|
+
十几个模块级全局靠 `global` 在 `rebuild_geometry()` 里一次性改写;GPU 伴生
|
|
15
|
+
文件还得 `import glass_window as G` 去读它们,窗口一缩放就可能拿到上一帧的坐标。
|
|
16
|
+
|
|
17
|
+
| 文件 | 职责 |
|
|
18
|
+
|---|---|
|
|
19
|
+
| `glass_geometry.py` | 一套尺寸的全部贴图。`Geometry` 是不可变值对象,气泡变宽用 `with_shapes()` 换新对象,尺寸相关的遮罩/折射图直接复用 |
|
|
20
|
+
| `glass_capture.py` | 抓屏、`MotionTracker`、麦克风贴图、圆角收尾 —— CPU/GPU 共用一份 |
|
|
21
|
+
| `glass_cpu.py` | CPU 合成管线 |
|
|
22
|
+
| `glass_gpu.py` | 同一件事在 moderngl 上做 |
|
|
23
|
+
| `window.py` | 编排 + Tk 窗口 + 交互 |
|
|
24
|
+
| `window_widgets.py` | `Fonts` / `ChatFeed` / `TextLayer` |
|
|
25
|
+
|
|
26
|
+
顺手清掉的死代码和重复逻辑:
|
|
27
|
+
|
|
28
|
+
- `_add_text_shadows()` 是空实现,`_text_shadows` 有 11 处引用但永远是空的;
|
|
29
|
+
- `_hide_window()` 定义了但没有任何调用点;`EDGE_MASK` 算出来没人用;
|
|
30
|
+
- `_fit_lines` / `_text` 对每个字重新 `measure()` 一遍前缀 —— O(n²) 次 Tcl 调用,
|
|
31
|
+
改成累积宽度;
|
|
32
|
+
- 每来一条消息就 `Entry.destroy()` 重建(丢输入法状态和光标),改成复用一个;
|
|
33
|
+
- `_preview_resize` / `_rebuild` / `_refresh_feed` 三份几乎相同的「清状态 + 重排 +
|
|
34
|
+
贴图」;
|
|
35
|
+
- `tkfont.Font(...)` 每帧新建几百个 Tcl 对象 → `Fonts` 按尺寸缓存。
|
|
36
|
+
|
|
37
|
+
### Fixed — 录屏模式下桌面图标被沿上下方向拉成竖条
|
|
38
|
+
|
|
39
|
+
`capturable: true` 时 mss 会抓到窗口自己,所以要把那块刷平。`fill_own_rect()`
|
|
40
|
+
当时的做法是「取窗口上下各一行像素做纵向插值」:窗口上方紧挨着的那一行里如果
|
|
41
|
+
正好有桌面图标或文字,**每个像素会被整列往下抹开** —— 用户看到的就是「东西被
|
|
42
|
+
沿上下方向拉成长条」。0.2.7 的 CHANGELOG 写的是「周边均值」,代码里却是纵向
|
|
43
|
+
插值,那次改动把方案退回去了。
|
|
44
|
+
|
|
45
|
+
现在改成四条边各自分段抹平高频 + Coons patch 融合:边界贴合真实背景,内部只有
|
|
46
|
+
大尺度色彩过渡,没有方向性条纹。填充区的列内/行内标准差从 **28.0 / 35.9**(有
|
|
47
|
+
方向性)变成 **12.7 / 7.1**(各向同性)。
|
|
48
|
+
|
|
49
|
+
算法本身要降到 1/8 粗网格再放大 —— 全分辨率算一次 **80 ms**,粗网格 **4.2 ms**,
|
|
50
|
+
而这块最后还要被高斯糊一遍,8px 的色阶看不出来。
|
|
51
|
+
|
|
52
|
+
同一个问题 GPU 侧本来没有(它压根没调 `fill_own_rect`),0.2.11 给 GPU 补上这
|
|
53
|
+
一步之后两边一致。
|
|
54
|
+
|
|
55
|
+
### Fixed — GPU 版的 OpenGL context 建在错误的线程上(每帧 `cannot create texture`)
|
|
56
|
+
|
|
57
|
+
`GpuRenderer` 原来在主线程构造。`moderngl.create_standalone_context()` 绑在创建
|
|
58
|
+
它的线程上,而 `render()` 跑在抓屏 worker 线程 —— 每帧抛 `cannot create texture`,
|
|
59
|
+
被 `_work()` 吞掉后只打印一行、窗口始终没有玻璃(截图里只剩文字浮在桌面上)。
|
|
60
|
+
|
|
61
|
+
改成首次 `render()` 时惰性创建(那时已经在 worker 线程);真起不来就打印原因并
|
|
62
|
+
回落 CPU,不刷屏。
|
|
63
|
+
|
|
64
|
+
### Fixed — GPU 版在录屏模式下没挖掉自己(`moved` 恒真、永不跳帧)
|
|
65
|
+
|
|
66
|
+
`CaptureSource.to_image()` 会 `fill_own_rect()`,但 GPU 直接吃 `shot.bgra`、
|
|
67
|
+
绕过了那一步 —— 背景里永远带着本窗口,`moved` 每帧为真:静态跳帧一次都不触发,
|
|
68
|
+
还叠出自我反馈的拖尾(0.2.7 修过同一个坑,只修了 CPU 侧)。补了数组版
|
|
69
|
+
`fill_own_rect_array()`。
|
|
70
|
+
|
|
71
|
+
实测 700×665 / 150% DPI / 同一桌面、同一条消息:
|
|
72
|
+
|
|
73
|
+
| | CPU 版 | GPU 版(修前) | GPU 版(修后) |
|
|
74
|
+
|---|---|---|---|
|
|
75
|
+
| CPU | 43.2% 单核 | 87.9% | **32.1%** |
|
|
76
|
+
| 内存 | 93 MB | 195 MB | 193 MB |
|
|
77
|
+
|
|
78
|
+
### Fixed — `DSH_GLASS_NATIVE=1` 一直是个假开关
|
|
79
|
+
|
|
80
|
+
`run-glass.cmd` 写着「走 GPU 路径」,但 `window.py` 从来没 import 过
|
|
81
|
+
`glass_window_gpu` —— 设了只是让 CPU 路径去读一个没人用的变量。现在
|
|
82
|
+
`DSH_GLASS_GPU=1` / `DSH_GLASS_NATIVE=1` 真的选后端;`glass_window_gpu.py`
|
|
83
|
+
仍可单独起窗(等价于 `DSH_GLASS_GPU=1`)。
|
|
84
|
+
|
|
85
|
+
### Fixed — GPU 版拖动窗口没有运动模糊、反而做静止才该做的色散
|
|
86
|
+
|
|
87
|
+
0.2.9 修的 `box_changed → moved` 只落在 CPU 路径上,GPU 那份还是老判断。现在
|
|
88
|
+
两条路径共用 `MotionTracker`,同一个判断只有一处实现。
|
|
89
|
+
|
|
90
|
+
### Fixed — GPU 版形状变化后混入上一帧残影
|
|
91
|
+
|
|
92
|
+
`_invalidate_glass()` 只清了 CPU 的 `_prev_out`,GPU 的 `_prev_valid` 还留着。
|
|
93
|
+
现在几何一变统一走 `renderer.invalidate()`,两边一起清。
|
|
94
|
+
|
|
95
|
+
### Fixed — 折叠时 `slabs_for()` 算出负高度矩形
|
|
96
|
+
|
|
97
|
+
折叠后窗口只有 78px,`bot = (o, h-o-70, ...)` 的 y0 大于 y1,PIL 直接抛
|
|
98
|
+
`y1 must be greater than or equal to y0` —— 点一下 ▾ 就 traceback。高度不够时
|
|
99
|
+
只返回标题栏那一块。
|
|
100
|
+
|
|
101
|
+
### Performance
|
|
102
|
+
|
|
103
|
+
- 高斯从 PIL `ImageFilter.GaussianBlur` 换成 `cv2.GaussianBlur`(有 OpenCV 时);
|
|
104
|
+
- GPU 首次渲染 **1137 ms → 79 ms**(几何贴图改用 cv2 缩放,不再 PIL 逐通道);
|
|
105
|
+
- 静止时的探针从 60fps 降到 30fps —— 单次 `grab()` 就要 7ms,空转等于白烧
|
|
106
|
+
40% 单核;有变化时 `idle` 立刻归零、下一拍恢复全速;
|
|
107
|
+
- 文字翻色从「每个字 `crop()` + `np.percentile`」改成整帧转一次灰度再切片。
|
|
108
|
+
|
|
109
|
+
### 验证
|
|
110
|
+
|
|
111
|
+
- 视觉:同一背景同一消息,新旧玻璃帧逐像素对比 **均差 0.068、99% 像素差 ≤1**;
|
|
112
|
+
- 交互:折叠 / 展开 / 拖动 / 边缘缩放 / 滚轮 / 改名 / 发送 / 新消息增量 / 关闭
|
|
113
|
+
全部实跑通过(810×625 与 760×560、折叠 78px 三种尺寸);
|
|
114
|
+
- `node scripts/check-syntax.mjs` 56 files ok;`node --test` **317 pass / 0 fail**。
|
|
115
|
+
|
|
6
116
|
## [0.2.10] - 2026-11-03
|
|
7
117
|
|
|
8
118
|
> GPU 路径其实一直是坏的,顺便量了一下它到底省不省。
|
package/README.md
CHANGED
|
@@ -434,9 +434,17 @@ pythonw examples/floating-chat/window.py # 无控制台
|
|
|
434
434
|
|
|
435
435
|
它长这样:三块分开的液态玻璃(标题 / 主体 / 输入栏),气泡圆角、半透明,
|
|
436
436
|
你说的话靠右;标题栏可拖动、双击标题改名、▾ 折叠、✕ 关闭,底部有麦克风图标和发送。
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
437
|
+
玻璃分成四层,各自不知道对方存在:
|
|
438
|
+
|
|
439
|
+
| 文件 | 职责 |
|
|
440
|
+
|---|---|
|
|
441
|
+
| `glass_geometry.py` | 一套尺寸的全部贴图(遮罩 / 折射坐标 / 内框透镜)。不可变值对象,气泡变宽就换一个新对象,尺寸相关的图直接复用 |
|
|
442
|
+
| `glass_capture.py` | 抓屏、静态跳帧与运动强度判定、麦克风贴图、圆角收尾 —— **CPU/GPU 共用一份** |
|
|
443
|
+
| `glass_cpu.py` | CPU 合成:缩小 → 高斯 → 边缘折射 → 内框透镜 → 染色 → 色散 |
|
|
444
|
+
| `glass_gpu.py` | 同一件事在 moderngl 上做(`DSH_GLASS_GPU=1` 或 `DSH_GLASS_NATIVE=1`) |
|
|
445
|
+
| `window.py` | Tk 窗口、聊天流、拖动/缩放/折叠/改名 |
|
|
446
|
+
|
|
447
|
+
CPU/GPU 输出是逐像素对齐的(同一背景均差 0.07,99% 像素差 ≤1),所以换后端看不出区别。
|
|
440
448
|
|
|
441
449
|
几个有意为之的细节:
|
|
442
450
|
|
|
@@ -449,9 +457,11 @@ pythonw examples/floating-chat/window.py # 无控制台
|
|
|
449
457
|
| 增量读 `chat.jsonl`(开场只回放最后 60 条) | 以前每 200ms 全量解析,两小时一场几千条会越来越慢 |
|
|
450
458
|
| 圆角外的像素用 `-transparentcolor` 抠掉 | tkinter 没有圆角窗口;取不到这个能力就老实退成直角 |
|
|
451
459
|
| 窗口只读 `window-events.jsonl`,写 `window-outbox.jsonl` | 和插件解耦;旧版插件没有协议文件时仍增量读 `chat.jsonl` |
|
|
452
|
-
| CPU/GPU 共用同一份 UI | GPU
|
|
460
|
+
| CPU/GPU 共用同一份 UI 和同一套抓屏/运动判定 | GPU 只换每帧合成,不另写窗口;历史上两边各写一份,同一个 bug 修两遍(#39、#48) |
|
|
453
461
|
|
|
454
|
-
> 窗口默认要 Pillow + numpy + mss;GPU 额外要 moderngl
|
|
462
|
+
> 窗口默认要 Pillow + numpy + mss;GPU 额外要 `pip install moderngl` 和 OpenGL 3.3。
|
|
463
|
+
> CPU 在这台机器上更省(约 43% 单核 / 93 MB,GPU 32% / 195 MB —— 显存纹理和每帧
|
|
464
|
+
> 读回要花内存),默认走 CPU;窗口很大或 CPU 较弱时再切 GPU。接口和
|
|
455
465
|
> 线协议见 [`docs/INTERFACES.zh.md`](docs/INTERFACES.zh.md)。
|
|
456
466
|
|
|
457
467
|
> **一定要用带 tkinter 的 Python**:Windows 商店版自带;python.org 官方安装包勾上 tcl/tk 也有。
|
package/docs/INTERFACES.zh.md
CHANGED
|
@@ -68,18 +68,35 @@ Node 侧:`src/window-protocol.js`(`appendWindowEvent` /
|
|
|
68
68
|
## 3. 悬浮窗样式
|
|
69
69
|
|
|
70
70
|
`examples/floating-chat/window.py` 是交互外壳(位置/拖动/缩放、麦克风、
|
|
71
|
-
改名、折叠、入场动画、气泡、meta
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
|
77
|
-
|
|
78
|
-
| `
|
|
79
|
-
| `
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
71
|
+
改名、折叠、入场动画、气泡、meta、发送)。玻璃由独立模块渲染,各层不知道
|
|
72
|
+
对方存在:
|
|
73
|
+
|
|
74
|
+
| 文件 | 职责 |
|
|
75
|
+
|---|---|
|
|
76
|
+
| `glass_geometry.py` | 一套尺寸的全部贴图:玻璃遮罩、边缘折射坐标、内框透镜、染色层。`Geometry` 是不可变值对象;气泡变宽用 `with_shapes()` 换一个新对象,尺寸相关的图直接复用 |
|
|
77
|
+
| `glass_capture.py` | 抓屏(外扩 PAD、零尺寸守卫、录屏模式挖掉自己)、`MotionTracker`(静态跳帧 + 运动强度,**拖动窗口也算动**)、麦克风图标、圆角收尾 |
|
|
78
|
+
| `glass_cpu.py` | CPU 合成:1/4 缩小 → 高斯 → 边缘折射 → 内框透镜 → 2x 合成 → 染色 → 色散 → 残影 |
|
|
79
|
+
| `glass_gpu.py` | 同一件事在 moderngl 上做;`native=True` 时走全分辨率采样 |
|
|
80
|
+
| `window.py` | 上面的编排 + Tk 窗口、聊天流、交互 |
|
|
81
|
+
| `window_widgets.py` | `Fonts`(字体缓存)、`ChatFeed`(chat.jsonl 增量视图)、`TextLayer`(逐字翻色) |
|
|
82
|
+
|
|
83
|
+
两条运行路径输出逐像素一致(同一背景均差 0.07、99% 像素差 ≤1)。切后端:
|
|
84
|
+
|
|
85
|
+
| 开关 | 效果 |
|
|
86
|
+
|---|---|
|
|
87
|
+
| 默认 | CPU |
|
|
88
|
+
| `DSH_GLASS_GPU=1` | GPU(1/4 采样) |
|
|
89
|
+
| `DSH_GLASS_NATIVE=1` | GPU + 全分辨率采样(边缘透镜没有 1/4 放大的像素感) |
|
|
90
|
+
| `python glass_window_gpu.py` | 等价于 `DSH_GLASS_GPU=1` 起窗 |
|
|
91
|
+
|
|
92
|
+
GPU 需要 `moderngl` 和 OpenGL 3.3;起不来时会打印一行原因并回落到 CPU,
|
|
93
|
+
窗口照开。`DSH_GLASS_WDA=0` 可关闭抓屏排除,方便录屏/截图;
|
|
94
|
+
`DSH_GLASS_PREVIEW=1` 是纯预览模式;`GLASS_FORCE=1` 每帧强制重合成(调试很费 CPU)。
|
|
95
|
+
|
|
96
|
+
> 拆分前这里是单文件 1931 行,`W/H/SHAPES/MASK/OVERLAY/WARP_X/BOX_MAPS` 十几个
|
|
97
|
+
> 模块级全局靠 `global` 在 `rebuild_geometry()` 里一起改写,GPU 还得
|
|
98
|
+
> `import glass_window as G` 去读它们 —— 窗口一缩放就可能拿到上一帧的坐标。
|
|
99
|
+
> 现在全部收进 `Geometry`,谁要什么就传进去。
|
|
83
100
|
|
|
84
101
|
## 4. 兼容性
|
|
85
102
|
|
package/dsh.plugin.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "dsh-watch-together",
|
|
3
|
-
"description": "Watch a video together with a text-only agent: follow the user's real playback position and hand over one segment at a time, just before the segment ends.",
|
|
4
|
-
"version": "0.2.
|
|
5
|
-
"entry": {
|
|
6
|
-
"name": "dsh-watch-together",
|
|
7
|
-
"inject": [
|
|
8
|
-
"tools",
|
|
9
|
-
"attachments"
|
|
10
|
-
]
|
|
11
|
-
}
|
|
12
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "dsh-watch-together",
|
|
3
|
+
"description": "Watch a video together with a text-only agent: follow the user's real playback position and hand over one segment at a time, just before the segment ends.",
|
|
4
|
+
"version": "0.2.11",
|
|
5
|
+
"entry": {
|
|
6
|
+
"name": "dsh-watch-together",
|
|
7
|
+
"inject": [
|
|
8
|
+
"tools",
|
|
9
|
+
"attachments"
|
|
10
|
+
]
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
"""glass_capture.py — 抓屏、运动判定、所有渲染路径共用的收尾。
|
|
2
|
+
|
|
3
|
+
CPU 与 GPU 两条管线以前各写一份「抓屏 → 算静态签名 → 判断要不要重合成 →
|
|
4
|
+
贴麦克风 → 贴圆角遮罩」,于是同一个 bug 要修两遍(#39、#48 都是这么来的:
|
|
5
|
+
CPU 修了,GPU 漏了)。这里把它们收成一套:
|
|
6
|
+
|
|
7
|
+
- `CaptureSource.grab()`:窗口矩形外扩 PAD、钳位、守卫零尺寸;
|
|
8
|
+
- `MotionTracker.observe()`:返回 `(moved, stale)`,**两个判断一次算完**,
|
|
9
|
+
不会再出现「拖动窗口算不算动」这种两边理解不一致;
|
|
10
|
+
- `finish()` / `paste_mic()`:两条路径的最后两步。
|
|
11
|
+
|
|
12
|
+
渲染器只负责「一张图 → 一张图」,其余都在这里。
|
|
13
|
+
"""
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import os
|
|
17
|
+
import time
|
|
18
|
+
from dataclasses import dataclass
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
import numpy as np
|
|
22
|
+
from PIL import Image
|
|
23
|
+
|
|
24
|
+
from glass_geometry import KEY, LPF, MIC_SIZE, PAD, Geometry
|
|
25
|
+
|
|
26
|
+
try:
|
|
27
|
+
import cv2 as _cv2
|
|
28
|
+
except Exception: # OpenCV 只是加速,缺了仍走 PIL
|
|
29
|
+
_cv2 = None
|
|
30
|
+
|
|
31
|
+
HERE = Path(__file__).resolve().parent
|
|
32
|
+
|
|
33
|
+
# 静态跳帧:像素差超过这个就算「动了」
|
|
34
|
+
SIG_STEP = 4 # 采样步长(像素)
|
|
35
|
+
MOVE_MEAN = 0.01 # 平均差阈值(0~255)
|
|
36
|
+
MOVE_MAX = 0.5 # 单点最大差阈值
|
|
37
|
+
STALE_AFTER = 0.30 # 运动结束后多久允许跳帧
|
|
38
|
+
|
|
39
|
+
FORCE = os.environ.get("GLASS_FORCE") == "1"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _load_mic() -> tuple[Image.Image, Image.Image]:
|
|
43
|
+
"""麦克风图标:去背景得到 alpha,再做浅色/深色两版(按背后亮度挑)。"""
|
|
44
|
+
try:
|
|
45
|
+
mask = Image.open(HERE / "mic-dsh.png").convert("L")
|
|
46
|
+
mask = mask.resize((MIC_SIZE, MIC_SIZE), Image.Resampling.LANCZOS)
|
|
47
|
+
except Exception:
|
|
48
|
+
mask = Image.new("L", (MIC_SIZE, MIC_SIZE), 200)
|
|
49
|
+
light = Image.new("RGBA", mask.size, (235, 244, 255, 0))
|
|
50
|
+
light.putalpha(mask)
|
|
51
|
+
dark = Image.new("RGBA", mask.size, (24, 48, 72, 0))
|
|
52
|
+
dark.putalpha(mask)
|
|
53
|
+
return light, dark
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
MIC_LIGHT, MIC_DARK = _load_mic()
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def resample(arr_or_im, size):
|
|
60
|
+
"""缩放一张图(两个方向都用);有 OpenCV 走 OpenCV,比 PIL 快一个数量级。"""
|
|
61
|
+
if _cv2 is not None:
|
|
62
|
+
src = np.asarray(arr_or_im)
|
|
63
|
+
return _cv2.resize(src, size, interpolation=_cv2.INTER_LINEAR)
|
|
64
|
+
im = arr_or_im if isinstance(arr_or_im, Image.Image) else Image.fromarray(arr_or_im)
|
|
65
|
+
return np.asarray(im.resize(size, Image.Resampling.BILINEAR))
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
CORNER_PROBE = 56 # 角块边长:从这个方块里取中位色
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _median_color(arr: np.ndarray, y0: int, y1: int, x0: int, x1: int):
|
|
72
|
+
"""一块区域的三通道中位数(没有像素返回 None)。
|
|
73
|
+
|
|
74
|
+
用中位数而不是均值:桌面图标、文字这类「少数像素」不会把整块颜色带跑,
|
|
75
|
+
均值会被一个白色图标顶到发白。
|
|
76
|
+
"""
|
|
77
|
+
h, w = arr.shape[:2]
|
|
78
|
+
y0, y1 = max(0, min(h, y0)), max(0, min(h, y1))
|
|
79
|
+
x0, x1 = max(0, min(w, x0)), max(0, min(w, x1))
|
|
80
|
+
patch = arr[y0:y1, x0:x1]
|
|
81
|
+
if patch.size == 0:
|
|
82
|
+
return None
|
|
83
|
+
return np.median(patch.reshape(-1, patch.shape[-1]), axis=0).astype(np.float32)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _fill_patch(arr: np.ndarray, box, step: int = 8) -> None:
|
|
87
|
+
"""把窗口矩形填成「四周环境色」的二维渐变(原地)。
|
|
88
|
+
|
|
89
|
+
**不要拿边缘像素插值**。窗口上下左右紧挨着的那一圈里如果正好有桌面图标或
|
|
90
|
+
文字,插值会把它们沿某个方向整条抹开 —— 纵插值是竖条、横插值是横条,换哪种
|
|
91
|
+
都难看(用户两次都撞上了)。
|
|
92
|
+
|
|
93
|
+
现在只取窗口外**四个角块的中位色**,做双线性。角块完全在窗口外面,不含窗口
|
|
94
|
+
自己的内容;中位数又抹掉了图标这类少数像素;四角双线性只有大尺度过渡,
|
|
95
|
+
不存在任何「把东西拉长」的方向。
|
|
96
|
+
|
|
97
|
+
渐变在 `step` 分之一的粗网格上算再放大:这块最后还要被高斯糊一遍,
|
|
98
|
+
8px 的色阶看不出来。全分辨率算一次要 80ms,粗网格是 0.3ms。
|
|
99
|
+
"""
|
|
100
|
+
h, w = arr.shape[:2]
|
|
101
|
+
x0, y0, x1, y1 = (int(v) for v in box)
|
|
102
|
+
x0, x1 = max(0, min(w, x0)), max(0, min(w, x1))
|
|
103
|
+
y0, y1 = max(0, min(h, y0)), max(0, min(h, y1))
|
|
104
|
+
bw, bh = x1 - x0, y1 - y0
|
|
105
|
+
if bw <= 0 or bh <= 0:
|
|
106
|
+
return
|
|
107
|
+
|
|
108
|
+
b = CORNER_PROBE
|
|
109
|
+
tl = _median_color(arr, y0 - b, y0, x0 - b, x0)
|
|
110
|
+
tr = _median_color(arr, y0 - b, y0, x1, x1 + b)
|
|
111
|
+
bl = _median_color(arr, y1, y1 + b, x0 - b, x0)
|
|
112
|
+
br = _median_color(arr, y1, y1 + b, x1, x1 + b)
|
|
113
|
+
# 窗口贴在屏幕边上时角块会缺 —— 用整圈外框的中位色兜底;
|
|
114
|
+
# 连外框都没有(窗口铺满屏幕)才退回窗口自己的中位色。
|
|
115
|
+
ring = _median_color(arr, y0 - b, y1 + b, x0 - b, x0) if x0 > 0 else None
|
|
116
|
+
if ring is None:
|
|
117
|
+
ring = _median_color(arr, y0 - b, y1 + b, x1, x1 + b) if x1 < w else None
|
|
118
|
+
if ring is None:
|
|
119
|
+
ring = _median_color(arr, y0 - b, y0, x0, x1) if y0 > 0 else None
|
|
120
|
+
if ring is None:
|
|
121
|
+
ring = _median_color(arr, y1, y1 + b, x0, x1) if y1 < h else None
|
|
122
|
+
if ring is None:
|
|
123
|
+
ring = _median_color(arr, y0, y1, x0, x1) or np.zeros(3, np.float32)
|
|
124
|
+
tl = ring if tl is None else tl
|
|
125
|
+
tr = ring if tr is None else tr
|
|
126
|
+
bl = ring if bl is None else bl
|
|
127
|
+
br = ring if br is None else br
|
|
128
|
+
|
|
129
|
+
gw, gh = max(2, bw // step), max(2, bh // step)
|
|
130
|
+
ty = ((np.arange(gh, dtype=np.float32) + 0.5) / gh)[:, None, None]
|
|
131
|
+
tx = ((np.arange(gw, dtype=np.float32) + 0.5) / gw)[None, :, None]
|
|
132
|
+
out = (tl * (1 - tx) * (1 - ty) + tr * tx * (1 - ty)
|
|
133
|
+
+ bl * (1 - tx) * ty + br * tx * ty)
|
|
134
|
+
out = np.clip(out, 0, 255)
|
|
135
|
+
big = resample(out, (bw, bh)) if (gw, gh) != (bw, bh) else out
|
|
136
|
+
arr[y0:y1, x0:x1] = big.astype(np.uint8)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def fill_own_rect(im: Image.Image, box) -> Image.Image:
|
|
140
|
+
"""录屏模式下把「窗口自己」在抓屏帧里占的那块填掉。
|
|
141
|
+
|
|
142
|
+
`capturable: true` 会关掉 WDA,于是 mss 抓背景时把窗口也抓进来,玻璃拿它
|
|
143
|
+
当背景就是「透过自己看自己」:背景里带着上一帧的字 → 亮度判据一直抖 →
|
|
144
|
+
字色来回翻;背景一直在变 → `moved` 恒真 → 越滚越浓的假拖尾。
|
|
145
|
+
|
|
146
|
+
窗口外 PAD 一圈仍是真背景,边缘透镜照常取料。
|
|
147
|
+
"""
|
|
148
|
+
arr = np.array(im)
|
|
149
|
+
_fill_patch(arr, box)
|
|
150
|
+
return Image.fromarray(arr)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def fill_own_rect_array(arr: np.ndarray, box) -> None:
|
|
154
|
+
"""`fill_own_rect` 的原地版本,给直接吃 `shot.bgra` 的 GPU 路径用。
|
|
155
|
+
|
|
156
|
+
GPU 不经过 PIL,如果漏了这一步,抓到的背景里永远有自己 —— 每帧都在变,
|
|
157
|
+
静态跳帧一次都不会触发,还叠出自我反馈的拖尾。
|
|
158
|
+
"""
|
|
159
|
+
_fill_patch(arr, box)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
class MotionTracker:
|
|
163
|
+
"""静态跳帧 + 运动强度,两条渲染路径共用。
|
|
164
|
+
|
|
165
|
+
语义(和 0.2.9 修过的 CPU 行为一致):
|
|
166
|
+
|
|
167
|
+
- **抓取区域变了就算动**。拖动窗口时 `box` 每帧都不同,旧区域的像素本来
|
|
168
|
+
也不能直接做差;不标 `moved` 的话,拖动全程都走「没在动」分支 ——
|
|
169
|
+
运动模糊一路衰减到 0、反而每帧去做静止才该做的色散。这就是 #48。
|
|
170
|
+
- 静止超过 `STALE_AFTER` 就允许跳帧(`stale=True`),省掉整条合成;
|
|
171
|
+
- `motion` 是 0~1 的平滑值,只有移动帧才拿它去混上一帧。
|
|
172
|
+
"""
|
|
173
|
+
|
|
174
|
+
def __init__(self, force: bool = FORCE):
|
|
175
|
+
self.force = force
|
|
176
|
+
self.motion = 0.0
|
|
177
|
+
self._sig: np.ndarray | None = None
|
|
178
|
+
self._bbox = None
|
|
179
|
+
self._last_motion = 0.0
|
|
180
|
+
|
|
181
|
+
def invalidate(self) -> None:
|
|
182
|
+
"""形状/尺寸变了:旧签名和旧背景都不能再用。"""
|
|
183
|
+
self._sig = None
|
|
184
|
+
self._bbox = None
|
|
185
|
+
self._last_motion = 0.0
|
|
186
|
+
self.motion = 0.0
|
|
187
|
+
|
|
188
|
+
def observe(self, small_rgb: np.ndarray, box, now: float | None = None):
|
|
189
|
+
"""`small_rgb` 是未模糊的 1/4 采样图。返回 `(moved, stale)`。"""
|
|
190
|
+
now = time.perf_counter() if now is None else now
|
|
191
|
+
sig = small_rgb[::SIG_STEP, ::SIG_STEP].astype(np.int16).mean(axis=2)
|
|
192
|
+
moved = False
|
|
193
|
+
if self._bbox is not None and box != self._bbox:
|
|
194
|
+
moved = True
|
|
195
|
+
elif self._sig is not None and sig.shape == self._sig.shape:
|
|
196
|
+
d = np.abs(sig - self._sig)
|
|
197
|
+
moved = bool(float(d.mean()) > MOVE_MEAN or float(d.max()) > MOVE_MAX)
|
|
198
|
+
if moved:
|
|
199
|
+
self._last_motion = now
|
|
200
|
+
self.motion = min(1.0, self.motion * 0.50 + 0.50)
|
|
201
|
+
else:
|
|
202
|
+
self.motion *= 0.55
|
|
203
|
+
stale = (not self.force and not moved and box == self._bbox
|
|
204
|
+
and now - self._last_motion > STALE_AFTER)
|
|
205
|
+
self._sig = sig
|
|
206
|
+
self._bbox = box
|
|
207
|
+
return moved, stale
|
|
208
|
+
|
|
209
|
+
@property
|
|
210
|
+
def blend(self) -> float:
|
|
211
|
+
"""移动帧混上一帧的强度;停下后立刻归零,不留残影。"""
|
|
212
|
+
return 0.35 * max(0.0, min(1.0, self.motion))
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
@dataclass(slots=True)
|
|
216
|
+
class Shot:
|
|
217
|
+
"""一次抓屏的结果。CPU 用 `rgb`,GPU 用 `bgra`,避免重复抓。"""
|
|
218
|
+
|
|
219
|
+
shot: object # mss 的 ScreenShot
|
|
220
|
+
box: tuple # 屏幕坐标 (gl, gt, gr, gb)
|
|
221
|
+
offset: tuple # 窗口左上角在抓屏图里的 1/4 分辨率坐标
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
class CaptureSource:
|
|
225
|
+
"""按窗口矩形抓背景,外扩 PAD 供边缘透镜取「框外」的像素。"""
|
|
226
|
+
|
|
227
|
+
def __init__(self, screen: tuple[int, int], pad: int = PAD, cappable: bool = False):
|
|
228
|
+
self.screen = screen
|
|
229
|
+
self.pad = pad
|
|
230
|
+
self.cappable = cappable
|
|
231
|
+
|
|
232
|
+
def grab(self, sct, bbox) -> Shot | None:
|
|
233
|
+
l, t, r, b = (int(v) for v in bbox)
|
|
234
|
+
if r <= l or b <= t:
|
|
235
|
+
return None
|
|
236
|
+
sw, sh = self.screen
|
|
237
|
+
gl = max(0, l - self.pad)
|
|
238
|
+
gt = max(0, t - self.pad)
|
|
239
|
+
gr = min(sw, r + self.pad)
|
|
240
|
+
gb = min(sh, b + self.pad)
|
|
241
|
+
# 多显示器 / DPI 变化 / 被别的程序挪出屏幕时,钳位后宽高可能 <= 0;
|
|
242
|
+
# mss 会抛 "Region has zero or negative size",而 _work() 会每帧吞掉它
|
|
243
|
+
# —— 表现就是日志刷屏、背景永久停在旧帧。没有可抓的区域就别抓。
|
|
244
|
+
if gr - gl <= 0 or gb - gt <= 0:
|
|
245
|
+
return None
|
|
246
|
+
shot = sct.grab({"left": gl, "top": gt, "width": gr - gl, "height": gb - gt})
|
|
247
|
+
return Shot(shot=shot, box=(gl, gt, gr, gb),
|
|
248
|
+
offset=(int(round((l - gl) / LPF)), int(round((t - gt) / LPF))))
|
|
249
|
+
|
|
250
|
+
def to_image(self, shot: Shot, bbox) -> Image.Image:
|
|
251
|
+
"""抓屏帧 → RGB 图;录屏模式下先把自己挖掉。"""
|
|
252
|
+
im = Image.frombytes("RGB", shot.shot.size, shot.shot.rgb)
|
|
253
|
+
if self.cappable:
|
|
254
|
+
l, t, r, b = (int(v) for v in bbox)
|
|
255
|
+
gl, gt = shot.box[0], shot.box[1]
|
|
256
|
+
im = fill_own_rect(im, (l - gl, t - gt, r - gl, b - gt))
|
|
257
|
+
return im
|
|
258
|
+
|
|
259
|
+
def own_rect(self, shot: Shot, bbox) -> tuple[int, int, int, int]:
|
|
260
|
+
gl, gt = shot.box[0], shot.box[1]
|
|
261
|
+
l, t, r, b = bbox
|
|
262
|
+
return (l - gl, t - gt, r - gl, b - gt)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def finish(geo: Geometry, panel: Image.Image) -> Image.Image:
|
|
266
|
+
"""贴二值圆角遮罩;缝隙/圆角外是 KEY,被 Tk 抠成透明。"""
|
|
267
|
+
out = Image.new("RGB", (geo.width, geo.height), KEY)
|
|
268
|
+
out.paste(panel.convert("RGB"), (0, 0), geo.mask_hard)
|
|
269
|
+
return out
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def paste_mic(geo: Geometry, im: Image.Image) -> Image.Image:
|
|
273
|
+
"""麦克风图标贴进玻璃帧,按背后亮度选浅/深两版(和仓库原做法一致)。"""
|
|
274
|
+
x, y = geo.mic_pos
|
|
275
|
+
box = im.crop((x, y, x + MIC_SIZE, y + MIC_SIZE)).convert("L")
|
|
276
|
+
bg = float(np.median(np.asarray(box))) / 255.0
|
|
277
|
+
out = im.convert("RGBA")
|
|
278
|
+
icon = MIC_LIGHT if bg < 0.48 else MIC_DARK
|
|
279
|
+
out.paste(icon, (x, y), icon)
|
|
280
|
+
return out.convert("RGB")
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
__all__ = [
|
|
284
|
+
"CaptureSource", "FORCE", "MIC_DARK", "MIC_LIGHT", "MotionTracker", "Shot",
|
|
285
|
+
"fill_own_rect", "finish", "paste_mic", "resample",
|
|
286
|
+
]
|