File size: 2,511 Bytes
70bb32f
563a7d7
b51d9c8
 
6ecfda0
 
35a5759
6ecfda0
 
b51d9c8
 
d73b1ce
b51d9c8
 
 
35a5759
 
 
b51d9c8
 
 
 
8fad186
d73b1ce
b51d9c8
8fad186
6ecfda0
b51d9c8
35a5759
b51d9c8
35a5759
 
 
8fad186
 
b51d9c8
8fad186
 
 
 
b51d9c8
35a5759
b51d9c8
 
 
 
35a5759
 
 
 
 
 
 
8fad186
b51d9c8
35a5759
 
b51d9c8
35a5759
b51d9c8
 
 
 
d73b1ce
693a43b
d73b1ce
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67


# app.py
import os, shutil, zipfile, pathlib, tempfile
import pandas as pd
import gradio as gr
from PIL import Image
from autogluon.multimodal import MultiModalPredictor

ZIP_FILENAME = "autogluon_image_predictor_dir.zip"
EXTRACT_DIR = pathlib.Path("predictor_native")

def _load_predictor() -> MultiModalPredictor:
    if not os.path.exists(ZIP_FILENAME):
        raise FileNotFoundError(f"Missing {ZIP_FILENAME} in repo root.")
    if EXTRACT_DIR.exists():
        shutil.rmtree(EXTRACT_DIR)
    EXTRACT_DIR.mkdir(parents=True, exist_ok=True)
    with zipfile.ZipFile(ZIP_FILENAME, "r") as zf:
        zf.extractall(str(EXTRACT_DIR))
    contents = list(EXTRACT_DIR.iterdir())
    predictor_root = contents[0] if (len(contents) == 1 and contents[0].is_dir()) else EXTRACT_DIR
    return MultiModalPredictor.load(str(predictor_root))  # ← fixed

PREDICTOR = _load_predictor()
CLASS_LABELS = {i: chr(65 + i) for i in range(26)}  # 0->A ... 25->Z

def predict(pil_img: Image.Image):
    if pil_img is None:
        return {}, None
    if pil_img.mode != "RGB":
        pil_img = pil_img.convert("RGB")
    processed = pil_img.resize((224, 224))
    tmpdir = pathlib.Path(tempfile.mkdtemp())
    img_path = tmpdir / "input.png"
    processed.save(img_path)

    df = pd.DataFrame({"image": [str(img_path)]})
    proba_df = PREDICTOR.predict_proba(df)

    pretty = {}
    for col in proba_df.columns:
        label = f"Letter {CLASS_LABELS[col]}" if isinstance(col, int) and 0 <= col < 26 else str(col)
        pretty[label] = float(proba_df[col].iloc[0])
    pretty = dict(sorted(pretty.items(), key=lambda kv: kv[1], reverse=True))
    return pretty, processed

EXAMPLES = [
    ["https://www.signingsavvy.com/images/words/alphabet/2/a1.jpg"],
    ["https://www.signingsavvy.com/images/words/alphabet/2/b1.jpg"],
    ["https://www.signingsavvy.com/images/words/alphabet/2/c1.jpg"],
]

with gr.Blocks(title="Sign Language Recognition") as demo:
    gr.Markdown("# Sign Language Recognition (AutoGluon Image)")
    with gr.Row():
        with gr.Column():
            img_in = gr.Image(type="pil", label="Upload image", sources=["upload", "webcam"])
        with gr.Column():
            processed = gr.Image(type="pil", label="Preprocessed (224×224)")
    topk = gr.Label(num_top_classes=5, label="Top-5 predictions")
    img_in.change(predict, inputs=img_in, outputs=[topk, processed])
    gr.Examples(examples=EXAMPLES, inputs=[img_in], cache_examples=False)

if __name__ == "__main__":
    demo.launch()