OCR用のapp.pyを作成
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pytesseract
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import fitz # PyMuPDF
|
| 5 |
+
import io
|
| 6 |
+
import cv2
|
| 7 |
+
import numpy as np
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
pytesseract.pytesseract.tesseract_cmd = "tesseract" # Hugging Face内はこれでOK
|
| 11 |
+
|
| 12 |
+
def preprocess_image_for_ocr(image):
|
| 13 |
+
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 14 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
| 15 |
+
gray = clahe.apply(gray)
|
| 16 |
+
blurred = cv2.bilateralFilter(gray, 9, 75, 75)
|
| 17 |
+
sharp = cv2.addWeighted(blurred, 1.5, cv2.GaussianBlur(blurred, (0, 0), 5), -0.5, 0)
|
| 18 |
+
denoised = cv2.fastNlMeansDenoising(sharp, h=20)
|
| 19 |
+
adaptive = cv2.adaptiveThreshold(denoised, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
| 20 |
+
cv2.THRESH_BINARY, 35, 11)
|
| 21 |
+
_, otsu = cv2.threshold(denoised, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 22 |
+
binary = cv2.bitwise_and(adaptive, otsu)
|
| 23 |
+
opened = cv2.morphologyEx(binary, cv2.MORPH_OPEN, np.ones((1, 1), np.uint8))
|
| 24 |
+
h, w = opened.shape
|
| 25 |
+
opened = cv2.resize(opened, (w * 2, h * 2), interpolation=cv2.INTER_CUBIC)
|
| 26 |
+
return Image.fromarray(opened)
|
| 27 |
+
|
| 28 |
+
def ocr_from_file(file):
|
| 29 |
+
ext = os.path.splitext(file.name)[1].lower()
|
| 30 |
+
result_text = ""
|
| 31 |
+
try:
|
| 32 |
+
if ext in ['.png', '.jpg', '.jpeg', '.bmp', '.tiff']:
|
| 33 |
+
image = Image.open(file)
|
| 34 |
+
processed = preprocess_image_for_ocr(image)
|
| 35 |
+
result_text = pytesseract.image_to_string(processed, lang="jpn+eng")
|
| 36 |
+
elif ext == '.pdf':
|
| 37 |
+
doc = fitz.open(file.name)
|
| 38 |
+
for page_num in range(min(len(doc), 3)):
|
| 39 |
+
page = doc.load_page(page_num)
|
| 40 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(4, 4))
|
| 41 |
+
img = Image.open(io.BytesIO(pix.tobytes()))
|
| 42 |
+
processed = preprocess_image_for_ocr(img)
|
| 43 |
+
result_text += pytesseract.image_to_string(processed, lang="jpn+eng") + "\n"
|
| 44 |
+
else:
|
| 45 |
+
return "非対応形式です。"
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"OCRエラー: {e}"
|
| 48 |
+
return result_text.strip()
|
| 49 |
+
|
| 50 |
+
gr.Interface(
|
| 51 |
+
fn=ocr_from_file,
|
| 52 |
+
inputs=gr.File(label="画像またはPDFをアップロード"),
|
| 53 |
+
outputs=gr.Textbox(label="OCR結果"),
|
| 54 |
+
title="🚀 高精度OCR Lite",
|
| 55 |
+
description="日本語・英語対応の高精度OCRツール(Lite体験版)"
|
| 56 |
+
).launch()
|