Spaces:
Sleeping
Sleeping
File size: 2,278 Bytes
7541803 4d37a12 add98ca 259b92e add98ca 259b92e c0cb8ff f221e96 259b92e 4d37a12 f221e96 2636f1e afd726b f221e96 2636f1e f221e96 2636f1e 259b92e f221e96 259b92e f221e96 d03d800 f221e96 2636f1e 259b92e f221e96 2636f1e f221e96 259b92e f221e96 2636f1e f221e96 add98ca 259b92e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import cv2
import pytesseract
from pytesseract import Output
import numpy as np
import gradio as gr
from PIL import Image
from collections import defaultdict
import re
def ocr_scb_slip_text(image):
# แปลงจาก PIL → OpenCV
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
# Preprocessing เพื่อให้ OCR อ่านแม่น
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.adaptiveThreshold(
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 11, 2
)
kernel = np.ones((2,2), np.uint8)
gray = cv2.dilate(gray, kernel, iterations=1)
gray = cv2.erode(gray, kernel, iterations=1)
# Tesseract config
custom_config = r'-l eng+tha --oem 3 --psm 6 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
# OCR
data = pytesseract.image_to_data(gray, config=custom_config, output_type=Output.DICT)
totalBox = len(data['text'])
lines = defaultdict(list)
# จัดข้อความเป็นบรรทัด
for i in range(totalBox):
if int(data['conf'][i]) > 0:
text = data['text'][i].strip()
if text != "":
line_id = (data['block_num'][i], data['par_num'][i], data['line_num'][i])
lines[line_id].append(text)
# รวมข้อความทั้งหมดเป็นบรรทัดเดียว
extracted_texts = [" ".join(words) for _, words in sorted(lines.items())]
final_text = "\n".join(extracted_texts)
# แก้เว้นวรรคภาษาไทย
final_text = re.sub(r'([\u0E00-\u0E7F])\s+([\u0E00-\u0E7F])', r'\1\2', final_text)
return final_text
# Gradio UI
demo = gr.Interface(
fn=ocr_scb_slip_text,
inputs=gr.Image(type="pil", label="อัปโหลดสลิป SCB"),
outputs=gr.Textbox(label="ข้อความ OCR รวมทุกบรรทัด"),
title="OCR สลิป SCB (ข้อความรวม)",
description="OCR สลิป SCB รวมโลโก้ SCB และข้อความทั้งหมดเป็นข้อความเดียว"
)
if __name__ == "__main__":
demo.launch()
|