# Image Intelligence Platform

> 图片智能处理全平台 — 3阶段识别流水线（OCR → 感知哈希 → 知识库）+ 质量检测 + 批量生成

- Skill: `openclaw-cn-dev/image-intelligence-platform` (Agent Skill, multi-file: 3 files)
- Install (CLI): `npx get-skill add openclaw-cn-dev/image-intelligence-platform`
- Raw SKILL.md: https://api.skillmd.com/api/skills/openclaw-cn-dev/image-intelligence-platform/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: openclaw-cn-dev (https://skillmd.com/u/openclaw-cn-dev)
- Updated: 2026-11-14
- Page: https://skillmd.com/skills/openclaw-cn-dev/image-intelligence-platform

---


# 图片智能处理平台 v2.0

## 能力矩阵

| 模块 | 功能 | 状态 |
|------|------|------|
| **解析层** | OCR 文字提取 | ✅ |
| | 编号智能识别（0/O 混淆修正） | ✅ |
| | 布局结构分析（标题区/内容区/配色区） | ✅ |
| | 感知哈希相似度分组 | ✅ |
| **理解层** | 图片质量评分（清晰度/对比度/可读性） | ✅ |
| | 设计风格分类（科技风/极简风/手绘风） | ✅ |
| | 配色方案提取（主色/辅色/强调色） | ✅ |
| | 元素完整性检测 | ✅ |
| **生成层** | 豆包生图 API 批量调用 | ✅ |
| | Prompt 模板引擎 | ✅ |
| | 生成参数调优 | ✅ |
| **管控层** | 重复图片自动去重 | ✅ |
| | 不合格图片自动打回 | ✅ |
| | 批量重命名标准化 | ✅ |
| | 输出报告生成 | ✅ |

---

## 模块 1：图片智能解析引擎

### 1.1 全区域 OCR 解析

不仅仅读顶部，而是按布局分区读取：

```python
def analyze_layout(img_path):
    """分析图片布局，返回各区域坐标"""
    img = Image.open(img_path)
    w, h = img.size
    
    return {
        "header": (0, 0, w, int(h * 0.25)),      # 顶部：编号+标题
        "content": (0, int(h*0.25), w, int(h*0.7)),  # 中部：内容
        "footer": (0, int(h*0.7), w, h),         # 底部：色值+领域
    }

def ocr_region(img, region_bbox):
    """OCR 读取指定区域"""
    crop = img.crop(region_bbox)
    crop.save("/tmp/_ocr_region.png")
    return subprocess.check_output(["/tmp/ocr", "/tmp/_ocr_region.png"], text=True)
```

### 1.2 编号智能识别（解决 0/O 混淆）

```python
def robust_number_detection(ocr_text, img=None):
    """
    增强版编号识别，结合字形特征
    0 vs O 判定规则：
    - 上下文是数字 → 0
    - 上下文是英文单词 → O
    - 结合图片像素：正圆形 vs 椭圆
    """
    import re
    
    # 第一阶段：正则粗提取
    candidates = re.findall(r'[Tt]\s*([0-9A-Za-z]{3})', ocr_text)
    
    if not candidates:
        return None
    
    raw = candidates[0].upper()
    
    # 第二阶段：字符级修正
    correction_map = {
        'O': '0', 'ｏ': '0',
        'I': '1', 'ｌ': '1',
        'Z': '2',
        'S': '5',
        'B': '8'
    }

    # 中文 OCR 常见错误（标题修正）
    title_correction_map = {
        'Al': 'AI',
        '丁具': '工具',
        '1则化': '正则化',
        '多智能休': '多智能体',
        '湜示词': '提示词',
    }
    ```

    完整的术语知识库和 OCR 错误修正表参见 `ai-flashcard-batch-rename` skill 的 `references/terminology_knowledge_base.md`。
    
    result = []
    for c in raw:
        result.append(correction_map.get(c, c))
    
    final = ''.join(result)
    
    # 第三阶段：验证必须是纯 3 位数字
    if re.match(r'^\d{3}$', final):
        return final
    return None
```

### 1.3 感知哈希相似度分组

解决编号识别失败的图片自动归类：

```python
import imagehash
from collections import defaultdict

def group_by_similarity(image_paths, threshold=0.92):
    """
    按图片相似度分组，相同布局/设计的归为一组
    threshold: 0.0-1.0，越高越严格
    - 0.95: 极严格，几乎完全相同才分组
    - 0.92: 默认，实测最佳，布局一致即可
    - 0.85: 宽松，风格类似就分组
    """
    hashes = {}
    for path in image_paths:
        h = imagehash.dhash(Image.open(path), hash_size=16)
        hashes[path] = h
    
    groups = defaultdict(list)
    grouped = set()
    
    for i, path1 in enumerate(image_paths):
        if path1 in grouped:
            continue
            
        h1 = hashes[path1]
        current_group = [path1]
        grouped.add(path1)
        
        for path2 in image_paths[i+1:]:
            if path2 in grouped:
                continue
                
            h2 = hashes[path2]
            similarity = 1 - (h1 - h2) / len(h1.hash) ** 2
            
            if similarity >= threshold:
                current_group.append(path2)
                grouped.add(path2)
        
        groups[f"group_{len(groups):03d}"] = current_group
    
    return dict(groups)
```

## 模块 1.4：知识库匹配（标题反推编号）

当 OCR 无法识别编号但能识别标题时，这是**提升识别率最有效的步骤**（实测 +50%）。

```python
# 标题 -> 编号 映射表（随项目积累扩展）
title_to_num = {
    "机器学习": "002",
    "深度学习": "003",
    "大语言模型": "004",
    # ... 70+ 条目（参见 ai-flashcard-batch-rename skill）
}

def match_by_knowledge_base(results, knowledge_base):
    """用知识库补全识别失败的条目"""
    for r in results:
        if r['final_num'] is None:
            title = r['final_title']
            # 精确匹配
            if title in knowledge_base:
                r['final_num'] = knowledge_base[title]
                r['num_source'] = 'kb_exact'
            # 模糊匹配：前 4 个字一致即可
            else:
                for known_title, known_num in knowledge_base.items():
                    if known_title[:4] in title:
                        r['final_num'] = known_num
                        r['num_source'] = 'kb_fuzzy'
                        break
    return results
```

**知识库构建策略**：
- 先处理 OCR 成功的图片
- 自动提取 标题-编号 对加入知识库
- 每批处理后，知识库自动增长

### 2.1 质量评分算法

```python
def calculate_quality_score(img_path):
    """
    综合质量评分 0-100
    维度：清晰度、对比度、文字可读性、色彩一致性
    """
    import numpy as np
    
    img = Image.open(img_path)
    img_gray = img.convert('L')
    arr = np.array(img_gray)
    
    score = 0
    
    # 1. 清晰度（拉普拉斯方差）
    from scipy import ndimage
    laplacian = ndimage.laplace(arr)
    clarity = np.var(laplacian)
    clarity_score = min(100, clarity / 50) * 30  # 权重 30%
    
    # 2. 对比度（标准差）
    contrast = np.std(arr)
    contrast_score = min(100, contrast / 30) * 25  # 权重 25%
    
    # 3. 亮度分布（避免过暗/过亮）
    mean_bright = np.mean(arr)
    if 80 <= mean_bright <= 200:
        brightness_score = 20  # 权重 20%
    else:
        brightness_score = max(0, 20 - abs(mean_bright - 140) / 3)
    
    # 4. 色彩完整性（检测是否有缺失色块）
    color_score = check_color_integrity(img) * 25  # 权重 25%
    
    total_score = clarity_score + contrast_score + brightness_score + color_score
    
    return {
        "total": round(total_score, 1),
        "clarity": round(clarity_score, 1),
        "contrast": round(contrast_score, 1),
        "brightness": round(brightness_score, 1),
        "color_integrity": round(color_score, 1),
        "pass": total_score >= 70
    }

def check_color_integrity(img):
    """检测底部色值区域是否完整"""
    w, h = img.size
    footer = img.crop((0, int(h*0.85), w, h))
    
    # 转换为 HSV 检测纯黑/纯白异常
    footer_arr = np.array(footer.convert('HSV'))
    sat_mean = np.mean(footer_arr[:, :, 1])
    
    # 饱和度太低说明可能渲染失败
    if sat_mean < 20:
        return 0.3  # 严重问题
    elif sat_mean < 50:
        return 0.7  # 轻微问题
    return 1.0
```

### 2.2 自动质量筛选

```python
def batch_quality_check(directory, threshold=70):
    """批量检测图片质量，分离合格/不合格"""
    import shutil
    from pathlib import Path
    
    passed_dir = Path(directory) / "_quality_passed"
    failed_dir = Path(directory) / "_quality_failed"
    passed_dir.mkdir(exist_ok=True)
    failed_dir.mkdir(exist_ok=True)
    
    results = []
    images = [f for f in os.listdir(directory) if f.endswith('.png')]
    
    for img_file in images:
        path = os.path.join(directory, img_file)
        score = calculate_quality_score(path)
        
        if score['pass']:
            shutil.copy2(path, passed_dir / img_file)
        else:
            shutil.copy2(path, failed_dir / img_file)
        
        results.append({
            "file": img_file,
            "score": score
        })
    
    # 生成报告
    passed = sum(1 for r in results if r['score']['pass'])
    report = f"""
📊 图片质量检测报告
━━━━━━━━━━━━━━━━━━━━━━
总图片数: {len(results)}
合格: {passed} ({passed/len(results)*100:.1f}%)
不合格: {len(results)-passed}
平均分: {sum(r['score']['total'] for r in results)/len(results):.1f}

⚠️ 不合格列表:
"""
    for r in sorted(results, key=lambda x: x['score']['total'])[:10]:
        if not r['score']['pass']:
            report += f"  {r['file']}: {r['score']['total']} 分\n"
    
    with open(Path(directory) / "quality_report.txt", "w") as f:
        f.write(report)
    
    return results
```

---

## 模块 3：配色方案提取

```python
def extract_color_palette(img_path, n_colors=6):
    """提取图片主色调配色方案"""
    from sklearn.cluster import KMeans
    import numpy as np
    
    img = Image.open(img_path)
    img_small = img.resize((100, 100))
    arr = np.array(img_small)
    
    # 只取有色彩的像素（排除白色背景）
    pixels = arr.reshape(-1, 3)
    mask = (pixels.sum(axis=1) < 700)  # 排除接近白色
    pixels = pixels[mask]
    
    if len(pixels) < 100:
        return []
    
    # K-means 聚类
    kmeans = KMeans(n_clusters=n_colors, n_init=10, random_state=42)
    kmeans.fit(pixels)
    
    colors = kmeans.cluster_centers_.astype(int)
    labels = kmeans.labels_
    
    # 按出现频率排序
    counts = np.bincount(labels)
    sorted_indices = np.argsort(counts)[::-1]
    
    palette = []
    for idx in sorted_indices:
        r, g, b = colors[idx]
        hex_color = f"#{r:02X}{g:02X}{b:02X}"
        percentage = counts[idx] / len(labels) * 100
        palette.append({
            "hex": hex_color,
            "rgb": (int(r), int(g), int(b)),
            "percentage": round(percentage, 1)
        })
    
    return palette
```

---

## 模块 4：批量重命名（升级版）

结合前面所有模块的 `smart_rename.py`：

```python
# 工作流程
1. 全量 OCR 解析编号和标题
2. 感知哈希分组，解决编号识别失败
3. 质量检测，不合格标记
4. 配色方案提取，附加到文件名元数据
5. 标准化重命名 txxx-标题-版本-质量分.png
6. 生成完整报告
```

---

## 模块 5：图片生成集成

### 5.1 豆包生图 API 封装

```python
import requests
import json
import base64
from pathlib import Path

class DoubaoImageGenerator:
    """豆包生图 API 封装"""
    
    def __init__(self, api_key, base_url="https://ark.cn-beijing.volces.com/api/v3"):
        self.api_key = api_key
        self.base_url = base_url
    
    def generate(self, prompt, model="doubao-seed-2-0:260215", 
                 size="1024x1024", n=1, **kwargs):
        """生成图片"""
        headers = {
            "Authorization": f"Bearer {self.api_key}",
            "Content-Type": "application/json"
        }
        
        payload = {
            "model": model,
            "prompt": prompt,
            "size": size,
            "n": n,
            **kwargs
        }
        
        response = requests.post(
            f"{self.base_url}/images/generations",
            headers=headers,
            json=payload,
            timeout=120
        )
        
        if response.status_code == 200:
            return response.json()
        else:
            raise Exception(f"API Error: {response.status_code} - {response.text}")
    
    def batch_generate(self, prompts, output_dir, **kwargs):
        """批量生成"""
        output_path = Path(output_dir)
        output_path.mkdir(parents=True, exist_ok=True)
        
        results = []
        for i, prompt in enumerate(prompts):
            try:
                result = self.generate(prompt, **kwargs)
                # 保存图片
                for j, img_data in enumerate(result['data']):
                    if 'url' in img_data:
                        # 下载 URL
                        img_response = requests.get(img_data['url'])
                        img_path = output_path / f"gen_{i:03d}_{j:02d}.png"
                        with open(img_path, 'wb') as f:
                            f.write(img_response.content)
                    elif 'b64_json' in img_data:
                        # base64 解码
                        img_bytes = base64.b64decode(img_data['b64_json'])
                        img_path = output_path / f"gen_{i:03d}_{j:02d}.png"
                        with open(img_path, 'wb') as f:
                            f.write(img_bytes)
                    
                    results.append({
                        "prompt_index": i,
                        "variant": j,
                        "path": str(img_path),
                        "success": True
                    })
            except Exception as e:
                results.append({
                    "prompt_index": i,
                    "error": str(e),
                    "success": False
                })
        
        return results
```

---

## 完整工作流

```
输入图片目录
    ↓
[模块 1] 布局解析 + OCR
    ↓
[模块 1] 编号识别 + 相似度分组补全
    ↓
[模块 2] 质量检测 + 打分
    ↓
[模块 3] 配色方案提取
    ↓
[模块 4] 标准化重命名 txxx-标题-vxx-Qxx.png
    ↓
[输出] 最终图片 + JSON 报告 + 统计摘要
```

---

## 性能指标

| 操作 | 单张耗时 | 200 张耗时 |
|------|----------|-----------|
| OCR 解析 | 3-5s | 10-15 min |
| 质量检测 | 0.1s | 20s |
| 相似度分组 | 0.05s | 10s |
| **总计** | 3-5s | 10-16 min |

---

## 快速开始

```bash
# 安装依赖
pip install pillow imagehash numpy scipy scikit-learn requests

# 批量重命名（带质量检测）
python3 scripts/smart_rename.py /path/to/images

# 仅质量检测
python3 scripts/quality_check.py /path/to/images

# 批量生成
python3 scripts/batch_generate.py prompts.txt /output/path
```

## 收纳会话档案（2026-10-23 curator 从 batch-file-renaming 移入）

本 skill 的模块 1-4（OCR 直读 → 感知哈希分组 → 知识库匹配 → 批量重命名）与 batch-file-renaming skill 的 AI 名词卡片 3 阶段管线同源。两条实战会话档案已归位于本 skill references/：

- `references/***SECRET***.md` — 华哥 2026-07-13 铁律：**必须解图片内容提取编号再重命名，严禁套用 TERM_MAP 预设表**（用户否决 lookup-table fallback 的原始会话记录，任何图片识别类任务先读这个）。
- `references/flashcard-session-2026-07-13.md` — 79 张 AI 名词卡片重命名会话全记录（manifest 发现、T001-T020 编号规律、4 变体/词条）。

通用管线 SOP 仍在 batch-file-renaming skill（`references/ai-flashcard-3stage-pipeline.md` + `scripts/rename_flashcards.py`）；本 skill 提供模块级实现与质量检测。

