Last updated: Oct 12, 2026
Week 6 — OCR with a Vision–Language Model
OCR (optical character recognition) turns a page image into text. Instead of a dedicated OCR engine, we send the image to a vision–language model through the OpenRouter API and ask it to transcribe the page.
1. Get the image
Download the test page (a woodblock-printed page of the Hongloumeng 紅樓夢, vertical classical Chinese) and put it in your dhg502 repo:

- (a) Local: right-click → Save Image As…, then drag it into the repo folder in VS Code.
- (b) Browser (Codespaces): save it to your computer, then drag it into the file explorer on the left.
2. Choose a model
The model must accept images. Use one of the Qwen models:
- Qwen3.5-9B (
qwen/qwen3.5-9b), our default - Qwen3.7 Flash
- Qwen3.8 Flash
Experiment: run the same page with each model and compare the results. To switch, copy the model ID from its OpenRouter page into ocr.py. Keep reasoning off.
3. In-class exercise
Open your repo in your AI coding assistant (for example VS Code with Copilot, OpenCode, or any other you set up in Week 1) and paste:
Prompt 1
Write a simple Python script ocr.py that does OCR on hongloumeng.jpeg using the OpenRouter API.
- Endpoint: https://openrouter.ai/api/v1/chat/completions
- Model: qwen/qwen3.5-9b, with reasoning disabled ("reasoning": {"enabled": false}).
- Send the image as a base64 data URL (image_url) together with a text prompt asking for OCR: transcribe all text exactly as it appears, preserve line breaks, output only the text. In the message content, put the image first and the text prompt after it.
- Read the API key from the environment variable OPENROUTER_API_KEY (do not hard-code it).
- Use only the Python standard library, set a timeout, and print the result.
- Also save the result to hongloumeng.txt.
Set your key and run it (the key is the one from keyreg.qhchina.org):
export OPENROUTER_API_KEY="your-key" python ocr.py
On Windows PowerShell use $env:OPENROUTER_API_KEY="your-key".
4. Check the result
The page is read in vertical columns, right to left. You should get something like:
京大学图书馆藏书 序红楼梦小说本名石头记作 者相传不一究竟未知出自何人 惟书内记雪芹曹先生删改 数过好事者每传抄一部置 庙市中昂其值得数十金可
Compare it with the image character by character. Note:
- The model outputs simplified characters although the page is in traditional script (normalization the model did for you, and you did not ask for).
- Errors and omissions are likely: plausible text is not the same as correct text. Always validate against a human-made transcription.
Try Prompt 2 to see how instructions change the output:
Prompt 2
Change the OCR prompt in ocr.py so the model keeps the original traditional characters (no conversion to simplified), and transcribes the page in the correct reading order (vertical columns, right to left). Run it again and show me the difference from the previous hongloumeng.txt.