Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import gradio as gr
|
|
| 5 |
from paddleocr import PaddleOCR
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
|
| 9 |
# Summarizer
|
| 10 |
try:
|
|
@@ -131,23 +132,37 @@ def handle_summary(url: str, sent_n: int) -> str:
|
|
| 131 |
return f"์๋ฌ: {e}"
|
| 132 |
|
| 133 |
# -----------------------------
|
| 134 |
-
#
|
| 135 |
# -----------------------------
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
def handle_image(img) -> str:
|
| 139 |
if img is None:
|
| 140 |
return "โ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์."
|
| 141 |
try:
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
img = Image.fromarray(img)
|
| 145 |
-
results = ocr.ocr(np.array(img))
|
| 146 |
if not results or not results[0]:
|
| 147 |
return "ํ
์คํธ๋ฅผ ์ถ์ถํ ์ ์์ต๋๋ค."
|
| 148 |
|
| 149 |
lines = []
|
| 150 |
-
for res in results[0]:
|
| 151 |
if len(res) == 2 and isinstance(res[1], tuple):
|
| 152 |
txt, conf = res[1]
|
| 153 |
lines.append(f"{txt} (conf:{conf:.2f})")
|
|
@@ -162,7 +177,7 @@ with gr.Blocks(css="""
|
|
| 162 |
#container { max-width: 920px; margin: 0 auto; }
|
| 163 |
.small { color:#666; font-size:14px; }
|
| 164 |
""") as demo:
|
| 165 |
-
gr.Markdown("## URL โ HTML/ํ
์คํธ/์์ฝ + ์ด๋ฏธ์ง OCR (PaddleOCR)", elem_id="container")
|
| 166 |
|
| 167 |
with gr.Row():
|
| 168 |
url_input = gr.Textbox(label="URL", placeholder="https://example.com", scale=4)
|
|
@@ -175,7 +190,7 @@ with gr.Blocks(css="""
|
|
| 175 |
sent_n = gr.Slider(1, 8, value=3, step=1, label="์์ฝ ๋ฌธ์ฅ ์")
|
| 176 |
btn_sum = gr.Button("์๋์์ฝ ๋ณด๊ธฐ", scale=1)
|
| 177 |
|
| 178 |
-
gr.Markdown("### ์ด๋ฏธ์ง ์
๋ก๋ โ OCR (PaddleOCR)")
|
| 179 |
with gr.Row():
|
| 180 |
img_input = gr.Image(type="numpy", label="์ด๋ฏธ์ง ์
๋ก๋")
|
| 181 |
btn_img = gr.Button("์ด๋ฏธ์ง OCR ์คํ", scale=1)
|
|
|
|
| 5 |
from paddleocr import PaddleOCR
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
| 8 |
+
import cv2
|
| 9 |
|
| 10 |
# Summarizer
|
| 11 |
try:
|
|
|
|
| 132 |
return f"์๋ฌ: {e}"
|
| 133 |
|
| 134 |
# -----------------------------
|
| 135 |
+
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ + PaddleOCR
|
| 136 |
# -----------------------------
|
| 137 |
+
def preprocess_image(img):
|
| 138 |
+
if isinstance(img, np.ndarray):
|
| 139 |
+
arr = img
|
| 140 |
+
else:
|
| 141 |
+
arr = np.array(img)
|
| 142 |
+
|
| 143 |
+
gray = cv2.cvtColor(arr, cv2.COLOR_RGB2GRAY)
|
| 144 |
+
# ๋๋น ๊ฐํ (์ด์งํ)
|
| 145 |
+
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 146 |
+
# ํฌ๊ธฐ ํค์ฐ๊ธฐ
|
| 147 |
+
h, w = thresh.shape
|
| 148 |
+
if h < 600:
|
| 149 |
+
scale = 600 / h
|
| 150 |
+
thresh = cv2.resize(thresh, None, fx=scale, fy=scale, interpolation=cv2.INTER_LINEAR)
|
| 151 |
+
return thresh
|
| 152 |
+
|
| 153 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='korean') # ํ๊ตญ์ด+์์ด ์ง์
|
| 154 |
|
| 155 |
def handle_image(img) -> str:
|
| 156 |
if img is None:
|
| 157 |
return "โ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์."
|
| 158 |
try:
|
| 159 |
+
proc_img = preprocess_image(img)
|
| 160 |
+
results = ocr.ocr(proc_img)
|
|
|
|
|
|
|
| 161 |
if not results or not results[0]:
|
| 162 |
return "ํ
์คํธ๋ฅผ ์ถ์ถํ ์ ์์ต๋๋ค."
|
| 163 |
|
| 164 |
lines = []
|
| 165 |
+
for res in results[0]:
|
| 166 |
if len(res) == 2 and isinstance(res[1], tuple):
|
| 167 |
txt, conf = res[1]
|
| 168 |
lines.append(f"{txt} (conf:{conf:.2f})")
|
|
|
|
| 177 |
#container { max-width: 920px; margin: 0 auto; }
|
| 178 |
.small { color:#666; font-size:14px; }
|
| 179 |
""") as demo:
|
| 180 |
+
gr.Markdown("## URL โ HTML/ํ
์คํธ/์์ฝ + ์ด๋ฏธ์ง OCR (PaddleOCR + ์ ์ฒ๋ฆฌ)", elem_id="container")
|
| 181 |
|
| 182 |
with gr.Row():
|
| 183 |
url_input = gr.Textbox(label="URL", placeholder="https://example.com", scale=4)
|
|
|
|
| 190 |
sent_n = gr.Slider(1, 8, value=3, step=1, label="์์ฝ ๋ฌธ์ฅ ์")
|
| 191 |
btn_sum = gr.Button("์๋์์ฝ ๋ณด๊ธฐ", scale=1)
|
| 192 |
|
| 193 |
+
gr.Markdown("### ์ด๋ฏธ์ง ์
๋ก๋ โ OCR (PaddleOCR + ์ ์ฒ๋ฆฌ)")
|
| 194 |
with gr.Row():
|
| 195 |
img_input = gr.Image(type="numpy", label="์ด๋ฏธ์ง ์
๋ก๋")
|
| 196 |
btn_img = gr.Button("์ด๋ฏธ์ง OCR ์คํ", scale=1)
|