orgoflu commited on
Commit
25cdf71
ยท
verified ยท
1 Parent(s): d1f0261

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -9
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
- # PaddleOCR ์ดˆ๊ธฐํ™” (ํ•œ๊ตญ์–ด+์˜์–ด ์ง€์›)
135
  # -----------------------------
136
- ocr = PaddleOCR(use_angle_cls=True, lang='korean') # ํ•œ๊ตญ์–ด ๋ชจ๋ธ์€ ์˜์–ด๋„ ์ง€์›
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
  def handle_image(img) -> str:
139
  if img is None:
140
  return "โŒ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜์„ธ์š”."
141
  try:
142
- # numpy โ†’ PIL ๋ณ€ํ™˜ ํ›„ ๋‹ค์‹œ numpy๋กœ
143
- if isinstance(img, np.ndarray):
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)