DEV Community

Warun C. ⚡
Warun C. ⚡ Subscriber

Posted on • Edited on

วิธีใช้ Typhoon-OCR และ Ollama แปลงภาพเป็นข้อความ (ฉบับย่อ)

เบื่อไหมกับ OCR ภาษาไทยที่ไม่แม่นยำ? ลองใช้ Typhoon-OCR ซึ่งเป็น LLM สำหรับอ่านข้อความไทยโดยเฉพาะ รันง่ายๆ ผ่าน Ollama บนเครื่องของคุณเอง ทำตามนี้ได้เลย

อ่านฉบับเต็ม...

เริ่มต้นใน 3 ขั้นตอน

สิ่งที่ต้องมี:

  1. Ollama
  2. Python 3

ขั้นตอนที่ 1: ติดตั้งโมเดลผ่าน Terminal

เปิด Terminal (หรือ Command Prompt) แล้วรันคำสั่งนี้เพื่อดาวน์โหลดโมเดล Typhoon-OCR:

ollama pull scb10x/typhoon-ocr-3b:latest
ollama run scb10x/typhoon-ocr-3b:latest
Enter fullscreen mode Exit fullscreen mode

ขั้นตอนที่ 2: เตรียมสคริปต์ Python

  1. ติดตั้ง library ที่จำเป็น:
pip install openai
Enter fullscreen mode Exit fullscreen mode
  1. สร้างไฟล์ Python (เช่น main.py) แล้วใส่โค้ดนี้:
import base64
from openai import OpenAI

# ตั้งค่าเพื่อเชื่อมต่อกับ Ollama
client = OpenAI(
    base_url='http://localhost:11434/v1',
    api_key='ollama',
)

# ฟังก์ชันแปลงภาพเป็น base64
def encode_image_to_base64(image_path):
  with open(image_path, "rb") as f:
      return base64.b64encode(f.read()).decode("utf-8")

# เปลี่ยน "sample_image.png" เป็นชื่อไฟล์ของคุณ
base64_image = encode_image_to_base64("sample_image.png")

# ส่ง request ไปยังโมเดล
response = client.chat.completions.create(
  model="sakares/typhoon-ocr",
  messages=[
    {
      "role": "user",
      "content": [
        {"type": "text", "text": "Extract text from this image:"},
        {
          "type": "image_url",
          "image_url": {"url": f"data:image/png;base64,{base64_image}"}
        }
      ]
    }
  ]
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

ขั้นตอนที่ 3: รันสคริปต์

สั่งรันไฟล์ Python ของคุณใน Terminal:

python main.py
Enter fullscreen mode Exit fullscreen mode

ผลจากการรัน Typhoon OCR ...

ผลลัพธ์


ถูกใจกด เป็นกำลังใจให้ทำคอนเทนต์ดีๆ & ติดตามเรื่องที่น่าสนใจผ่านทาง
Warun Chareonsuk & Super AI Agent -- @ubinix-warun

ตามลิงค์เข้าไปเลี้ยงกาแฟผมได้ที่ ☕ Buy Me A Coffee

Top comments (0)