Skip to content

Latest commit

 

History

History
395 lines (288 loc) · 7.07 KB

File metadata and controls

395 lines (288 loc) · 7.07 KB

贡献指南

感谢您对 OCR Agent 项目的兴趣!本指南将帮助您了解如何贡献代码。

开发环境设置

前置要求

  • Python 3.10+
  • Git
  • pip 或 conda

克隆和设置

# 克隆项目
git clone https://github.com/your-repo/ocr-agent.git
cd ocr-agent

# 创建虚拟环境
python -m venv venv
source venv/bin/activate  # Linux/Mac
#
venv\Scripts\activate  # Windows

# 安装开发依赖
pip install -e ".[dev]"
pip install pytest pytest-cov flake8 black isort

开发工作流

1. 创建分支

# 从 main 分支创建新分支
git checkout main
git pull origin main
git checkout -b feature/your-feature-name

分支命名规范:

  • feature/ - 新功能
  • fix/ - bug 修复
  • docs/ - 文档更新
  • refactor/ - 代码重构
  • test/ - 测试相关

2. 编写代码

代码风格

遵循 PEP 8 规范:

# 格式化代码
black src/ tests/

# 排序导入
isort src/ tests/

# 检查代码风格
flake8 src/ tests/ --max-line-length=100

类型提示

使用类型提示提高代码可读性:

from typing import Optional, List, Dict
from pathlib import Path
import numpy as np

def process_image(
    image_path: Path,
    output_format: str = "docx"
) -> Dict[str, str]:
    """处理图像并返回结果。
    
    Args:
        image_path: 输入图像路径
        output_format: 输出格式
    
    Returns:
        包含输出文件路径的字典
    """
    pass

文档字符串

使用 Google 风格的文档字符串:

def repair_image_advanced(
    self,
    image: np.ndarray,
    config: Optional[Config] = None
) -> np.ndarray:
    """高级图像修复,自动选择修复策略。
    
    根据图像质量自动选择超分辨率倍数和去噪方法。
    
    Args:
        image: 输入图像(BGR 格式)
        config: 配置对象(可选)
    
    Returns:
        修复后的图像
    
    Raises:
        ValueError: 如果图像格式无效
        RuntimeError: 如果修复失败
    
    Example:
        >>> module = ImageRepairModule()
        >>> repaired = module.repair_image_advanced(image)
    """
    pass

3. 编写测试

使用 TDD(测试驱动开发)方法:

import pytest
import numpy as np
from ocr_agent.modules.repair import ImageRepairModule

def test_repair_image_advanced():
    """测试高级修复功能。"""
    # 创建测试数据
    test_image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
    
    # 执行操作
    module = ImageRepairModule()
    result = module.repair_image_advanced(test_image)
    
    # 验证结果
    assert result is not None
    assert result.shape[2] == 3
    assert result.dtype == np.uint8

def test_repair_image_advanced_with_config():
    """测试带配置的修复功能。"""
    from ocr_agent.core.config import Config
    
    config = Config(repair_upscale_factor=4)
    module = ImageRepairModule(upscale_factor=4)
    
    test_image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
    result = module.repair_image_advanced(test_image, config)
    
    assert result is not None

运行测试:

# 运行所有测试
pytest tests/ -v

# 运行特定测试文件
pytest tests/test_repair.py -v

# 运行特定测试函数
pytest tests/test_repair.py::test_repair_image_advanced -v

# 生成覆盖率报告
pytest tests/ --cov=src/ocr_agent --cov-report=html

4. 提交代码

# 查看更改
git status
git diff

# 暂存更改
git add src/ocr_agent/modules/repair.py tests/test_repair.py

# 提交更改
git commit -m "feat: add advanced image repair with auto strategy selection"

提交信息规范:

  • feat: - 新功能
  • fix: - bug 修复
  • docs: - 文档更新
  • test: - 测试相关
  • refactor: - 代码重构
  • perf: - 性能优化
  • chore: - 其他更改

5. 推送和创建 PR

# 推送分支
git push origin feature/your-feature-name

# 在 GitHub 上创建 Pull Request
# 1. 访问 https://github.com/your-repo/ocr-agent
# 2. 点击 "New Pull Request"
# 3. 选择您的分支
# 4. 填写 PR 描述

PR 描述模板

## 描述
简要描述您的更改。

## 相关 Issue
关闭 #123

## 更改类型
- [ ] 新功能
- [ ] bug 修复
- [ ] 文档更新
- [ ] 代码重构

## 测试
- [ ] 添加了新测试
- [ ] 所有测试通过
- [ ] 覆盖率 > 80%

## 检查清单
- [ ] 代码遵循风格指南
- [ ] 文档已更新
- [ ] 没有新的警告
- [ ] 测试通过

代码审查

审查标准

  1. 功能正确性 - 代码是否实现了预期功能
  2. 代码质量 - 代码是否清晰、可维护
  3. 测试覆盖 - 是否有充分的测试
  4. 文档完整 - 是否有清晰的文档
  5. 性能影响 - 是否有性能问题

反馈流程

  • 作者回复审查意见
  • 进行必要的修改
  • 重新请求审查
  • 获得批准后合并

报告 Bug

Bug 报告模板

## 描述
清晰简洁地描述 bug。

## 复现步骤
1. 执行...
2. 然后...
3. 看到...

## 预期行为
应该发生什么。

## 实际行为
实际发生了什么。

## 环境
- OS: [e.g. Ubuntu 20.04]
- Python: [e.g. 3.10]
- 版本: [e.g. 0.1.0]

## 日志

粘贴相关日志

功能请求

功能请求模板

## 描述
清晰简洁地描述您想要的功能。

## 用例
为什么需要这个功能?

## 建议的解决方案
您建议如何实现?

## 替代方案
是否有其他方式实现?

文档贡献

文档结构

docs/
├── USER_GUIDE.md              # 用户指南
├── DOCKER_DEPLOYMENT.md       # Docker 部署
├── superpowers/
│   ├── specs/                 # 设计文档
│   └── plans/                 # 实现计划
└── implementation/            # 实现指南

文档风格

  • 使用 Markdown 格式
  • 清晰的标题层级
  • 代码示例要完整可运行
  • 包含必要的图表和表格

性能优化

性能测试

# 运行性能基准测试
pytest tests/test_benchmark.py -v -s

# 使用 cProfile 分析
python -m cProfile -s cumulative src/ocr_agent/pipeline.py

优化建议

  • 使用 NumPy 向量化操作
  • 避免不必要的复制
  • 使用缓存减少重复计算
  • 考虑并行处理

发布流程

版本号规范

遵循 Semantic Versioning:

  • MAJOR.MINOR.PATCH
  • 例如:0.1.0

发布步骤

  1. 更新版本号
  2. 更新 CHANGELOG
  3. 创建 git tag
  4. 发布到 PyPI

常见问题

Q: 如何运行完整的测试套件?

A:

pytest tests/ -v --cov=src/ocr_agent --cov-report=html

Q: 如何调试代码?

A:

# 使用 pdb
import pdb; pdb.set_trace()

# 或使用 IDE 调试器

Q: 如何处理依赖冲突?

A:

# 重新创建虚拟环境
rm -rf venv
python -m venv venv
source venv/bin/activate
pip install -e ".[dev]"

联系方式


感谢您的贡献!🎉