OpenRouter OCR Skill
Overview
This skill provides OCR (Optical Character Recognition) functionality using models available via OpenRouter. It uses the OpenAI Python library to communicate with OpenRouter's API, specifically designed for models like Baidu's Qianfan OCR.
Quick Start
When you need to extract text from an image:
-
Ensure prerequisites:
- Python 3.x installed
- Required packages:
openai,requests(install viapip install openai requests) - Place your OpenRouter API key in the file:
C:\Users\Administrator\.openclaw\secrets\openrouter.env(format:OPENROUTER_API_KEY=your_key_here)
-
Call the OCR script with an image URL or local file path:
python ocr.py <image_input> [prompt]image_input: Either a URL or a local file path to the imageprompt: Optional text prompt for the OCR (default: "OCR提取图片所有文字")
-
Get result: The script prints the extracted text to stdout.
Usage Examples
Basic Usage with Default Prompt
python ocr.py "https://example.com/image.jpg"
Custom Prompt
python ocr.py "https://example.com/image.jpg" "请识别图片中的所有文字"
Local Image File
python ocr.py "C:\path\to\image.jpg"
How It Works
The skill uses the OpenAI client configured with:
- Base URL:
https://openrouter.ai/api/v1 - Model:
baidu/qianfan-ocr-fast:free(configurable via environment variable) - API Key: Read from
OPENROUTER_API_KEYenvironment variable
It sends a multimodal request containing:
- A text prompt (default: "OCR提取图片所有文字")
- The image (encoded as base64 if local, or passed directly if URL)
The model returns the extracted text which is printed to console.
Environment Variables
OPENROUTER_API_KEY: Required - Your OpenRouter API keyOCR_MODEL: Optional - Model to use (default:baidu/qianfan-ocr-fast:free)OCR_BASE_URL: Optional - OpenRouter base URL (default:https://openrouter.ai/api/v1)
Installation
- Create the skill directory:
mkdir -p skills/openrouter-ocr - Save the
ocr.pyscript in this directory - Install dependencies:
pip install openai requests - Set your OpenRouter API key:
(Restart terminal after setting)setx OPENROUTER_API_KEY "your_api_key_here"
Notes
- The skill works with both HTTP/HTTPS URLs and local file paths
- For local files, the image is read and base64-encoded before sending
- Error handling includes network issues, invalid API keys, and model errors
- The default model is Baidu's Qianfan OCR fast version (free tier)
- You can change the model by setting the
OCR_MODELenvironment variable - Response time depends on image size and model speed
Troubleshooting
- API Key Error: Ensure
OPENROUTER_API_KEYis set correctly - Module Not Found: Install required packages with
pip install openai requests - Image Access: Verify the image URL is accessible or local path exists
- Model Not Available: Check if the specified model is available on OpenRouter
Example Output
✅ OCR 识别结果:
------------------------------------------------------------
这是识别出的文本内容
...
------------------------------------------------------------
Security Note
Never commit your API key to version control. Keep it secure in environment variables.