File size: 1,264 Bytes
cd00a55
31447f5
dfd8ef3
cd00a55
31447f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from PIL import Image
import traceback

def generate_caption(filepaths, choice_index):
    try:
        idx = int(choice_index)
        img_path = filepaths[idx]
        print(f"[DEBUG] Selected image path: {img_path}")

        # 1) ์ด๋ฏธ์ง€ ๋กœ๋“œ
        img = Image.open(img_path).convert("RGB")
        print("[DEBUG] Image loaded")

        # 2) ์บก์…˜ ์ƒ์„ฑ
        out = captioner(img)
        print(f"[DEBUG] captioner output: {out!r}")
        first = out[0] if isinstance(out, list) else out
        raw = first.get("generated_text") or first.get("text") or str(first)
        raw = raw.strip()
        print(f"[DEBUG] raw caption: {raw!r}")

        # 3) ์žฅ๋ฉด ๋ถ„๋ฅ˜
        cls = scene_classifier(img, candidate_labels=SCENE_LABELS)
        print(f"[DEBUG] scene_classifier output: {cls!r}")
        scene = cls["labels"][0]

        # 4) ํ…œํ”Œ๋ฆฟ ๋งคํ•‘
        template = TEMPLATES.get(scene, "In this picture, {caption}.")
        result = template.format(caption=raw)
        print(f"[DEBUG] Final result: {result}")
        return result

    except Exception as e:
        # ์—๋Ÿฌ ๋ฉ”์‹œ์ง€์™€ ์Šคํƒ ํŠธ๋ ˆ์ด์Šค ๋ฆฌํ„ด
        error_msg = f"๐Ÿ”ด Error:\n{e}\n\n{traceback.format_exc()}"
        print(error_msg)
        return error_msg