diff --git a/.gitignore b/.gitignore index 392de40..ad537d3 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,6 @@ hf-cache/ piper-models/ ai-models/ waifu2x/ +face-models/ *.log .venv-hf/ diff --git a/Dockerfile b/Dockerfile index 2c854de..2e8e83c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,10 @@ RUN apt-get update \ # models are pre-downloaded on the host (build env has no internet) RUN pip install --no-cache-dir piper-tts faster-whisper RUN pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple "rembg[cpu]" +# AI 换脸: insightface(纯Python wheel 免编译) + buffalo_l人脸检测 + inswapper_128换脸模型(宿主已预置) +RUN pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple insightface COPY hf-cache /root/.cache/huggingface +COPY face-models /app/face-models COPY piper-models /app/piper-models # AI 图片: u2net 抠图模型(预下载) + waifu2x-ncnn-vulkan 无损放大(CPU) COPY ai-models/u2net.onnx /root/.u2net/u2net.onnx diff --git a/server.py b/server.py index 2b37904..68fcd90 100644 --- a/server.py +++ b/server.py @@ -68,7 +68,7 @@ CF_DAILY_LIMIT = int(os.environ.get("CF_DAILY_LIMIT", "173")) CF_DAILY_FILE = os.environ.get("CF_DAILY_FILE", "/app/data/daily_gen_cf.json") DAY_MS = 24 * 60 * 60 * 1000 TZ_OFFSET_MS = 8 * 60 * 60 * 1000 # 北京时间 UTC+8 —— 重置对齐自然日 0 点 -PROTECTED_TOOLS = {"translator", "image-ocr", "pdf-ocr", "ai-retouch", "doc-convert", "image-tools", "pdf-tools", "video-tools", "audio-tools", "text-to-image", "ai-image-tools", "copywriting", "screenshot-qa", "video-subtitle", "ai-gen", "ai-text-edit"} +PROTECTED_TOOLS = {"translator", "image-ocr", "pdf-ocr", "ai-retouch", "doc-convert", "image-tools", "pdf-tools", "video-tools", "audio-tools", "text-to-image", "ai-image-tools", "copywriting", "screenshot-qa", "video-subtitle", "ai-gen", "ai-text-edit", "face-swap", "poster-maker"} MACHINE_RE = re.compile(r"^\d{10,16}$") # verify() messages that unconditionally block activation @@ -711,6 +711,51 @@ def check(machine: str, tool: str) -> int: return 200 if trial_allowed(machine, tool) else 403 +# ---- AI 换脸引擎:insightface buffalo_l 人脸检测 + inswapper_128 换脸(本机 CPU 推理) ---- +_FACE_LOCK = threading.Lock() +_FACE_APP = None +_FACE_SWAP = None +_FACE_MODELS_DIR = os.environ.get("FACE_MODELS_DIR", "/app/face-models") + + +def _face_engine(): + """懒加载换脸引擎(线程安全;首次调用约 1~2s,之后常驻内存复用)。""" + global _FACE_APP, _FACE_SWAP + if _FACE_APP is None: + from insightface.app import FaceAnalysis + from insightface import model_zoo + app = FaceAnalysis(name="buffalo_l", root=_FACE_MODELS_DIR, providers=["CPUExecutionProvider"]) + app.prepare(ctx_id=0, det_size=(640, 640)) + swap = model_zoo.get_model(os.path.join(_FACE_MODELS_DIR, "inswapper_128.onnx"), + providers=["CPUExecutionProvider"]) + _FACE_APP, _FACE_SWAP = app, swap + return _FACE_APP, _FACE_SWAP + + +def _largest_face(faces): + """多张脸时取面积最大的一张(换脸默认作用于最显眼的人脸)。""" + return max(faces, key=lambda f: float((f.bbox[2] - f.bbox[0]) * (f.bbox[3] - f.bbox[1]))) + + +def _face_swap_run(src_img, dst_img): + """源人脸 -> 目标照片。返回 (jpg字节|None, 错误信息, 耗时ms)。加锁串行避免并发吃满 CPU。""" + import cv2 as _cv2 + t0 = time.time() + with _FACE_LOCK: + app, swap = _face_engine() + src_faces = app.get(src_img) + dst_faces = app.get(dst_img) + if not src_faces: + return None, "源图中未检测到人脸,请换一张清晰的正脸照片", 0 + if not dst_faces: + return None, "目标图中未检测到人脸,请换一张含人物的照片", 0 + out = swap.get(dst_img, _largest_face(dst_faces), _largest_face(src_faces), paste_back=True) + ok, buf = _cv2.imencode(".jpg", out, [int(_cv2.IMWRITE_JPEG_QUALITY), 92]) + if not ok: + return None, "结果编码失败", 0 + return buf.tobytes(), None, int((time.time() - t0) * 1000) + + def _account_license_ok(machine: str) -> bool: """True when this device logged into an authorized account. @@ -3124,6 +3169,9 @@ class Handler(BaseHTTPRequestHandler): if parsed.path == "/ai/edit-image": self._handle_edit_image() return + if parsed.path == "/ai/face-swap": + self._handle_face_swap() + return if parsed.path == "/ai/seamless-edit": self._handle_seamless_edit() return @@ -3347,7 +3395,7 @@ class Handler(BaseHTTPRequestHandler): msg = str(err or d.get("message") or "") raise RuntimeError(msg[:300] or "上游未返回图片") - def _persist_gen_image(self, machine, d, model, prompt, w, h): + def _persist_gen_image(self, machine, d, model, prompt, w, h, kind="gen"): """把生成结果落盘并写入该机器的历史索引;返回站内图片 URL(任何失败返回空串)。""" try: if not self._gen_machine_ok(machine): @@ -3366,6 +3414,14 @@ class Handler(BaseHTTPRequestHandler): raw = rr.read() if not raw or len(raw) < 200: return "" + if not (w and h): + try: + from PIL import Image as _PIL + import io as _io + _im = _PIL.open(_io.BytesIO(raw)) + w, h = _im.size + except Exception: + w, h = 0, 0 if raw[:8] == b"\x89PNG\r\n\x1a\n": ext = ".png" elif raw[:2] == b"\xff\xd8": @@ -3394,6 +3450,7 @@ class Handler(BaseHTTPRequestHandler): idx = [] idx.insert(0, {"id": iid, "file": fname, "at": int(time.time()), "model": str(model)[:60], "prompt": str(prompt)[:500], + "kind": str(kind or "gen")[:20], "w": w, "h": h}) while len(idx) > 100: old = idx.pop() or {} @@ -3872,6 +3929,94 @@ class Handler(BaseHTTPRequestHandler): canvas.paste(im2, ((tw - im2.width) // 2, (th - im2.height) // 2)) return canvas + def _handle_face_swap(self): + """AI换脸(授权功能):源人脸照片 -> 目标照片,本机 CPU 推理,结果走 /ai/img 站内图链路。""" + machine = self.headers.get("X-Machine-Id", "") + code = check(machine, "face-swap") + if code != 200: + self._reply(403, json.dumps({"code": 403, "message": "AI换脸是授权功能:试用期内可免费体验,正式使用请先激活卡密"}, + ensure_ascii=False).encode("utf-8"), "application/json") + return + log_usage(machine, "face-swap") + ctype = self.headers.get("Content-Type") or "" + if "multipart/form-data" not in ctype: + self._reply(400, json.dumps({"code": 400, "message": "需要 multipart/form-data"}, ensure_ascii=False).encode("utf-8"), "application/json") + return + try: + form = cgi.FieldStorage(fp=self.rfile, headers=self.headers, + environ={"REQUEST_METHOD": "POST", "CONTENT_TYPE": ctype}) + except Exception as e: # noqa: BLE001 + self._reply(400, json.dumps({"code": 400, "message": f"表单解析失败: {e}"}, ensure_ascii=False).encode("utf-8"), "application/json") + return + if str(form.getvalue("consent") or "") != "1": + self._reply(400, json.dumps({"code": 400, "message": "请先勾选「我承诺仅使用已获本人授权的照片进行换脸」"}, + ensure_ascii=False).encode("utf-8"), "application/json") + return + try: + src_raw = form["source"].file.read() if "source" in form else b"" + dst_raw = form["target"].file.read() if "target" in form else b"" + except Exception: + src_raw, dst_raw = b"", b"" + if not src_raw or not dst_raw: + self._reply(400, json.dumps({"code": 400, "message": "请同时上传源人脸照片与目标照片"}, ensure_ascii=False).encode("utf-8"), "application/json") + return + if len(src_raw) > 15 * 1024 * 1024 or len(dst_raw) > 15 * 1024 * 1024: + self._reply(400, json.dumps({"code": 400, "message": "单张图片不能超过 15MB"}, ensure_ascii=False).encode("utf-8"), "application/json") + return + import cv2 as _cv2 + import numpy as _np + src_img = _cv2.imdecode(_np.frombuffer(src_raw, _np.uint8), _cv2.IMREAD_COLOR) + dst_img = _cv2.imdecode(_np.frombuffer(dst_raw, _np.uint8), _cv2.IMREAD_COLOR) + if src_img is None or dst_img is None: + self._reply(400, json.dumps({"code": 400, "message": "图片解码失败,请上传 JPG/PNG 等常见格式"}, ensure_ascii=False).encode("utf-8"), "application/json") + return + try: + out_jpg, msg, ms = _face_swap_run(src_img, dst_img) + except Exception as e: # noqa: BLE001 + print(f"[face-swap] failed: {e}", flush=True) + self._reply(500, json.dumps({"code": 500, "message": f"换脸失败: {e}"}, ensure_ascii=False).encode("utf-8"), "application/json") + return + if out_jpg is None: + self._reply(400, json.dumps({"code": 400, "message": msg}, ensure_ascii=False).encode("utf-8"), "application/json") + return + try: + gen_dir = os.path.join(DATA_DIR, "gen_images", machine) + os.makedirs(gen_dir, exist_ok=True) + iid = f"{int(time.time())}_{uuid.uuid4().hex[:8]}" + with open(os.path.join(gen_dir, iid + ".jpg"), "wb") as f: + f.write(out_jpg) + idx_path = os.path.join(gen_dir, "index.json") + with GEN_IMG_LOCK: + idx = [] + if os.path.exists(idx_path): + try: + idx = json.load(open(idx_path, encoding="utf-8")) + if not isinstance(idx, list): + idx = [] + except Exception: + idx = [] + idx.insert(0, {"id": iid, "file": iid + ".jpg", "at": int(time.time()), + "model": "AI换脸", "prompt": "face-swap", + "w": int(dst_img.shape[1]), "h": int(dst_img.shape[0])}) + while len(idx) > 100: + old = idx.pop() or {} + fn = str(old.get("file") or "") + if fn and "/" not in fn and "\\" not in fn: + try: + os.remove(os.path.join(gen_dir, fn)) + except Exception: + pass + with open(idx_path, "w", encoding="utf-8") as f: + json.dump(idx, f, ensure_ascii=False) + except Exception as e: # noqa: BLE001 + print(f"[face-swap] persist failed: {e}", flush=True) + self._reply(500, json.dumps({"code": 500, "message": "结果保存失败"}, ensure_ascii=False).encode("utf-8"), "application/json") + return + body = json.dumps({"code": 200, "message": "ok", + "data": {"url": f"/relay/ai/img/{machine}/{iid}", "elapsed_ms": ms}}, + ensure_ascii=False).encode("utf-8") + self._reply(200, body, "application/json") + def _handle_seamless_edit(self): machine = self.headers.get("X-Machine-Id", "") if check(machine, "ai-text-edit") != 200: