Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,11 +7,11 @@ from PIL import Image
|
|
| 7 |
from collections import defaultdict
|
| 8 |
import re
|
| 9 |
|
| 10 |
-
def
|
| 11 |
# แปลงจาก PIL → OpenCV
|
| 12 |
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 16 |
gray = cv2.adaptiveThreshold(
|
| 17 |
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
|
@@ -21,59 +21,38 @@ def ocr_scb_slip(image):
|
|
| 21 |
gray = cv2.dilate(gray, kernel, iterations=1)
|
| 22 |
gray = cv2.erode(gray, kernel, iterations=1)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
custom_config = r'-l eng --oem 3 --psm
|
| 26 |
|
| 27 |
-
# OCR
|
| 28 |
data = pytesseract.image_to_data(gray, config=custom_config, output_type=Output.DICT)
|
| 29 |
totalBox = len(data['text'])
|
| 30 |
lines = defaultdict(list)
|
| 31 |
|
| 32 |
-
# จัด
|
| 33 |
for i in range(totalBox):
|
| 34 |
if int(data['conf'][i]) > 0:
|
| 35 |
text = data['text'][i].strip()
|
| 36 |
if text != "":
|
| 37 |
line_id = (data['block_num'][i], data['par_num'][i], data['line_num'][i])
|
| 38 |
-
lines[line_id].append(
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
for line_id, indices in lines.items():
|
| 43 |
-
xs = [data['left'][i] for i in indices]
|
| 44 |
-
ys = [data['top'][i] for i in indices]
|
| 45 |
-
ws = [data['width'][i] for i in indices]
|
| 46 |
-
hs = [data['height'][i] for i in indices]
|
| 47 |
-
|
| 48 |
-
x, y = min(xs), min(ys)
|
| 49 |
-
w, h = max([xs[i]+ws[i] for i in range(len(xs))]) - x, max([ys[i]+hs[i] for i in range(len(ys))]) - y
|
| 50 |
-
|
| 51 |
-
cv2.rectangle(img, (x-margin, y-margin), (x + w+margin, y + h+margin), (0, 255, 0), 2)
|
| 52 |
-
|
| 53 |
-
# วางข้อความบนกรอบ
|
| 54 |
-
text_line = " ".join([data['text'][i] for i in indices])
|
| 55 |
-
cv2.putText(img, text_line, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
|
| 56 |
-
0.7, (0, 0, 255), 2, cv2.LINE_AA)
|
| 57 |
-
|
| 58 |
-
# รวมข้อความทั้งหมด
|
| 59 |
-
extracted_texts = []
|
| 60 |
-
for indices in lines.values():
|
| 61 |
-
extracted_texts.append(" ".join([data['text'][i] for i in indices]))
|
| 62 |
final_text = "\n".join(extracted_texts)
|
| 63 |
|
| 64 |
-
# แ
|
| 65 |
-
|
| 66 |
-
img_pil = Image.fromarray(img_out)
|
| 67 |
|
| 68 |
-
return
|
| 69 |
|
| 70 |
# Gradio UI
|
| 71 |
demo = gr.Interface(
|
| 72 |
-
fn=
|
| 73 |
inputs=gr.Image(type="pil", label="อัปโหลดสลิป SCB"),
|
| 74 |
-
outputs=
|
| 75 |
-
title="OCR สลิป SCB (
|
| 76 |
-
description="OCR โลโก้ SCB และข้อความ
|
| 77 |
)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
|
|
|
| 7 |
from collections import defaultdict
|
| 8 |
import re
|
| 9 |
|
| 10 |
+
def ocr_scb_slip_text(image):
|
| 11 |
# แปลงจาก PIL → OpenCV
|
| 12 |
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 13 |
|
| 14 |
+
# Preprocessing เพื่อให้ OCR อ่านแม่น
|
| 15 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 16 |
gray = cv2.adaptiveThreshold(
|
| 17 |
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
|
|
|
| 21 |
gray = cv2.dilate(gray, kernel, iterations=1)
|
| 22 |
gray = cv2.erode(gray, kernel, iterations=1)
|
| 23 |
|
| 24 |
+
# Tesseract config
|
| 25 |
+
custom_config = r'-l eng+tha --oem 3 --psm 6 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
| 26 |
|
| 27 |
+
# OCR
|
| 28 |
data = pytesseract.image_to_data(gray, config=custom_config, output_type=Output.DICT)
|
| 29 |
totalBox = len(data['text'])
|
| 30 |
lines = defaultdict(list)
|
| 31 |
|
| 32 |
+
# จัดข้อความเป็นบรรทัด
|
| 33 |
for i in range(totalBox):
|
| 34 |
if int(data['conf'][i]) > 0:
|
| 35 |
text = data['text'][i].strip()
|
| 36 |
if text != "":
|
| 37 |
line_id = (data['block_num'][i], data['par_num'][i], data['line_num'][i])
|
| 38 |
+
lines[line_id].append(text)
|
| 39 |
|
| 40 |
+
# รวมข้อความทั้งหมดเป็นบรรทัดเดียว
|
| 41 |
+
extracted_texts = [" ".join(words) for _, words in sorted(lines.items())]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
final_text = "\n".join(extracted_texts)
|
| 43 |
|
| 44 |
+
# แก้เว้นวรรคภาษาไทย
|
| 45 |
+
final_text = re.sub(r'([\u0E00-\u0E7F])\s+([\u0E00-\u0E7F])', r'\1\2', final_text)
|
|
|
|
| 46 |
|
| 47 |
+
return final_text
|
| 48 |
|
| 49 |
# Gradio UI
|
| 50 |
demo = gr.Interface(
|
| 51 |
+
fn=ocr_scb_slip_text,
|
| 52 |
inputs=gr.Image(type="pil", label="อัปโหลดสลิป SCB"),
|
| 53 |
+
outputs=gr.Textbox(label="ข้อความ OCR รวมทุกบรรทัด"),
|
| 54 |
+
title="OCR สลิป SCB (ข้อความรวม)",
|
| 55 |
+
description="OCR สลิป SCB รวมโลโก้ SCB และข้อความทั้งหมดเป็นข้อความเดียว"
|
| 56 |
)
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|