# ai_imagerecognition_translation **Repository Path**: loadtogitee/ai_imagerecognition_translation ## Basic Information - **Project Name**: ai_imagerecognition_translation - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-07-15 - **Last Updated**: 2026-07-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 屏幕区域 OCR 识别 + LLM 翻译工具 框选屏幕任意区域,持续 OCR 识别其中的英文文本,带时间戳写入本地文件。识别完成后可调用 LLM(DeepSeek)将英文翻译为中文。 ## 功能 - **交互式区域框选**:半透明覆盖层 + 鼠标拖拽框选屏幕区域 - **持续 OCR**:基于 EasyOCR,实时识别英文文本 - **智能去重**:基于文本相似度(非精确匹配),解决 OCR 对同一字幕产生微小差异导致重复记录的问题 - **LLM 翻译 + 修正**:调用 DeepSeek API 先修正 OCR 识别错误(补全、纠错),再翻译为中文,输出三语对照 ## 安装 ```bash # 1. 安装依赖 pip install -r requirements.txt # 2. 下载 EasyOCR 英文模型(二选一) ``` ### 方式一:自动下载(首次运行时 EasyOCR 自动联网下载) 直接运行 `python image_recognition.py`,EasyOCR 会在首次启动时自动下载模型文件。 ### 方式二:手动下载模型 模型文件需放入 `D:\Source\easyocr_models`(或通过 `--model-dir` 指定其他目录)。 需要下载两个文件: | 文件 | 用途 | 大小 | 下载地址 | |------|------|------|----------| | `craft_mlt_25k.pth` | 文本检测模型 | ~79MB | [GitHub Releases](https://github.com/JaidedAI/EasyOCR/releases/download/pre-v1.1.6/craft_mlt_25k.zip) | | `english_g2.pth` | 英文识别模型 | ~14MB | [GitHub Releases](https://github.com/JaidedAI/EasyOCR/releases/download/v1.3/english_g2.zip) | 下载后解压 zip 文件,将 `.pth` 文件放入模型目录即可。 ## 使用 ### 1. OCR 录制 ```bash # 交互式框选区域 python image_recognition.py # 预设区域(跳过框选) python image_recognition.py --region 100,200,600,100 # 自定义参数 python image_recognition.py -o my_output.txt -f 3 --no-gpu # 调整去重灵敏度 python image_recognition.py --similarity 0.60 --stability 5 ``` | 参数 | 默认值 | 说明 | |------|--------|------| | `-o, --output` | `ocr_output.txt` | 输出文件路径 | | `-f, --fps` | `2.0` | 每秒截取帧数(0.5~10) | | `--no-gpu` | 关闭 | 强制 CPU 模式 | | `--region x,y,w,h` | 交互框选 | 预设截取区域 | | `--model-dir` | `D:\Source\easyocr_models` | 模型文件目录 | | `--similarity` | `0.70` | 文本相似度阈值(0~1) | | `--stability` | `3` | 稳定性帧数 | 运行中按 **Ctrl+C** 停止录制。 ### 2. LLM 翻译 ```bash # 设置 API Key(一次性配置) setx ANTHROPIC_API_KEY your-api-key setx ANTHROPIC_BASE_URL https://api.deepseek.com/anthropic # 翻译 OCR 输出 python ai_translation.py # 指定输入输出 python ai_translation.py -i ocr_output.txt -o translation_output.txt ``` | 参数 | 默认值 | 说明 | |------|--------|------| | `-i, --input` | `ocr_output.txt` | OCR 输出文件 | | `-o, --output` | `translation_output.txt` | 翻译输出文件 | | `--model` | `deepseek-chat` | 模型名 | | `--api-key` | 从环境变量读取 | API Key | > 翻译需要 DeepSeek API Key。API Key 从环境变量 `ANTHROPIC_API_KEY` 读取, > 通过 `ANTHROPIC_BASE_URL` 指定 DeepSeek 的 Anthropic 兼容端点。 ### 3. 输出示例 **ocr_output.txt**(OCR 原始识别结果,含识别错误): ``` [00:00] Happy New Year! [00:05] I you had a New Year' s Eve; and I hope that the new year treats you well. hope good [00:12] I just wanted to start this video by thanking all of you for watching my Youtube videos: [00:19] I know help you leatn English, but their also a lot of fun for meito make, and I started they [00:29] Two years ago I had 0 subscribers, and as of I have close to 18,000 subscribers. today [00:40] You're awesomel ``` **translation_output.txt**(LLM 修正英文 + 中文翻译): ``` [00:00] EN_OCR: Happy New Year! ZH: 新年快乐! [00:05] EN_OCR: I you had a New Year' s Eve; and I hope that the new year treats you well. hope good EN: I hope you had a good New Year's Eve, and I hope that the new year treats you well. ZH: 我希望你跨年夜过得愉快,也希望新的一年对你好一点。 [00:19] EN_OCR: I know help you leatn English, but their also a lot of fun for meito make, and I started they EN: I know they help you learn English, but they're also a lot of fun for me to make, and I started them ZH: 我知道它们能帮助你们学英语,但制作这些视频对我来说也很有意思。 [00:40] EN_OCR: You're awesomel EN: You're awesome! ZH: 你们太棒了! ``` > `EN_OCR` = OCR 原始识别文本,`EN` = LLM 根据语义修正后的英文(无修正时不显示),`ZH` = 中文翻译。 ## 模块 | 文件 | 说明 | |------|------| | `image_recognition.py` | 主入口:CLI 参数、区域框选、截屏循环 | | `region_selector.py` | 半透明覆盖层框选工具 | | `ocr_processor.py` | EasyOCR 引擎、文本去重、文件写入 | | `ai_translation.py` | LLM 翻译,独立运行 | ## 依赖 - [EasyOCR](https://github.com/JaidedAI/EasyOCR) — 离线 OCR 引擎 - [mss](https://github.com/BoboTiG/python-mss) — 高速屏幕截取 - [Anthropic SDK](https://github.com/anthropics/anthropic-sdk-python) — LLM API 调用