Spaces:
Sleeping
Sleeping
def extract_text_from_image(image_input):
Browse files
app.py
CHANGED
@@ -23,6 +23,9 @@ from google import genai
|
|
23 |
from google.genai import types
|
24 |
import base64
|
25 |
import io
|
|
|
|
|
|
|
26 |
|
27 |
# From other files
|
28 |
from storage_service import GoogleCloudStorage
|
@@ -2282,19 +2285,35 @@ def download_content(content):
|
|
2282 |
return word_path
|
2283 |
|
2284 |
# OCR
|
2285 |
-
def extract_text_from_image(
|
2286 |
-
"""
|
2287 |
-
|
2288 |
-
|
2289 |
-
|
2290 |
-
|
2291 |
-
|
2292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2293 |
buffered = io.BytesIO()
|
2294 |
image.save(buffered, format="JPEG")
|
2295 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
2296 |
|
2297 |
-
#
|
2298 |
image_part = types.Part.from_bytes(
|
2299 |
data=base64.b64decode(img_str),
|
2300 |
mime_type="image/jpeg",
|
|
|
23 |
from google.genai import types
|
24 |
import base64
|
25 |
import io
|
26 |
+
from PIL import Image
|
27 |
+
import requests
|
28 |
+
|
29 |
|
30 |
# From other files
|
31 |
from storage_service import GoogleCloudStorage
|
|
|
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):
|
2294 |
+
if image_input.startswith("http"):
|
2295 |
+
# 網路圖片
|
2296 |
+
response = requests.get(image_input)
|
2297 |
+
image = Image.open(io.BytesIO(response.content)).convert("RGB")
|
2298 |
+
elif image_input.startswith("data:image"):
|
2299 |
+
# base64 with header
|
2300 |
+
base64_data = image_input.split(",", 1)[1]
|
2301 |
+
image = Image.open(io.BytesIO(base64.b64decode(base64_data))).convert("RGB")
|
2302 |
+
elif os.path.exists(image_input):
|
2303 |
+
# 本地圖片
|
2304 |
+
image = Image.open(image_input).convert("RGB")
|
2305 |
+
else:
|
2306 |
+
# 純 base64 字串
|
2307 |
+
image = Image.open(io.BytesIO(base64.b64decode(image_input))).convert("RGB")
|
2308 |
+
else:
|
2309 |
+
raise ValueError("❌ 不支援的 image 輸入格式")
|
2310 |
+
|
2311 |
+
# ✅ 圖片轉 base64
|
2312 |
buffered = io.BytesIO()
|
2313 |
image.save(buffered, format="JPEG")
|
2314 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
2315 |
|
2316 |
+
# 🧠 組成 image_part
|
2317 |
image_part = types.Part.from_bytes(
|
2318 |
data=base64.b64decode(img_str),
|
2319 |
mime_type="image/jpeg",
|