museav-cli 2.3.0 → 2.4.1

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.
@@ -0,0 +1,312 @@
1
+ /**
2
+ * 本地去水印 —— remove-watermark 的核心实现。
3
+ * 链路:纯像素启发式定位水印(零模型依赖,不占内存)→ LaMa 掩码修复 →
4
+ * 输出干净图。也可 --mask 手工给掩码(白=要去除的区域),跳过自动定位。
5
+ * 全本地、免登录、零成本;许可证均兼容(LaMa 模型 Apache-2.0,onnxruntime MIT)。
6
+ */
7
+ import { mkdir, writeFile, stat } from 'node:fs/promises';
8
+ import { join } from 'node:path';
9
+ import { homedir } from 'node:os';
10
+ const LAMA_URLS = [
11
+ 'https://huggingface.co/Carve/LaMa-ONNX/resolve/main/lama_fp32.onnx',
12
+ // 国内网络拉不动 HF 时的镜像(同一文件;镜像没缓存会回落直链,所以放第二位)
13
+ 'https://hf-mirror.com/Carve/LaMa-ONNX/resolve/main/lama_fp32.onnx',
14
+ ];
15
+ const LAMA_PATH = join(homedir(), '.museav-models', 'lama', 'lama_fp32.onnx');
16
+ async function ensureLamaModel() {
17
+ try {
18
+ if ((await stat(LAMA_PATH)).size > 50_000_000)
19
+ return LAMA_PATH;
20
+ }
21
+ catch { /* 未下载 */ }
22
+ await mkdir(join(homedir(), '.museav-models', 'lama'), { recursive: true });
23
+ process.stderr.write('↓ 首次使用,下载 LaMa 修复模型(~200MB,一次性,缓存到 ~/.museav-models/lama)...\n');
24
+ let lastErr = null;
25
+ for (const url of LAMA_URLS) {
26
+ try {
27
+ const resp = await fetch(url, { redirect: 'follow' });
28
+ if (!resp.ok || !resp.body)
29
+ throw new Error(`HTTP ${resp.status}`);
30
+ const total = Number(resp.headers.get('content-length') || 0);
31
+ const chunks = [];
32
+ let got = 0;
33
+ const reader = resp.body.getReader();
34
+ for (;;) {
35
+ const { done, value } = await reader.read();
36
+ if (done)
37
+ break;
38
+ chunks.push(Buffer.from(value));
39
+ got += value.length;
40
+ if (total)
41
+ process.stderr.write(` ${(got / 1048576).toFixed(0)}/${(total / 1048576).toFixed(0)}MB\r`);
42
+ }
43
+ process.stderr.write('\n');
44
+ const buf = Buffer.concat(chunks);
45
+ if (buf.length < 50_000_000)
46
+ throw new Error('下载不完整');
47
+ await writeFile(LAMA_PATH, buf);
48
+ return LAMA_PATH;
49
+ }
50
+ catch (e) {
51
+ lastErr = e;
52
+ }
53
+ }
54
+ throw new Error(`LaMa 模型下载失败(${lastErr instanceof Error ? lastErr.message : lastErr}),也可手动放到 ${LAMA_PATH}`);
55
+ }
56
+ /** LaMa 要求边长被 8 整除:右/下边缘复制填充,修完再裁回 */
57
+ const pad8 = (n) => Math.ceil(n / 8) * 8;
58
+ /**
59
+ * 纯像素启发式水印定位(零模型依赖)。
60
+ * 原理:半透明水印(角标/文字)是「低对比、高频、铺在大片区域的细碎纹理」,
61
+ * 把四角区域做中值模糊后与原图差分,叠字区会出现稳定的高差值像素团。
62
+ * 判定保守:角区差分像素占比在 [0.3%, 8%] 才算水印(太少=没叠字,
63
+ * 太多=画面本身纹理复杂,不硬修);坐标 0-1000 归一化输出。
64
+ * 误检最坏是 LaMa 重绘一块(轻微损伤),另有 --mask 手工精确兜底。
65
+ */
66
+ export async function detectWatermarkBoxes(imagePath) {
67
+ const sharpMod = await import('sharp');
68
+ const sharp = sharpMod.default ?? sharpMod;
69
+ const meta = await sharp(imagePath).rotate().metadata();
70
+ const W = meta.width || 0;
71
+ const H = meta.height || 0;
72
+ if (!W || !H)
73
+ throw new Error('读不到图片尺寸');
74
+ const corners = [
75
+ { x: 0, y: 0 },
76
+ { x: 1, y: 0 },
77
+ { x: 0, y: 1 },
78
+ { x: 1, y: 1 },
79
+ ];
80
+ const cw = Math.round(W * 0.35);
81
+ const ch = Math.round(H * 0.35);
82
+ const boxes = [];
83
+ for (const { x, y } of corners) {
84
+ const left = x ? W - cw : 0;
85
+ const top = y ? H - ch : 0;
86
+ const { data: orig } = await sharp(imagePath).rotate()
87
+ .extract({ left, top, width: cw, height: ch }).raw().toBuffer({ resolveWithObject: true });
88
+ const { data: blurred } = await sharp(imagePath).rotate()
89
+ .extract({ left, top, width: cw, height: ch })
90
+ .median(7)
91
+ .raw().toBuffer({ resolveWithObject: true });
92
+ // 差分 + 阈值;记录超阈值像素的 bbox
93
+ let cnt = 0;
94
+ let minX = Infinity, minY = Infinity, maxX = -1, maxY = -1;
95
+ for (let i = 0; i < cw * ch; i++) {
96
+ const dr = Math.abs(orig[i * 3] - blurred[i * 3]);
97
+ const dg = Math.abs(orig[i * 3 + 1] - blurred[i * 3 + 1]);
98
+ const db = Math.abs(orig[i * 3 + 2] - blurred[i * 3 + 2]);
99
+ if (dr + dg + db > 90) { // 每通道均值 >30
100
+ cnt++;
101
+ const px = i % cw;
102
+ const py = Math.floor(i / cw);
103
+ if (px < minX)
104
+ minX = px;
105
+ if (px > maxX)
106
+ maxX = px;
107
+ if (py < minY)
108
+ minY = py;
109
+ if (py > maxY)
110
+ maxY = py;
111
+ }
112
+ }
113
+ const ratio = cnt / (cw * ch);
114
+ if (ratio >= 0.003 && ratio <= 0.08 && maxX >= 0) {
115
+ // 像素 → 0-1000 归一化(带 2% 外扩)
116
+ const pad = 0.02;
117
+ const box = {
118
+ x1: Math.max(0, ((left + minX) / W) * 1000 - pad * 1000),
119
+ y1: Math.max(0, ((top + minY) / H) * 1000 - pad * 1000),
120
+ x2: Math.min(1000, ((left + maxX) / W) * 1000 + pad * 1000),
121
+ y2: Math.min(1000, ((top + maxY) / H) * 1000 + pad * 1000),
122
+ };
123
+ // 贴角锚定:水印通常贴着所在角的边沿(5% 容差)。背景装饰/纹理也常被差分命中,
124
+ // 但它们离角远——角标类水印的判据就是「贴角」,否则宁可不修。
125
+ const anchored = (x === 0 && y === 0 && box.x1 <= 50 && box.y1 <= 50) ||
126
+ (x === 1 && y === 0 && box.x2 >= 950 && box.y1 <= 50) ||
127
+ (x === 0 && y === 1 && box.x1 <= 50 && box.y2 >= 950) ||
128
+ (x === 1 && y === 1 && box.x2 >= 950 && box.y2 >= 950);
129
+ if (anchored)
130
+ boxes.push(box);
131
+ }
132
+ }
133
+ return boxes;
134
+ }
135
+ /** 掩码修复主流程:mask 白色=要去除(Buffer 或文件路径);输出 PNG Buffer。
136
+ * 模型是固定 512×512 输入——整图缩进去会毁分辨率,所以按掩码连通域逐块处理:
137
+ * 裁出带边距的局部 → letterbox 进 512 修复 → 只把掩码内的像素贴回原图。 */
138
+ export async function inpaintLocal(imagePath, mask) {
139
+ const ort = await import('onnxruntime-node');
140
+ const sharpMod = await import('sharp');
141
+ const sharp = sharpMod.default ?? sharpMod;
142
+ const modelFile = await ensureLamaModel();
143
+ const session = await ort.InferenceSession.create(modelFile);
144
+ const S = 512;
145
+ const { data: rgb, info } = await sharp(imagePath).rotate().removeAlpha().raw().toBuffer({ resolveWithObject: true });
146
+ const W = info.width;
147
+ const H = info.height;
148
+ const maskRaw = await sharp(mask).rotate().resize(W, H, { fit: 'fill' }).greyscale().raw().toBuffer();
149
+ // 连通域(在 1/4 采样上 BFS,够用):返回像素坐标 bbox 列表
150
+ const boxes = connectedBoxes(maskRaw, W, H);
151
+ // 工作底图:逐块修复后叠回去
152
+ let out = rgb;
153
+ for (const box of boxes) {
154
+ // 边距:给修复模型一点上下文
155
+ const margin = Math.round(Math.max(box.w, box.h) * 0.35) + 16;
156
+ const x0 = Math.max(0, box.x - margin);
157
+ const y0 = Math.max(0, box.y - margin);
158
+ const x1 = Math.min(W, box.x + box.w + margin);
159
+ const y1 = Math.min(H, box.y + box.h + margin);
160
+ const cw = x1 - x0;
161
+ const ch = y1 - y0;
162
+ // letterbox 进 512×512(等比缩放居中,四周留黑)
163
+ const scale = Math.min(S / cw, S / ch);
164
+ const iw = Math.max(8, Math.round(cw * scale));
165
+ const ih = Math.max(8, Math.round(ch * scale));
166
+ const ox = Math.floor((S - iw) / 2);
167
+ const oy = Math.floor((S - ih) / 2);
168
+ const cropImg = await sharp(out, { raw: { width: W, height: H, channels: 3 } })
169
+ .extract({ left: x0, top: y0, width: cw, height: ch })
170
+ .resize(iw, ih, { fit: 'fill' })
171
+ .raw().toBuffer();
172
+ // ⚠ sharp 的坑:raw 1 通道经 extract→resize 会悄悄变 3 通道(长度×3),
173
+ // 后面按 1 通道读全是错位数据。掩码管线一律 toColourspace('b-w') 强制单通道。
174
+ const cropMask = await sharp(maskRaw, { raw: { width: W, height: H, channels: 1 } })
175
+ .extract({ left: x0, top: y0, width: cw, height: ch })
176
+ .resize(iw, ih, { fit: 'fill' })
177
+ .toColourspace('b-w')
178
+ .raw().toBuffer();
179
+ const imgC = Buffer.alloc(S * S * 3, 0);
180
+ const maskC = Buffer.alloc(S * S, 0);
181
+ for (let y = 0; y < ih; y++) {
182
+ for (let x = 0; x < iw; x++) {
183
+ const si = (y * iw + x) * 3;
184
+ const di = ((oy + y) * S + (ox + x)) * 3;
185
+ imgC[di] = cropImg[si];
186
+ imgC[di + 1] = cropImg[si + 1];
187
+ imgC[di + 2] = cropImg[si + 2];
188
+ maskC[(oy + y) * S + (ox + x)] = cropMask[y * iw + x] > 127 ? 1 : 0;
189
+ }
190
+ }
191
+ const N = S * S;
192
+ const imgF = new Float32Array(3 * N);
193
+ for (let i = 0; i < N; i++) {
194
+ imgF[i] = imgC[i * 3] / 255;
195
+ imgF[N + i] = imgC[i * 3 + 1] / 255;
196
+ imgF[2 * N + i] = imgC[i * 3 + 2] / 255;
197
+ }
198
+ const results = await session.run({
199
+ image: new ort.Tensor('float32', imgF, [1, 3, S, S]),
200
+ mask: new ort.Tensor('float32', new Float32Array(maskC), [1, 1, S, S]),
201
+ });
202
+ const o = results[session.outputNames[0]].data;
203
+ // 取回中心区域,缩回原尺寸
204
+ const back = Buffer.alloc(iw * ih * 3);
205
+ for (let y = 0; y < ih; y++) {
206
+ for (let x = 0; x < iw; x++) {
207
+ const so = ((oy + y) * S + (ox + x));
208
+ const di = (y * iw + x) * 3;
209
+ back[di] = clamp255(o[so]);
210
+ back[di + 1] = clamp255(o[N + so]);
211
+ back[di + 2] = clamp255(o[2 * N + so]);
212
+ }
213
+ }
214
+ const restoredFull = await sharp(back, { raw: { width: iw, height: ih, channels: 3 } })
215
+ .resize(cw, ch, { fit: 'fill' }).raw().toBuffer();
216
+ const maskFull = await sharp(cropMask, { raw: { width: iw, height: ih, channels: 1 } })
217
+ .resize(cw, ch, { fit: 'fill' })
218
+ .toColourspace('b-w')
219
+ .raw().toBuffer();
220
+ // 只把掩码内的像素贴回,其余保持原图(缩放往返会损伤未掩码区,不能整块覆盖)
221
+ const next = Buffer.from(out);
222
+ for (let i = 0; i < cw * ch; i++) {
223
+ if (maskFull[i] > 127) {
224
+ next[((y0 + Math.floor(i / cw)) * W + (x0 + (i % cw))) * 3] = restoredFull[i * 3];
225
+ next[(((y0 + Math.floor(i / cw)) * W + (x0 + (i % cw))) * 3) + 1] = restoredFull[i * 3 + 1];
226
+ next[(((y0 + Math.floor(i / cw)) * W + (x0 + (i % cw))) * 3) + 2] = restoredFull[i * 3 + 2];
227
+ }
228
+ }
229
+ out = next;
230
+ process.stderr.write(` 修复块 ${cw}x${ch} @(${x0},${y0}) 完成\n`);
231
+ }
232
+ return sharp(out, { raw: { width: W, height: H, channels: 3 } }).png().toBuffer();
233
+ }
234
+ const clamp255 = (v) => Math.max(0, Math.min(255, Math.round(v)));
235
+ /** 掩码连通域 → 像素 bbox 列表。在 1/4 采样上网格 BFS,快且够准 */
236
+ function connectedBoxes(mask, W, H) {
237
+ const step = 4;
238
+ const gw = Math.ceil(W / step);
239
+ const gh = Math.ceil(H / step);
240
+ const grid = new Uint8Array(gw * gh);
241
+ for (let gy = 0; gy < gh; gy++) {
242
+ for (let gx = 0; gx < gw; gx++) {
243
+ let hit = false;
244
+ for (let dy = 0; dy < step && !hit; dy++) {
245
+ for (let dx = 0; dx < step && !hit; dx++) {
246
+ const x = gx * step + dx;
247
+ const y = gy * step + dy;
248
+ if (x < W && y < H && mask[y * W + x] > 127)
249
+ hit = true;
250
+ }
251
+ }
252
+ grid[gy * gw + gx] = hit ? 1 : 0;
253
+ }
254
+ }
255
+ const seen = new Uint8Array(gw * gh);
256
+ const boxes = [];
257
+ const q = [];
258
+ for (let g = 0; g < grid.length; g++) {
259
+ if (!grid[g] || seen[g])
260
+ continue;
261
+ q.length = 0;
262
+ q.push(g);
263
+ seen[g] = 1;
264
+ let minx = gw, miny = gh, maxx = -1, maxy = -1;
265
+ while (q.length) {
266
+ const cur = q.pop();
267
+ const cx = cur % gw;
268
+ const cy = Math.floor(cur / gw);
269
+ minx = Math.min(minx, cx);
270
+ maxx = Math.max(maxx, cx);
271
+ miny = Math.min(miny, cy);
272
+ maxy = Math.max(maxy, cy);
273
+ for (const [dx, dy] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) {
274
+ const nx = cx + dx, ny = cy + dy;
275
+ if (nx < 0 || ny < 0 || nx >= gw || ny >= gh)
276
+ continue;
277
+ const ni = ny * gw + nx;
278
+ if (grid[ni] && !seen[ni]) {
279
+ seen[ni] = 1;
280
+ q.push(ni);
281
+ }
282
+ }
283
+ }
284
+ boxes.push({ x: minx * step, y: miny * step, w: (maxx - minx + 1) * step, h: (maxy - miny + 1) * step });
285
+ }
286
+ return boxes;
287
+ }
288
+ /** 从检测框生成掩码 Buffer(框外扩 2%,白=去除区) */
289
+ export async function maskFromBoxes(imagePath, boxes) {
290
+ const sharpMod = await import('sharp');
291
+ const sharp = sharpMod.default ?? sharpMod;
292
+ const meta = await sharp(imagePath).metadata();
293
+ const w = meta.width || 0;
294
+ const h = meta.height || 0;
295
+ if (!w || !h)
296
+ throw new Error('读不到图片尺寸');
297
+ // 坐标体系一次判清:全部 ≤1.5 视为 0-1 归一化,否则按 qwen 惯例的 0-1000
298
+ const all = boxes.flatMap((b) => [b.x1, b.y1, b.x2, b.y2]);
299
+ const denom = Math.max(...all) <= 1.5 ? 1 : 1000;
300
+ const svg = boxes
301
+ .map((b) => {
302
+ const pad = 0.02;
303
+ const x1 = Math.max(0, (b.x1 / denom) * w - w * pad);
304
+ const y1 = Math.max(0, (b.y1 / denom) * h - h * pad);
305
+ const x2 = Math.min(w, (b.x2 / denom) * w + w * pad);
306
+ const y2 = Math.min(h, (b.y2 / denom) * h + h * pad);
307
+ return `<rect x="${x1}" y="${y1}" width="${Math.max(1, x2 - x1)}" height="${Math.max(1, y2 - y1)}" fill="#fff"/>`;
308
+ })
309
+ .join('');
310
+ const maskSvg = Buffer.from(`<svg width="${w}" height="${h}"><rect width="${w}" height="${h}" fill="#000"/>${svg}</svg>`);
311
+ return sharp(maskSvg).png().toBuffer();
312
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "museav-cli",
3
- "version": "2.3.0",
3
+ "version": "2.4.1",
4
4
  "description": "MUSE AV 出图中台官方 CLI —— 命令行调中台 API 出图、出视频、读图逆向、图生模板",
5
5
  "type": "module",
6
6
  "bin": {
@@ -4,6 +4,8 @@
4
4
  import { stat, writeFile } from 'node:fs/promises'
5
5
  import { basename, dirname, extname, join } from 'node:path'
6
6
  import { removeBackgroundLocal, BG_MODELS, type BgModelKey } from '../local-bg.js'
7
+ import { upscaleLocal, UPSCALE_MODELS, type UpscaleModel } from '../local-upscale.js'
8
+ import { detectWatermarkBoxes, inpaintLocal, maskFromBoxes } from '../local-watermark.js'
7
9
 
8
10
  async function fileExists(path: string): Promise<boolean> {
9
11
  try {
@@ -93,3 +95,62 @@ export async function removeBgCmd(input: string, opts: RemoveBgOpts): Promise<vo
93
95
  process.stderr.write(`✅ 抠图完成(${BG_MODELS[modelKey].label},用时 ${((Date.now() - start) / 1000).toFixed(1)}s,${fmtBytes(png.length)})\n`)
94
96
  console.log(outPath)
95
97
  }
98
+
99
+ export interface RemoveWatermarkOpts {
100
+ out?: string
101
+ mask?: string
102
+ overwrite?: boolean
103
+ }
104
+
105
+ export async function removeWatermarkCmd(input: string, opts: RemoveWatermarkOpts): Promise<void> {
106
+ if (!(await fileExists(input))) throw new Error(`文件不存在: ${input}`)
107
+ const outPath = opts.out || defaultOut(input, 'clean', 'png')
108
+ if ((await fileExists(outPath)) && !opts.overwrite) {
109
+ throw new Error(`输出已存在(用 --overwrite 覆盖或 --out 换路径): ${outPath}`)
110
+ }
111
+
112
+ const start = Date.now()
113
+ let mask: Buffer | string
114
+ if (opts.mask) {
115
+ if (!(await fileExists(opts.mask))) throw new Error(`掩码文件不存在: ${opts.mask}`)
116
+ mask = opts.mask
117
+ process.stderr.write('使用手工掩码,跳过自动定位\n')
118
+ } else {
119
+ // 纯像素启发式定位水印(零模型依赖),失败时明确指引改用 --mask
120
+ const boxes = await detectWatermarkBoxes(input)
121
+ if (!boxes.length) throw new Error('自动定位没找到水印(角标式半透明水印通常可识别;复杂画面请用 --mask 手工指定)')
122
+ process.stderr.write(`定位到 ${boxes.length} 处水印:${boxes.map((b) => `(${b.x1},${b.y1})-(${b.x2},${b.y2})`).join(' ')}\n`)
123
+ mask = await maskFromBoxes(input, boxes)
124
+ }
125
+
126
+ const png = await inpaintLocal(input, mask)
127
+ await writeFile(outPath, png)
128
+ process.stderr.write(`✅ 去水印完成(用时 ${((Date.now() - start) / 1000).toFixed(1)}s,${fmtBytes(png.length)})\n`)
129
+ console.log(outPath)
130
+ }
131
+
132
+ export interface UpscaleOpts {
133
+ out?: string
134
+ scale?: string
135
+ model?: string
136
+ overwrite?: boolean
137
+ }
138
+
139
+ export async function upscaleCmd(input: string, opts: UpscaleOpts): Promise<void> {
140
+ if (!(await fileExists(input))) throw new Error(`文件不存在: ${input}`)
141
+ const scale = opts.scale ? Number(opts.scale) : 4
142
+ if (![2, 3, 4].includes(scale)) throw new Error('--scale 只支持 2 / 3 / 4')
143
+ const model = (opts.model || 'realesrgan-x4plus') as UpscaleModel
144
+ if (!(model in UPSCALE_MODELS)) throw new Error(`--model 只支持 ${Object.keys(UPSCALE_MODELS).join(' / ')}`)
145
+
146
+ const outPath = opts.out || defaultOut(input, `${scale}x`, 'png')
147
+ if ((await fileExists(outPath)) && !opts.overwrite) {
148
+ throw new Error(`输出已存在(用 --overwrite 覆盖或 --out 换路径): ${outPath}`)
149
+ }
150
+
151
+ const start = Date.now()
152
+ await upscaleLocal({ input, output: outPath, scale, model })
153
+ const size = (await stat(outPath)).size
154
+ process.stderr.write(`✅ 超分完成(${UPSCALE_MODELS[model].label},${scale}x,用时 ${((Date.now() - start) / 1000).toFixed(1)}s,${fmtBytes(size)})\n`)
155
+ console.log(outPath)
156
+ }
@@ -1,18 +1,19 @@
1
1
  /** museav reverse —— 图片逆向(SCULPT 六要素反推 prompt)。
2
- * 主路是本地 Ollamaqwen3-vl),快、零成本、无需登录;中台 API 是回落路,走回落时会明确提示较慢。
3
- * client 懒构造(getClient):本地路成功就完全不碰中台凭证。
4
- * 本地系统的 AI 能力统一收口在这个 CLI,reverse 是第一个本地化的能力。 */
2
+ * 默认走中台 API(快、稳定、不需本地模型);--local 可切本地 Ollama(需自备 qwen3-vl
3
+ * 仅在用户显式要求时使用——本地大模型默认不拉起,不给用户的内存添负担)。
4
+ * client 懒构造:本地路成功就完全不碰中台凭证。 */
5
5
  import type { StudioClient, ReverseResult } from '../client.js'
6
6
  import { checkLocalVlm, reverseLocally, LOCAL_VLM_MODEL } from '../local-vision.js'
7
7
 
8
8
  export async function reverse(
9
9
  getClient: () => StudioClient,
10
10
  input: string,
11
- opts: { api?: boolean } = {},
11
+ opts: { api?: boolean; local?: boolean } = {},
12
12
  ): Promise<void> {
13
13
  const isUrl = /^https?:\/\//.test(input)
14
14
 
15
- if (!opts.api && !isUrl) {
15
+ // 本地路只在用户显式 --local 且输入是本地文件时尝试;服务不可用给出指引后回落 API
16
+ if (opts.local && !isUrl) {
16
17
  const status = await checkLocalVlm()
17
18
  if (status.running && status.modelPresent) {
18
19
  try {
@@ -27,7 +28,7 @@ export async function reverse(
27
28
  } else {
28
29
  process.stderr.write(`⚠ 本地读图不可用(${status.reason}),回落中台 API —— 速度较慢,请耐心等待\n`)
29
30
  }
30
- } else if (!opts.api && isUrl) {
31
+ } else if (opts.local && isUrl) {
31
32
  process.stderr.write(`ℹ URL 输入走中台 API(本地路只收文件路径)\n`)
32
33
  }
33
34
 
package/src/index.ts CHANGED
@@ -15,7 +15,7 @@ import { bindFeishu } from './commands/bind-feishu.js'
15
15
  import { printWelcome } from './commands/welcome.js'
16
16
  import { gen } from './commands/gen.js'
17
17
  import { reverse } from './commands/reverse.js'
18
- import { compressCmd, removeBgCmd } from './commands/img-tools.js'
18
+ import { compressCmd, removeBgCmd, upscaleCmd, removeWatermarkCmd } from './commands/img-tools.js'
19
19
  import { projects, createProject, listAssets, addAsset, removeAsset, resolveWorkspace } from './commands/projects.js'
20
20
  import { imageToTemplate } from './commands/image-to-template.js'
21
21
  import { upload } from './commands/upload.js'
@@ -190,6 +190,23 @@ program
190
190
  .option('--overwrite', '允许覆盖已存在的输出文件')
191
191
  .action(asyncRun((input: string, opts: any) => removeBgCmd(input, opts)))
192
192
 
193
+ program
194
+ .command('upscale <file>')
195
+ .description('本地超分放大(Real-ESRGAN + Vulkan GPU,免登录):默认 4x 输出 PNG。首次使用自动下载引擎与模型(~65MB,缓存 ~/.museav-bin 与 ~/.museav-models)')
196
+ .option('--out <path>', '输出路径(默认 <名>-<N>x.png)')
197
+ .option('--scale <n>', '放大倍数 2 / 3 / 4,默认 4')
198
+ .option('--model <name>', 'realesrgan-x4plus(通用照片,默认)/ realesrgan-x4plus-anime(插画动漫)')
199
+ .option('--overwrite', '允许覆盖已存在的输出文件')
200
+ .action(asyncRun((input: string, opts: any) => upscaleCmd(input, opts)))
201
+
202
+ program
203
+ .command('remove-watermark <file>')
204
+ .description('本地去水印(免登录):纯像素启发式自动定位水印 → LaMa 掩码修复,零模型依赖。首次使用自动下载修复模型(~200MB);复杂画面用 --mask 手工指定(白=去除区)')
205
+ .option('--out <path>', '输出路径(默认 <名>-clean.png)')
206
+ .option('--mask <file>', '手工掩码图(白色=要去除的区域),跳过自动定位')
207
+ .option('--overwrite', '允许覆盖已存在的输出文件')
208
+ .action(asyncRun((input: string, opts: any) => removeWatermarkCmd(input, opts)))
209
+
193
210
  // 工作区(项目)与项目素材库:平台 → 账户 → 工作区三层归属,素材挂工作区
194
211
  const projectsCmd = program
195
212
  .command('projects')
@@ -226,8 +243,9 @@ assetsCmd
226
243
 
227
244
  program
228
245
  .command('reverse <input>')
229
- .description('读图:反推 SCULPT prompt,stdout 输出英文 prompt。主路本地 Ollamaqwen3-vl,快,无需登录);本地不可用回落中台 API(会提示较慢)。只读图;要做成模板用 image-to-template')
230
- .option('--api', '跳过本地 Ollama,强制走中台 API(慢,需登录)')
246
+ .description('读图:反推 SCULPT prompt,stdout 输出英文 prompt。默认走中台 API(需登录);--local 显式切本地 Ollama(需自备 qwen3-vl)。只读图;要做成模板用 image-to-template')
247
+ .option('--api', '强制走中台 API(默认路径)')
248
+ .option('--local', '改用本地 Ollama 读图(需先 ollama pull qwen3-vl:8b;本地不可用时回落 API)')
231
249
  .action(withLazyClient((getClient: () => StudioClient, input: string, opts: any) => reverse(getClient, input, opts)))
232
250
 
233
251
  program
@@ -0,0 +1,123 @@
1
+ /**
2
+ * 本地超分(放大)—— upscale 的核心实现。
3
+ * 引擎:upscayl-ncnn(Real-ESRGAN 的 ncnn/Vulkan 后端,AGPL-3.0 —— 它是独立进程
4
+ * 二进制而非链接进 npm 包,CLI 与之分发解耦,不构成合并作品;这与把 AGPL 代码
5
+ * 编进依赖是两回事)。
6
+ * 跨平台:macOS(universal)/ Windows / Linux 二进制都在 upscayl 官方 release;
7
+ * 解压统一走 `tar -xf`(Win10+/macOS/Linux 都自带 libarchive 版 tar,不依赖 unzip);
8
+ * 二进制落地后 chmod +x(Windows 不需要)。全程 node:child_process execFile,零 shell。
9
+ */
10
+ import { mkdir, writeFile, stat, chmod } from 'node:fs/promises'
11
+ import { join } from 'node:path'
12
+ import { homedir } from 'node:os'
13
+ import { execFile } from 'node:child_process'
14
+ import { promisify } from 'node:util'
15
+ const run = promisify(execFile)
16
+
17
+ // 锁定已实测的版本(20251207-174704,macOS universal 实测可用),升级要重新过测试
18
+ const UPSCAYL_TAG = '20251207-174704'
19
+ const UPSCAYL_BASE = `https://github.com/upscayl/upscayl-ncnn/releases/download/${UPSCAYL_TAG}/upscayl-bin-${UPSCAYL_TAG}`
20
+ // 模型从 Real-ESRGAN 官方 release 的 zip 里取(只取需要的两个,别拖全量)
21
+ const MODEL_ZIP = 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesrgan-ncnn-vulkan-20220424-macos.zip'
22
+
23
+ export const UPSCALE_MODELS = {
24
+ 'realesrgan-x4plus': { label: '通用照片(默认)' },
25
+ 'realesrgan-x4plus-anime': { label: '插画/动漫' },
26
+ } as const
27
+ export type UpscaleModel = keyof typeof UPSCALE_MODELS
28
+
29
+ const BIN_DIR = join(homedir(), '.museav-bin', 'upscayl')
30
+ const MODEL_DIR = join(homedir(), '.museav-models')
31
+
32
+ function platformAsset(): { zip: string; exe: string } {
33
+ if (process.platform === 'win32') return { zip: `${UPSCAYL_BASE}-windows.zip`, exe: 'upscayl-bin.exe' }
34
+ if (process.platform === 'darwin') return { zip: `${UPSCAYL_BASE}-macos.zip`, exe: 'upscayl-bin' }
35
+ return { zip: `${UPSCAYL_BASE}-linux.zip`, exe: 'upscayl-bin' }
36
+ }
37
+
38
+ async function exists(path: string): Promise<boolean> {
39
+ try {
40
+ await stat(path)
41
+ return true
42
+ } catch {
43
+ return false
44
+ }
45
+ }
46
+
47
+ async function download(url: string, dest: string, label: string): Promise<void> {
48
+ const resp = await fetch(url)
49
+ if (!resp.ok || !resp.body) throw new Error(`${label} 下载失败 HTTP ${resp.status}`)
50
+ const total = Number(resp.headers.get('content-length') || 0)
51
+ const chunks: Buffer[] = []
52
+ let got = 0
53
+ const reader = resp.body.getReader()
54
+ for (;;) {
55
+ const { done, value } = await reader.read()
56
+ if (done) break
57
+ chunks.push(Buffer.from(value))
58
+ got += value.length
59
+ if (total) process.stderr.write(` ${label} ${(got / 1048576).toFixed(1)}/${(total / 1048576).toFixed(0)}MB\r`)
60
+ }
61
+ process.stderr.write('\n')
62
+ await writeFile(dest, Buffer.concat(chunks))
63
+ }
64
+
65
+ /** 首次使用时准备好二进制与模型,返回 { exe, modelDir }。之后直接走缓存 */
66
+ export async function ensureUpscaleRuntime(): Promise<{ exe: string; modelDir: string }> {
67
+ const { zip, exe } = platformAsset()
68
+ const exePath = join(BIN_DIR, exe)
69
+ const modelDir = join(MODEL_DIR, 'realesrgan')
70
+ const paramPath = join(modelDir, 'realesrgan-x4plus.param')
71
+
72
+ if (!(await exists(exePath))) {
73
+ await mkdir(BIN_DIR, { recursive: true })
74
+ const zipPath = join(BIN_DIR, `dl-${process.platform}.zip`)
75
+ process.stderr.write(`↓ 首次使用,下载超分引擎(~15MB,一次性,缓存到 ${BIN_DIR})...\n`)
76
+ await download(zip, zipPath, '引擎')
77
+ // tar -xf 解压:Win10+/macOS/Linux 自带,比依赖 unzip 稳
78
+ await run('tar', ['-xf', zipPath, '-C', BIN_DIR], { windowsHide: true })
79
+ // zip 里是 upscayl-bin-<tag>-<os>/upscayl-bin,拍平到 BIN_DIR
80
+ const { readdir } = await import('node:fs/promises')
81
+ for (const entry of await readdir(BIN_DIR, { withFileTypes: true })) {
82
+ if (entry.isDirectory()) {
83
+ const { rename, readdir: rd } = await import('node:fs/promises')
84
+ for (const f of await rd(join(BIN_DIR, entry.name))) {
85
+ await rename(join(BIN_DIR, entry.name, f), join(BIN_DIR, f))
86
+ }
87
+ }
88
+ }
89
+ if (process.platform !== 'win32') await chmod(exePath, 0o755)
90
+ if (!(await exists(exePath))) throw new Error(`解压后未找到 ${exe},请检查 ${BIN_DIR}`)
91
+ const { unlink } = await import('node:fs/promises')
92
+ await unlink(zipPath).catch(() => {})
93
+ }
94
+
95
+ if (!(await exists(paramPath))) {
96
+ await mkdir(modelDir, { recursive: true })
97
+ const zipPath = join(MODEL_DIR, 'dl-models.zip')
98
+ process.stderr.write('↓ 首次使用,下载超分模型(~50MB,一次性)...\n')
99
+ await download(MODEL_ZIP, zipPath, '模型')
100
+ await run('tar', ['-xf', zipPath, '-C', MODEL_DIR, 'models/realesrgan-x4plus.param', 'models/realesrgan-x4plus.bin', 'models/realesrgan-x4plus-anime.param', 'models/realesrgan-x4plus-anime.bin'], { windowsHide: true })
101
+ const { rename, rm } = await import('node:fs/promises')
102
+ for (const f of ['realesrgan-x4plus.param', 'realesrgan-x4plus.bin', 'realesrgan-x4plus-anime.param', 'realesrgan-x4plus-anime.bin']) {
103
+ await rename(join(MODEL_DIR, 'models', f), join(modelDir, f)).catch(() => {})
104
+ }
105
+ await rm(join(MODEL_DIR, 'models'), { recursive: true, force: true }).catch(() => {})
106
+ await rm(zipPath, { force: true }).catch(() => {})
107
+ if (!(await exists(paramPath))) throw new Error(`模型解压失败,请检查 ${modelDir}`)
108
+ }
109
+
110
+ return { exe: exePath, modelDir }
111
+ }
112
+
113
+ /** 超分主流程:返回输出文件的字节数组由引擎直写磁盘,这里只负责调度 */
114
+ export async function upscaleLocal(opts: {
115
+ input: string
116
+ output: string
117
+ scale: number
118
+ model: UpscaleModel
119
+ }): Promise<void> {
120
+ const { exe, modelDir } = await ensureUpscaleRuntime()
121
+ // 不走 shell 拼接;路径原样传参,空格/中文路径都安全
122
+ await run(exe, ['-i', opts.input, '-o', opts.output, '-s', String(opts.scale), '-n', opts.model, '-m', modelDir], { windowsHide: true })
123
+ }