SuperAPI AI Directory

code-ocr

Coding & development

View repository

Updated 10-16 00:29

About this skill

Code OCR

Convert code screenshots into text files using the Baidu OCR high-precision with location version API, preserving the original indentation.

Environment Configuration

Before use, you need to configure the Baidu API key (system environment variable). It is assumed that the user has already completed the configuration:

# Windows
setx BAIDU_API_KEY "your_api_key"
setx BAIDU_SECRET_KEY "your_secret_key"

Or set it in the current terminal:

# Windows (当前终端)
set BAIDU_API_KEY=your_api_key
set BAIDU_SECRET_KEY=your_secret_key

Usage

Basic Usage

# 保存 OCR 中间结果(避免重复调用 API)
python "C:\Users\12556\.claude\skills\code-ocr\scripts\code_ocr.py" <图片路径> --save-json <json文件路径>

# 从 JSON 加载结果,多次调整 ratio 参数实验
python "C:\Users\12556\.claude\skills\code-ocr\scripts\code_ocr.py" <json文件> --load-json <json文件路径> --ratio <值>

# 仅识别图片(不推荐)
python "C:\Users\12556\.claude\skills\code-ocr\scripts\code_ocr.py" <图片路径> [输出目录]

Parameter Description

Parameter Description
图片路径 Optional, the path to the screenshot file (supports PNG, JPG, BMP, and other formats), or the path to a JSON file
输出目录 Optional, by default saved to the directory where the image is located
--ratio / -r Optional, the scaling multiplier for the average character width, used to fine-tune the indentation calculation. Default 0.955
--save-json FILE / -s FILE Optional, save the intermediate result to a JSON file after successful OCR
--load-json FILE / -l FILE Optional, load the OCR result from a JSON file, skipping the OCR step

Usage Examples

# 1. 首次运行:OCR 识别并保存中间结果
python "C:\Users\12556\.claude\skills\code-ocr\scripts\code_ocr.py" screenshot.png -s screenshot.json

# 2. 后续实验:加载 JSON(使用 -l 参数),多次调整 ratio 参数
# 只需指定 -l JSON文件路径 和 --ratio 值,无需其他参数
python "C:\Users\12556\.claude\skills\code-ocr\scripts\code_ocr.py" -l screenshot.json --ratio 0.96
python "C:\Users\12556\.claude\skills\code-ocr\scripts\code_ocr.py" -l screenshot.json --ratio 0.955
python "C:\Users\12556\.claude\skills\code-ocr\scripts\code_ocr.py" -l screenshot.json --ratio 0.95

# 3. 简写形式
python "C:\Users\12556\.claude\skills\code-ocr\scripts\code_ocr.py" -l screenshot.json -r 0.955

Tip: After the first OCR recognition, a JSON file will be generated. If you need to adjust the indentation effect, please use the -l parameter to load that JSON file and run again.

Output File

  • File name format:{原文件名}_{时间戳}_{ratio}.txt
  • For example:screenshot_20260220_143052_0.955.txt
  • Encoding: UTF-8

Prompts the AI tool needs to provide

Important: When the AI tool should proactively provide the command to rerun based on the JSON file, for example:

# 使用 -l 参数加载 JSON 文件,调整 ratio 参数
python "C:\Users\12556\.claude\skills\code-ocr\scripts\code_ocr.py" -l screenshot.json --ratio 0.96
python "C:\Users\12556\.claude\skills\code-ocr\scripts\code_ocr.py" -l screenshot.json --ratio 0.955
python "C:\Users\12556\.claude\skills\code-ocr\scripts\code_ocr.py" -l screenshot.json --ratio 0.95

And explain to the user the effect differences of different ratio values, helping the user choose the best indentation parameter.

How It Works

  1. API Call: Use the Baidu OCR "high-precision with location version" (accurate) interface
  2. Location Parsing: Use the returned location information (location) to calculate the coordinates of each character
  3. Line Reconstruction: Group by the top coordinate of the characters to identify all lines
  4. Indentation Preservation: Calculate the number of indentation spaces based on the left coordinate of the first character of each line

Cross-Validation (Model Self-Check)

After the OCR result is output, the AI model should proactively use its own multimodal capabilities to perform "visual OCR" on the original image and cross-validate it with the output of Baidu OCR.

Validation Process

  1. Read the original image: The AI model directly views the code screenshot provided by the user
  2. Visual Recognition: Use the model's visual capabilities to recognize the code text in the image
  3. Comparison and Proofreading: Compare the visual recognition result with the Baidu OCR output
  4. Correct Errors: When differences are found, correct them based on the visual recognition result

Common Items That Need Verification

Verification Item Description Example
Similar Characters O vs 0,I vs l vs 1,S vs 5 list misrecognized as 1ist
Symbol Confusion ' vs ''," vs "",-> vs → don't misrecognized as don''t
Special Symbols @, #, $, %, &, |, \, / @staticmethod in @
Indentation Alignment Mixed use of spaces and tabs Indentation levels in Python code
Comment symbols //, /*, */, #, <!-- Comment start and end positions
Bracket matching (), {}, [], <> Completeness of nested brackets
Missing symbols Semicolons, commas, etc. at the end of code lines return x is mistakenly recognized as return x;
Chinese and English symbols English comma , vs Chinese comma , print("Hello, World") is mistakenly recognized as print("Hello,World")

Flexible validation: determine the code language based on the code language and context, and perform more targeted validation according to the syntax rules of that language.

Output format after proofreading

=== 百度 OCR 原始结果 ===
[原始识别文本]

=== 交叉验证修正 ===
- 第 X 行: "原始错误" → "修正后"
- 第 Y 行: "原始错误" → "修正后"

=== 最终修正结果 ===
[校对后的最终文本]

Usage suggestions

  • For critical code (such as keys, configurations, regular expressions),mustperform cross-validation
  • When there are obvious syntax errors in the OCR results, prioritize trusting the model's visual recognition
  • If the image is blurry or the font is unusual, you can prompt the user to provide a clearer screenshot

Script location

Script path:C:\Users\12556\.claude\skills\code-ocr\scripts\code_ocr.py