File size: 1,636 Bytes
02b12e0
 
 
 
 
970c2b7
02b12e0
970c2b7
02b12e0
970c2b7
02b12e0
970c2b7
02b12e0
dca70f3
02b12e0
dca70f3
970c2b7
 
 
 
 
dca70f3
f233268
970c2b7
 
 
 
872cc73
02b12e0
970c2b7
 
 
 
 
 
f233268
970c2b7
 
f233268
970c2b7
 
02b12e0
970c2b7
02b12e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# image_processor.py
import google.generativeai as genai
from PIL import Image
import config
import os
import json

def extract_and_translate(image_path, tribe):
    """
    💡 視覺翻譯一體化:辨識 + 翻譯一次完成,節省 80% 時間
    """
    if not config.GEMINI_KEY: return None

    optimized_path = f"{image_path}_opt.jpg"
    try:
        with Image.open(image_path) as img:
            if img.mode != 'RGB': img = img.convert('RGB')
            if img.width > 1024:
                ratio = 1024 / float(img.width)
                img = img.resize((1024, int(img.height * ratio)), Image.Resampling.LANCZOS)
            img.save(optimized_path, "JPEG", quality=70)

        genai.configure(api_key=config.GEMINI_KEY)
        # 💡 使用 JSON 模式強制對齊格式
        model = genai.GenerativeModel(
            'models/gemini-3-flash-preview',
            generation_config={"temperature": 0.0, "response_mime_type": "application/json"}
        )
        
        prompt = f"""
        你是視覺翻譯專家。請辨識圖中文字並翻譯成{tribe}語。
        1. 排除重複內容。
        2. 輸出格式必須為 JSON:{{"items": [{{"original": "原文", "translated": "翻譯"}}]}}
        3. 遵守書寫憲法 2.0 (o轉u)。
        """
        
        img_opt = Image.open(optimized_path)
        response = model.generate_content([prompt, img_opt])
        
        if os.path.exists(optimized_path): os.remove(optimized_path)
        return json.loads(response.text)
    except Exception as e:
        if os.path.exists(optimized_path): os.remove(optimized_path)
        return None