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.
- package/AGENTS.md +6 -0
- package/CHANGELOG.md +14 -0
- package/README.md +44 -8
- package/dist/commands/img-tools.d.ts +13 -0
- package/dist/commands/img-tools.js +49 -0
- package/dist/commands/reverse.d.ts +4 -3
- package/dist/commands/reverse.js +3 -2
- package/dist/index.js +19 -3
- package/dist/local-upscale.d.ts +21 -0
- package/dist/local-upscale.js +115 -0
- package/dist/local-watermark.d.ts +21 -0
- package/dist/local-watermark.js +312 -0
- package/package.json +1 -1
- package/src/commands/img-tools.ts +61 -0
- package/src/commands/reverse.ts +7 -6
- package/src/index.ts +21 -3
- package/src/local-upscale.ts +123 -0
- package/src/local-watermark.ts +320 -0
package/AGENTS.md
CHANGED
|
@@ -64,8 +64,14 @@ museav upload ./face.png
|
|
|
64
64
|
# compress: resize/re-encode via sharp. Output <name>-min.<fmt>, never overwrites input.
|
|
65
65
|
# remove-bg: ISNet via onnxruntime-node → alpha PNG (~170MB model auto-downloaded
|
|
66
66
|
# to ~/.museav-models on first use; %USERPROFILE%\.museav-models on Windows).
|
|
67
|
+
# upscale: Real-ESRGAN via Vulkan GPU → 2M image becomes 10M+ (~65MB engine+model
|
|
68
|
+
# auto-downloaded once to ~/.museav-bin + ~/.museav-models).
|
|
69
|
+
# remove-watermark: pixel-heuristic auto-locate (zero model deps) + LaMa inpainting
|
|
70
|
+
# → clean PNG (~200MB model on-demand, freed after run).
|
|
67
71
|
museav compress ./photo.jpg --max-edge 800 --format webp --quality 70
|
|
68
72
|
museav remove-bg ./shoe.png # stdout: path to <name>-nobg.png
|
|
73
|
+
museav upscale ./photo.jpg --scale 4 # stdout: path to <name>-4x.png
|
|
74
|
+
museav remove-watermark ./poster.jpg # stdout: path to <name>-clean.png
|
|
69
75
|
|
|
70
76
|
# List your own (or, if using a tenant apiKey, your tenant's) recent jobs as JSON
|
|
71
77
|
museav jobs --limit 10 --status failed
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 2.4.0 · 2026-08-17
|
|
4
|
+
|
|
5
|
+
**本地图像工具箱补全(免登录、零成本、macOS/Windows 通用)。** 均为轻量级工具,无大模型常驻内存。
|
|
6
|
+
|
|
7
|
+
- `upscale <file>`:Real-ESRGAN 超分(Vulkan GPU 加速),`--scale 2/3/4`、`--model`(通用照片 / 插画动漫)。首次自动下载引擎+模型(~65MB);M3 实测 4x 约 30 秒。
|
|
8
|
+
- `remove-watermark <file>`:本地去水印。**纯像素启发式自动定位**(零模型依赖,角标式水印实测零误检)→ LaMa 掩码修复;复杂画面用 `--mask` 手工指定。修复模型 ~200MB 按需下载、用完即释放。
|
|
9
|
+
- 依赖红线:本地绝不自动拉起开源大模型(reverse 已翻回 API 默认,`--local` 显式可选;Ollama 与 qwen3-vl 已从本机卸载)。
|
|
10
|
+
|
|
11
|
+
**中台与前端联动。**
|
|
12
|
+
|
|
13
|
+
- 上游目录移除 gemini 死条目(路由/密钥早已清除,目录残留会误判读图有冗余),相关测试 mock 统一改用 volcengine-plan-vision。
|
|
14
|
+
- museav-web 参考图选择器接入工作区素材库:上传同步入库、素材库面板点选直接垫图、工作区切换自动刷新。
|
|
15
|
+
- 发布卡片样式重排:标题/升级命令/仓库链接全部对齐 `museav-cli` 新名(旧包名已停更,照旧卡片敲命令装不到新版),剥掉 changelog 标题噪声,双列字段 + 三按钮。
|
|
16
|
+
|
|
3
17
|
## 2.3.0 · 2026-08-17
|
|
4
18
|
|
|
5
19
|
**项目(工作区)与项目素材库。** 层级:平台 → 账户 → 工作区,素材挂工作区——人像库的项目出模特图、产品库的项目出电商图,业务隔离互不污染。`--project` 收 id 或名称。
|
package/README.md
CHANGED
|
@@ -85,6 +85,27 @@
|
|
|
85
85
|
|
|
86
86
|
两条路径互不依赖,选跟你身份匹配的那条,不用两个都配。下面「快速开始」走的是平台用户(login)这条路;如果你是租户,直接跳到下面「B 端 / CI 场景」那一节。
|
|
87
87
|
|
|
88
|
+
## 能力速览(小白先看这个)
|
|
89
|
+
|
|
90
|
+
`museav` 能干什么,一句话一组:
|
|
91
|
+
|
|
92
|
+
| 分类 | 能力 | 命令 |
|
|
93
|
+
|---|---|---|
|
|
94
|
+
| **AI 生成**(中台) | 文字出图 / 垫图出图 / 技能出图 / 模板出图 | `gen`(含 `--ref` 垫图、`--transparent` 透明底) |
|
|
95
|
+
| | 文生视频 / 图生视频 | `gen --video` |
|
|
96
|
+
| | 从已有图片反推提示词 | `reverse` |
|
|
97
|
+
| | 一张图做成可复用模板 | `image-to-template` |
|
|
98
|
+
| | 建/查图片模板、视频模板 | `templates` / `video-templates` |
|
|
99
|
+
| **项目管理**(中台) | 工作区(项目)+ 每个项目自己的素材库 | `projects`、`projects assets` |
|
|
100
|
+
| | 素材直链垫图、作品归档进项目 | `gen --project`、`jobs --project` |
|
|
101
|
+
| **本地图像处理**(免登录/免费/离线) | 压缩图片 | `compress` |
|
|
102
|
+
| | 抠图去背景(输出透明 PNG) | `remove-bg` |
|
|
103
|
+
| | **放大清晰度**(2M → 10M+ 级) | `upscale` |
|
|
104
|
+
| | 去水印 | `remove-watermark` |
|
|
105
|
+
| **素材与统计** | 上传素材 / 查任务 / 查余额 / 查模型 / 查身份 | `upload` / `jobs` / `balance` / `models` / `whoami` |
|
|
106
|
+
|
|
107
|
+
> 本地工具(`compress` / `remove-bg` / `upscale` / `remove-watermark`)**不用登录、不花一分钱**,装了就能用;其余命令需要一个凭证(个人 `login` 或租户 apiKey)。
|
|
108
|
+
|
|
88
109
|
---
|
|
89
110
|
|
|
90
111
|
## 快速开始
|
|
@@ -237,24 +258,22 @@ museav gen --template "$ID" --fields '{"artist":"..."}'
|
|
|
237
258
|
|
|
238
259
|
### 图片逆向 `reverse`
|
|
239
260
|
|
|
240
|
-
逆推出图 prompt
|
|
261
|
+
逆推出图 prompt,可以直接拿去再出一张同风格。**默认走中台 API**(SCULPT 六要素:主体/构图/世界观/光影/输出/质感);`--local` 可显式切本地 Ollama(需自备 qwen3-vl,本地不可用时自动回落 API):
|
|
241
262
|
|
|
242
263
|
```bash
|
|
243
|
-
#
|
|
264
|
+
# 默认:中台 API(需登录)
|
|
244
265
|
museav reverse photo.png
|
|
245
266
|
|
|
246
|
-
#
|
|
247
|
-
museav reverse
|
|
267
|
+
# 显式本地:需 ollama pull qwen3-vl:8b 自备模型(本地大模型默认不自动拉起)
|
|
268
|
+
museav reverse photo.png --local
|
|
248
269
|
|
|
249
|
-
#
|
|
250
|
-
museav reverse photo.png
|
|
270
|
+
# 图片 URL 走中台 API
|
|
271
|
+
museav reverse https://example.com/photo.png
|
|
251
272
|
|
|
252
273
|
# 逆向 + 出图,一条龙
|
|
253
274
|
museav gen --prompt "$(museav reverse photo.png)"
|
|
254
275
|
```
|
|
255
276
|
|
|
256
|
-
本地路依赖:Ollama 运行中(`brew services start ollama`)+ 模型在位(`ollama pull qwen3-vl:8b`,换档位设 `MUSEAV_LOCAL_VLM`,自定地址设 `OLLAMA_HOST`)。缺哪个都会提示对应命令并回落 API。
|
|
257
|
-
|
|
258
277
|
分析详情打到 stderr(人看),**stdout 只输出英文 prompt**(机器用,方便管道)。
|
|
259
278
|
|
|
260
279
|
`reverse` **只读图**,不会顺手帮你建模板。要把图做成模板看下一节——中台 2026-08-16 把这两件事
|
|
@@ -341,6 +360,21 @@ museav gen --prompt "$(museav reverse $(museav remove-bg shoe.png))" # 抠图
|
|
|
341
360
|
- 两个命令的输出默认带后缀(`-min` / `-nobg`),不会覆盖你的输入文件;要覆盖已存在的输出加 `--overwrite`。
|
|
342
361
|
- 可选依赖缺失时不炸,报一行重装指引(`npm install -g museav-cli`)。
|
|
343
362
|
|
|
363
|
+
### 超分 `upscale` 与去水印 `remove-watermark`(轻量工具,无大模型常驻)
|
|
364
|
+
|
|
365
|
+
```bash
|
|
366
|
+
# 超分放大(Real-ESRGAN + Vulkan GPU;首次自动下载引擎+模型 ~65MB)
|
|
367
|
+
museav upscale photo.jpg --scale 4 # 默认输出 <名>-4x.png
|
|
368
|
+
museav upscale anime.png --model realesrgan-x4plus-anime
|
|
369
|
+
|
|
370
|
+
# 去水印(纯像素启发式定位 → LaMa 修复,零模型依赖;修复模型 ~200MB 按需下载)
|
|
371
|
+
museav remove-watermark photo.jpg # 角标式水印自动定位
|
|
372
|
+
museav remove-watermark photo.jpg --mask mask.png # 复杂画面手工掩码(白=去除区)
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
- 依赖红线:本地**绝不自动拉起开源大模型**——这些工具是轻量 CNN/传统算法,用完即释放内存。reverse 的本地路已翻回显式 `--local`(需自备 Ollama),默认走中台 API。
|
|
376
|
+
- 二进制与模型缓存:`~/.museav-bin/upscayl`(引擎)、`~/.museav-models/`(模型),Windows 对应 `%USERPROFILE%` 下同名目录;删除即彻底清理。
|
|
377
|
+
|
|
344
378
|
### 项目(工作区)与项目素材库 `projects`
|
|
345
379
|
|
|
346
380
|
层级:**平台 → 账户 → 工作区**。一个账户最多 5 个项目,每个项目挂自己的素材库——「人像库的项目出模特图、产品库的项目出电商图」,业务隔离互不污染:
|
|
@@ -463,6 +497,8 @@ console.log(r.sculpt.light) // 光影分析
|
|
|
463
497
|
| `upload <file>` | 上传素材(图片/音频/视频) | 公网直链 |
|
|
464
498
|
| `compress <file>` | 本地压缩图片(免登录,`--max-edge`/`--quality`/`--format`) | 产物路径 |
|
|
465
499
|
| `remove-bg <file>` | 本地抠图去背景(免登录,输出带 alpha 的 PNG) | 产物路径 |
|
|
500
|
+
| `upscale <file>` | 本地超分放大,2M 图变 10M+(免登录,Real-ESRGAN GPU,`--scale 2/3/4`) | 产物路径 |
|
|
501
|
+
| `remove-watermark <file>` | 本地去水印(免登录,自动定位 + LaMa 修复,`--mask` 手工兜底) | 产物路径 |
|
|
466
502
|
| `projects` | 工作区(项目)列表:一账户多项目,各带自己的素材库 | 项目 id 列表 |
|
|
467
503
|
| `projects assets --project` | 项目素材库:ls / add / rm(垫图母版,人像库/产品库各管各的) | `id<TAB>url` 行 |
|
|
468
504
|
| `models` | 可用模型 | 模型名列表 |
|
|
@@ -14,3 +14,16 @@ export interface RemoveBgOpts {
|
|
|
14
14
|
overwrite?: boolean;
|
|
15
15
|
}
|
|
16
16
|
export declare function removeBgCmd(input: string, opts: RemoveBgOpts): Promise<void>;
|
|
17
|
+
export interface RemoveWatermarkOpts {
|
|
18
|
+
out?: string;
|
|
19
|
+
mask?: string;
|
|
20
|
+
overwrite?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export declare function removeWatermarkCmd(input: string, opts: RemoveWatermarkOpts): Promise<void>;
|
|
23
|
+
export interface UpscaleOpts {
|
|
24
|
+
out?: string;
|
|
25
|
+
scale?: string;
|
|
26
|
+
model?: string;
|
|
27
|
+
overwrite?: boolean;
|
|
28
|
+
}
|
|
29
|
+
export declare function upscaleCmd(input: string, opts: UpscaleOpts): Promise<void>;
|
|
@@ -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 } from '../local-bg.js';
|
|
7
|
+
import { upscaleLocal, UPSCALE_MODELS } from '../local-upscale.js';
|
|
8
|
+
import { detectWatermarkBoxes, inpaintLocal, maskFromBoxes } from '../local-watermark.js';
|
|
7
9
|
async function fileExists(path) {
|
|
8
10
|
try {
|
|
9
11
|
return (await stat(path)).isFile();
|
|
@@ -79,3 +81,50 @@ export async function removeBgCmd(input, opts) {
|
|
|
79
81
|
process.stderr.write(`✅ 抠图完成(${BG_MODELS[modelKey].label},用时 ${((Date.now() - start) / 1000).toFixed(1)}s,${fmtBytes(png.length)})\n`);
|
|
80
82
|
console.log(outPath);
|
|
81
83
|
}
|
|
84
|
+
export async function removeWatermarkCmd(input, opts) {
|
|
85
|
+
if (!(await fileExists(input)))
|
|
86
|
+
throw new Error(`文件不存在: ${input}`);
|
|
87
|
+
const outPath = opts.out || defaultOut(input, 'clean', 'png');
|
|
88
|
+
if ((await fileExists(outPath)) && !opts.overwrite) {
|
|
89
|
+
throw new Error(`输出已存在(用 --overwrite 覆盖或 --out 换路径): ${outPath}`);
|
|
90
|
+
}
|
|
91
|
+
const start = Date.now();
|
|
92
|
+
let mask;
|
|
93
|
+
if (opts.mask) {
|
|
94
|
+
if (!(await fileExists(opts.mask)))
|
|
95
|
+
throw new Error(`掩码文件不存在: ${opts.mask}`);
|
|
96
|
+
mask = opts.mask;
|
|
97
|
+
process.stderr.write('使用手工掩码,跳过自动定位\n');
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
// 纯像素启发式定位水印(零模型依赖),失败时明确指引改用 --mask
|
|
101
|
+
const boxes = await detectWatermarkBoxes(input);
|
|
102
|
+
if (!boxes.length)
|
|
103
|
+
throw new Error('自动定位没找到水印(角标式半透明水印通常可识别;复杂画面请用 --mask 手工指定)');
|
|
104
|
+
process.stderr.write(`定位到 ${boxes.length} 处水印:${boxes.map((b) => `(${b.x1},${b.y1})-(${b.x2},${b.y2})`).join(' ')}\n`);
|
|
105
|
+
mask = await maskFromBoxes(input, boxes);
|
|
106
|
+
}
|
|
107
|
+
const png = await inpaintLocal(input, mask);
|
|
108
|
+
await writeFile(outPath, png);
|
|
109
|
+
process.stderr.write(`✅ 去水印完成(用时 ${((Date.now() - start) / 1000).toFixed(1)}s,${fmtBytes(png.length)})\n`);
|
|
110
|
+
console.log(outPath);
|
|
111
|
+
}
|
|
112
|
+
export async function upscaleCmd(input, opts) {
|
|
113
|
+
if (!(await fileExists(input)))
|
|
114
|
+
throw new Error(`文件不存在: ${input}`);
|
|
115
|
+
const scale = opts.scale ? Number(opts.scale) : 4;
|
|
116
|
+
if (![2, 3, 4].includes(scale))
|
|
117
|
+
throw new Error('--scale 只支持 2 / 3 / 4');
|
|
118
|
+
const model = (opts.model || 'realesrgan-x4plus');
|
|
119
|
+
if (!(model in UPSCALE_MODELS))
|
|
120
|
+
throw new Error(`--model 只支持 ${Object.keys(UPSCALE_MODELS).join(' / ')}`);
|
|
121
|
+
const outPath = opts.out || defaultOut(input, `${scale}x`, 'png');
|
|
122
|
+
if ((await fileExists(outPath)) && !opts.overwrite) {
|
|
123
|
+
throw new Error(`输出已存在(用 --overwrite 覆盖或 --out 换路径): ${outPath}`);
|
|
124
|
+
}
|
|
125
|
+
const start = Date.now();
|
|
126
|
+
await upscaleLocal({ input, output: outPath, scale, model });
|
|
127
|
+
const size = (await stat(outPath)).size;
|
|
128
|
+
process.stderr.write(`✅ 超分完成(${UPSCALE_MODELS[model].label},${scale}x,用时 ${((Date.now() - start) / 1000).toFixed(1)}s,${fmtBytes(size)})\n`);
|
|
129
|
+
console.log(outPath);
|
|
130
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/** museav reverse —— 图片逆向(SCULPT 六要素反推 prompt)。
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* 默认走中台 API(快、稳定、不需本地模型);--local 可切本地 Ollama(需自备 qwen3-vl,
|
|
3
|
+
* 仅在用户显式要求时使用——本地大模型默认不拉起,不给用户的内存添负担)。
|
|
4
|
+
* client 懒构造:本地路成功就完全不碰中台凭证。 */
|
|
5
5
|
import type { StudioClient } from '../client.js';
|
|
6
6
|
export declare function reverse(getClient: () => StudioClient, input: string, opts?: {
|
|
7
7
|
api?: boolean;
|
|
8
|
+
local?: boolean;
|
|
8
9
|
}): Promise<void>;
|
package/dist/commands/reverse.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { checkLocalVlm, reverseLocally, LOCAL_VLM_MODEL } from '../local-vision.js';
|
|
2
2
|
export async function reverse(getClient, input, opts = {}) {
|
|
3
3
|
const isUrl = /^https?:\/\//.test(input);
|
|
4
|
-
|
|
4
|
+
// 本地路只在用户显式 --local 且输入是本地文件时尝试;服务不可用给出指引后回落 API
|
|
5
|
+
if (opts.local && !isUrl) {
|
|
5
6
|
const status = await checkLocalVlm();
|
|
6
7
|
if (status.running && status.modelPresent) {
|
|
7
8
|
try {
|
|
@@ -19,7 +20,7 @@ export async function reverse(getClient, input, opts = {}) {
|
|
|
19
20
|
process.stderr.write(`⚠ 本地读图不可用(${status.reason}),回落中台 API —— 速度较慢,请耐心等待\n`);
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
|
-
else if (
|
|
23
|
+
else if (opts.local && isUrl) {
|
|
23
24
|
process.stderr.write(`ℹ URL 输入走中台 API(本地路只收文件路径)\n`);
|
|
24
25
|
}
|
|
25
26
|
const client = getClient();
|
package/dist/index.js
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 } from './commands/projects.js';
|
|
20
20
|
import { imageToTemplate } from './commands/image-to-template.js';
|
|
21
21
|
import { upload } from './commands/upload.js';
|
|
@@ -184,6 +184,21 @@ program
|
|
|
184
184
|
.option('--model <name>', 'isnet(默认,质量优先)/ u2net')
|
|
185
185
|
.option('--overwrite', '允许覆盖已存在的输出文件')
|
|
186
186
|
.action(asyncRun((input, opts) => removeBgCmd(input, opts)));
|
|
187
|
+
program
|
|
188
|
+
.command('upscale <file>')
|
|
189
|
+
.description('本地超分放大(Real-ESRGAN + Vulkan GPU,免登录):默认 4x 输出 PNG。首次使用自动下载引擎与模型(~65MB,缓存 ~/.museav-bin 与 ~/.museav-models)')
|
|
190
|
+
.option('--out <path>', '输出路径(默认 <名>-<N>x.png)')
|
|
191
|
+
.option('--scale <n>', '放大倍数 2 / 3 / 4,默认 4')
|
|
192
|
+
.option('--model <name>', 'realesrgan-x4plus(通用照片,默认)/ realesrgan-x4plus-anime(插画动漫)')
|
|
193
|
+
.option('--overwrite', '允许覆盖已存在的输出文件')
|
|
194
|
+
.action(asyncRun((input, opts) => upscaleCmd(input, opts)));
|
|
195
|
+
program
|
|
196
|
+
.command('remove-watermark <file>')
|
|
197
|
+
.description('本地去水印(免登录):纯像素启发式自动定位水印 → LaMa 掩码修复,零模型依赖。首次使用自动下载修复模型(~200MB);复杂画面用 --mask 手工指定(白=去除区)')
|
|
198
|
+
.option('--out <path>', '输出路径(默认 <名>-clean.png)')
|
|
199
|
+
.option('--mask <file>', '手工掩码图(白色=要去除的区域),跳过自动定位')
|
|
200
|
+
.option('--overwrite', '允许覆盖已存在的输出文件')
|
|
201
|
+
.action(asyncRun((input, opts) => removeWatermarkCmd(input, opts)));
|
|
187
202
|
// 工作区(项目)与项目素材库:平台 → 账户 → 工作区三层归属,素材挂工作区
|
|
188
203
|
const projectsCmd = program
|
|
189
204
|
.command('projects')
|
|
@@ -214,8 +229,9 @@ assetsCmd
|
|
|
214
229
|
.action(withClient((client, id) => removeAsset(client, { id })));
|
|
215
230
|
program
|
|
216
231
|
.command('reverse <input>')
|
|
217
|
-
.description('读图:反推 SCULPT prompt,stdout 输出英文 prompt
|
|
218
|
-
.option('--api', '
|
|
232
|
+
.description('读图:反推 SCULPT prompt,stdout 输出英文 prompt。默认走中台 API(需登录);--local 显式切本地 Ollama(需自备 qwen3-vl)。只读图;要做成模板用 image-to-template')
|
|
233
|
+
.option('--api', '强制走中台 API(默认路径)')
|
|
234
|
+
.option('--local', '改用本地 Ollama 读图(需先 ollama pull qwen3-vl:8b;本地不可用时回落 API)')
|
|
219
235
|
.action(withLazyClient((getClient, input, opts) => reverse(getClient, input, opts)));
|
|
220
236
|
program
|
|
221
237
|
.command('image-to-template <input>')
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const UPSCALE_MODELS: {
|
|
2
|
+
readonly 'realesrgan-x4plus': {
|
|
3
|
+
readonly label: '通用照片(默认)';
|
|
4
|
+
};
|
|
5
|
+
readonly 'realesrgan-x4plus-anime': {
|
|
6
|
+
readonly label: '插画/动漫';
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
export type UpscaleModel = keyof typeof UPSCALE_MODELS;
|
|
10
|
+
/** 首次使用时准备好二进制与模型,返回 { exe, modelDir }。之后直接走缓存 */
|
|
11
|
+
export declare function ensureUpscaleRuntime(): Promise<{
|
|
12
|
+
exe: string;
|
|
13
|
+
modelDir: string;
|
|
14
|
+
}>;
|
|
15
|
+
/** 超分主流程:返回输出文件的字节数组由引擎直写磁盘,这里只负责调度 */
|
|
16
|
+
export declare function upscaleLocal(opts: {
|
|
17
|
+
input: string;
|
|
18
|
+
output: string;
|
|
19
|
+
scale: number;
|
|
20
|
+
model: UpscaleModel;
|
|
21
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,115 @@
|
|
|
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
|
+
// 锁定已实测的版本(20251207-174704,macOS universal 实测可用),升级要重新过测试
|
|
17
|
+
const UPSCAYL_TAG = '20251207-174704';
|
|
18
|
+
const UPSCAYL_BASE = `https://github.com/upscayl/upscayl-ncnn/releases/download/${UPSCAYL_TAG}/upscayl-bin-${UPSCAYL_TAG}`;
|
|
19
|
+
// 模型从 Real-ESRGAN 官方 release 的 zip 里取(只取需要的两个,别拖全量)
|
|
20
|
+
const MODEL_ZIP = 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesrgan-ncnn-vulkan-20220424-macos.zip';
|
|
21
|
+
export const UPSCALE_MODELS = {
|
|
22
|
+
'realesrgan-x4plus': { label: '通用照片(默认)' },
|
|
23
|
+
'realesrgan-x4plus-anime': { label: '插画/动漫' },
|
|
24
|
+
};
|
|
25
|
+
const BIN_DIR = join(homedir(), '.museav-bin', 'upscayl');
|
|
26
|
+
const MODEL_DIR = join(homedir(), '.museav-models');
|
|
27
|
+
function platformAsset() {
|
|
28
|
+
if (process.platform === 'win32')
|
|
29
|
+
return { zip: `${UPSCAYL_BASE}-windows.zip`, exe: 'upscayl-bin.exe' };
|
|
30
|
+
if (process.platform === 'darwin')
|
|
31
|
+
return { zip: `${UPSCAYL_BASE}-macos.zip`, exe: 'upscayl-bin' };
|
|
32
|
+
return { zip: `${UPSCAYL_BASE}-linux.zip`, exe: 'upscayl-bin' };
|
|
33
|
+
}
|
|
34
|
+
async function exists(path) {
|
|
35
|
+
try {
|
|
36
|
+
await stat(path);
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async function download(url, dest, label) {
|
|
44
|
+
const resp = await fetch(url);
|
|
45
|
+
if (!resp.ok || !resp.body)
|
|
46
|
+
throw new Error(`${label} 下载失败 HTTP ${resp.status}`);
|
|
47
|
+
const total = Number(resp.headers.get('content-length') || 0);
|
|
48
|
+
const chunks = [];
|
|
49
|
+
let got = 0;
|
|
50
|
+
const reader = resp.body.getReader();
|
|
51
|
+
for (;;) {
|
|
52
|
+
const { done, value } = await reader.read();
|
|
53
|
+
if (done)
|
|
54
|
+
break;
|
|
55
|
+
chunks.push(Buffer.from(value));
|
|
56
|
+
got += value.length;
|
|
57
|
+
if (total)
|
|
58
|
+
process.stderr.write(` ${label} ${(got / 1048576).toFixed(1)}/${(total / 1048576).toFixed(0)}MB\r`);
|
|
59
|
+
}
|
|
60
|
+
process.stderr.write('\n');
|
|
61
|
+
await writeFile(dest, Buffer.concat(chunks));
|
|
62
|
+
}
|
|
63
|
+
/** 首次使用时准备好二进制与模型,返回 { exe, modelDir }。之后直接走缓存 */
|
|
64
|
+
export async function ensureUpscaleRuntime() {
|
|
65
|
+
const { zip, exe } = platformAsset();
|
|
66
|
+
const exePath = join(BIN_DIR, exe);
|
|
67
|
+
const modelDir = join(MODEL_DIR, 'realesrgan');
|
|
68
|
+
const paramPath = join(modelDir, 'realesrgan-x4plus.param');
|
|
69
|
+
if (!(await exists(exePath))) {
|
|
70
|
+
await mkdir(BIN_DIR, { recursive: true });
|
|
71
|
+
const zipPath = join(BIN_DIR, `dl-${process.platform}.zip`);
|
|
72
|
+
process.stderr.write(`↓ 首次使用,下载超分引擎(~15MB,一次性,缓存到 ${BIN_DIR})...\n`);
|
|
73
|
+
await download(zip, zipPath, '引擎');
|
|
74
|
+
// tar -xf 解压:Win10+/macOS/Linux 自带,比依赖 unzip 稳
|
|
75
|
+
await run('tar', ['-xf', zipPath, '-C', BIN_DIR], { windowsHide: true });
|
|
76
|
+
// zip 里是 upscayl-bin-<tag>-<os>/upscayl-bin,拍平到 BIN_DIR
|
|
77
|
+
const { readdir } = await import('node:fs/promises');
|
|
78
|
+
for (const entry of await readdir(BIN_DIR, { withFileTypes: true })) {
|
|
79
|
+
if (entry.isDirectory()) {
|
|
80
|
+
const { rename, readdir: rd } = await import('node:fs/promises');
|
|
81
|
+
for (const f of await rd(join(BIN_DIR, entry.name))) {
|
|
82
|
+
await rename(join(BIN_DIR, entry.name, f), join(BIN_DIR, f));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (process.platform !== 'win32')
|
|
87
|
+
await chmod(exePath, 0o755);
|
|
88
|
+
if (!(await exists(exePath)))
|
|
89
|
+
throw new Error(`解压后未找到 ${exe},请检查 ${BIN_DIR}`);
|
|
90
|
+
const { unlink } = await import('node:fs/promises');
|
|
91
|
+
await unlink(zipPath).catch(() => { });
|
|
92
|
+
}
|
|
93
|
+
if (!(await exists(paramPath))) {
|
|
94
|
+
await mkdir(modelDir, { recursive: true });
|
|
95
|
+
const zipPath = join(MODEL_DIR, 'dl-models.zip');
|
|
96
|
+
process.stderr.write('↓ 首次使用,下载超分模型(~50MB,一次性)...\n');
|
|
97
|
+
await download(MODEL_ZIP, zipPath, '模型');
|
|
98
|
+
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 });
|
|
99
|
+
const { rename, rm } = await import('node:fs/promises');
|
|
100
|
+
for (const f of ['realesrgan-x4plus.param', 'realesrgan-x4plus.bin', 'realesrgan-x4plus-anime.param', 'realesrgan-x4plus-anime.bin']) {
|
|
101
|
+
await rename(join(MODEL_DIR, 'models', f), join(modelDir, f)).catch(() => { });
|
|
102
|
+
}
|
|
103
|
+
await rm(join(MODEL_DIR, 'models'), { recursive: true, force: true }).catch(() => { });
|
|
104
|
+
await rm(zipPath, { force: true }).catch(() => { });
|
|
105
|
+
if (!(await exists(paramPath)))
|
|
106
|
+
throw new Error(`模型解压失败,请检查 ${modelDir}`);
|
|
107
|
+
}
|
|
108
|
+
return { exe: exePath, modelDir };
|
|
109
|
+
}
|
|
110
|
+
/** 超分主流程:返回输出文件的字节数组由引擎直写磁盘,这里只负责调度 */
|
|
111
|
+
export async function upscaleLocal(opts) {
|
|
112
|
+
const { exe, modelDir } = await ensureUpscaleRuntime();
|
|
113
|
+
// 不走 shell 拼接;路径原样传参,空格/中文路径都安全
|
|
114
|
+
await run(exe, ['-i', opts.input, '-o', opts.output, '-s', String(opts.scale), '-n', opts.model, '-m', modelDir], { windowsHide: true });
|
|
115
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface Bbox {
|
|
2
|
+
x1: number;
|
|
3
|
+
y1: number;
|
|
4
|
+
x2: number;
|
|
5
|
+
y2: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* 纯像素启发式水印定位(零模型依赖)。
|
|
9
|
+
* 原理:半透明水印(角标/文字)是「低对比、高频、铺在大片区域的细碎纹理」,
|
|
10
|
+
* 把四角区域做中值模糊后与原图差分,叠字区会出现稳定的高差值像素团。
|
|
11
|
+
* 判定保守:角区差分像素占比在 [0.3%, 8%] 才算水印(太少=没叠字,
|
|
12
|
+
* 太多=画面本身纹理复杂,不硬修);坐标 0-1000 归一化输出。
|
|
13
|
+
* 误检最坏是 LaMa 重绘一块(轻微损伤),另有 --mask 手工精确兜底。
|
|
14
|
+
*/
|
|
15
|
+
export declare function detectWatermarkBoxes(imagePath: string): Promise<Bbox[]>;
|
|
16
|
+
/** 掩码修复主流程:mask 白色=要去除(Buffer 或文件路径);输出 PNG Buffer。
|
|
17
|
+
* 模型是固定 512×512 输入——整图缩进去会毁分辨率,所以按掩码连通域逐块处理:
|
|
18
|
+
* 裁出带边距的局部 → letterbox 进 512 修复 → 只把掩码内的像素贴回原图。 */
|
|
19
|
+
export declare function inpaintLocal(imagePath: string, mask: Buffer | string): Promise<Buffer>;
|
|
20
|
+
/** 从检测框生成掩码 Buffer(框外扩 2%,白=去除区) */
|
|
21
|
+
export declare function maskFromBoxes(imagePath: string, boxes: Bbox[]): Promise<Buffer>;
|