youngtsai commited on
Commit
e0a1b3a
·
1 Parent(s): 63cc718

def extract_text_from_image_url(image_url):

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -2285,9 +2285,8 @@ def download_content(content):
2285
  return word_path
2286
 
2287
  # OCR
2288
- def extract_text_from_image(image_input):
2289
- """從上傳的圖片中提取文字,支援:路徑、網址、base64 字串、PIL 圖片"""
2290
- # 🔍 判斷輸入來源
2291
  if isinstance(image_input, Image.Image):
2292
  image = image_input
2293
  elif isinstance(image_input, str):
@@ -2311,6 +2310,16 @@ def extract_text_from_image(image_input):
2311
  else:
2312
  raise ValueError("❌ 不支援的 image 輸入格式")
2313
 
 
 
 
 
 
 
 
 
 
 
2314
  # ✅ 圖片轉 base64
2315
  buffered = io.BytesIO()
2316
  image.save(buffered, format="JPEG")
@@ -2383,6 +2392,11 @@ def extract_text_from_image(image_input):
2383
 
2384
  return response.text
2385
 
 
 
 
 
 
2386
 
2387
  # === INIT PARAMS ===
2388
  def init_params(request: gr.Request):
@@ -4073,7 +4087,7 @@ with gr.Blocks(theme=THEME, css=CSS) as demo:
4073
  )
4074
 
4075
  chinese_image_url_input_button.click(
4076
- fn=extract_text_from_image,
4077
  inputs=[chinese_image_url_input],
4078
  outputs=[chinese_full_paragraph_input]
4079
  )
 
2285
  return word_path
2286
 
2287
  # OCR
2288
+
2289
+ def get_image(image_input):
 
2290
  if isinstance(image_input, Image.Image):
2291
  image = image_input
2292
  elif isinstance(image_input, str):
 
2310
  else:
2311
  raise ValueError("❌ 不支援的 image 輸入格式")
2312
 
2313
+ return image
2314
+
2315
+ def extract_text_from_image(image_input):
2316
+ """從上傳的圖片中提取文字,支援:路徑、網址、base64 字串、PIL 圖片"""
2317
+ # 🔍 判斷輸入來源
2318
+ if isinstance(image_input, Image.Image):
2319
+ image = image_input
2320
+ else:
2321
+ image = get_image(image_input)
2322
+
2323
  # ✅ 圖片轉 base64
2324
  buffered = io.BytesIO()
2325
  image.save(buffered, format="JPEG")
 
2392
 
2393
  return response.text
2394
 
2395
+ def extract_text_from_image_url(image_url):
2396
+ image = get_image(image_url)
2397
+ text = extract_text_from_image(image)
2398
+
2399
+ return text
2400
 
2401
  # === INIT PARAMS ===
2402
  def init_params(request: gr.Request):
 
4087
  )
4088
 
4089
  chinese_image_url_input_button.click(
4090
+ fn=extract_text_from_image_url,
4091
  inputs=[chinese_image_url_input],
4092
  outputs=[chinese_full_paragraph_input]
4093
  )