Face Scan


Python
python -m venv face_scan
face_scan\Scripts\activate

安装依赖(GPU 版)
pip install –upgrade pip
pip install insightface onnxruntime-gpu opencv-python numpy tqdm

准备本人的人脸特征
1. 目录结构建议

project/ │
├─ me/ # 放你的 5–10 张个人照片
│ ├─ 1.jpg
│ ├─ 2.jpg
├─ scan_and_copy.py
├─ my_face.npy

生成人脸特征(一次性执行)
import cv2
import numpy as np
from insightface.app import FaceAnalysis
from pathlib import Path

app = FaceAnalysis(name=”buffalo_l”, providers=[“CUDAExecutionProvider”])
app.prepare(ctx_id=0, det_size=(640, 640))

embeddings = []

for img_path in Path(“me”).glob(“*”):
img = cv2.imread(str(img_path))
faces = app.get(img)
if faces:
embeddings.append(faces[0].embedding)

if not embeddings:
raise RuntimeError(“未检测到你的人脸”)

my_face = np.mean(embeddings, axis=0)
np.save(“my_face.npy”, my_face)
print(“本人特征已生成:my_face.npy”)

扫描文件夹并拷贝高度疑似本人照片
import os
import shutil
import cv2
import numpy as np
from insightface.app import FaceAnalysis
from PIL import Image
import pillow_heif

# 注册 HEIC / HEIF 支持

pillow_heif.register_heif_opener()

#================== 配置区 ==================

SCAN_ROOT = r”C:\Users\Downloads” # ← 改成你的源目录 / 公共盘
OUTPUT_ROOT = r”C:\Picture”

#分级阈值(ArcFace 经验值)

THRESH_SURE = 0.38 # 几乎 100% 是你
THRESH_LIKELY = 0.33 # 高概率是你
THRESH_REVIEW = 0.30 # 值得人工看一眼

image_ext = (
“.jpg”, “.jpeg”, “.png”, “.bmp”, “.tiff”,
“.jfif”, “.heic”, “.heif”
)

#===========================================
#创建输出目录

for sub in [“sure”, “likely”, “review”]:
os.makedirs(os.path.join(OUTPUT_ROOT, sub), exist_ok=True)

#加载本人特征

my_face = np.load(“my_face.npy”)

#初始化 InsightFace(CPU)

app = FaceAnalysis(
name=”buffalo_l”,
providers=[“CPUExecutionProvider”]
)
app.prepare(ctx_id=-1, det_size=(640, 640))

def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

def read_image_any_format(path):
“””
支持 JPG / PNG / HEIC / HEIF
返回 OpenCV BGR 图像
“””
try:
img = Image.open(path).convert(“RGB”)
return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
except Exception:
return None

#================== 扫描开始 ==================

for root, _, files in os.walk(SCAN_ROOT):
for file in files:
if not file.lower().endswith(image_ext):
continue

img_path = os.path.join(root, file)
img = read_image_any_format(img_path)

if img is None:
continue

faces = app.get(img)
if not faces:
continue

best_score = -1.0
for face in faces:
score = cosine_similarity(face.embedding, my_face)
if score > best_score:
best_score = score

if best_score >= THRESH_SURE:
subdir = “sure”
elif best_score >= THRESH_LIKELY:
subdir = “likely”
elif best_score >= THRESH_REVIEW:
subdir = “review”
else:
continue

dst = os.path.join(
OUTPUT_ROOT,
subdir,
f”{best_score:.3f}_{file}”
)

shutil.copy2(img_path, dst)
print(f”[COPY] {best_score:.3f} -> {dst}”)

print(“扫描完成(支持 HEIC,CPU)”)

这套脚本已经可以:

  • ✔ 扫 JPG + iPhone HEIC 原图
  • ✔ 支持多人合影(只要有你就拷)
  • ✔ 不漏掉你(低阈值兜底)
  • ✔ 自动分级,人工成本极低
  • ✔ 完全本地、可审计、可解释