File size: 2,311 Bytes
0fca60a
 
 
 
 
dc81505
 
137ebc2
1530884
421706c
fde596e
fcc6228
0fca60a
2a67502
0fca60a
 
 
 
 
 
 
 
 
1530884
 
 
 
 
 
 
 
 
 
 
0fca60a
65d5b0d
fde596e
c17576b
1530884
fde596e
0fca60a
4646611
 
c17576b
1530884
 
 
 
 
 
0fca60a
fde596e
 
 
 
1530884
 
 
 
c17576b
fde596e
 
1530884
dc81505
65d5b0d
0fca60a
2a67502
 
fde596e
1530884
fde596e
2a67502
 
 
fde596e
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
68
69
70
71
72
73
74
75
import gradio as gr
import cv2
from ultralytics import YOLO
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import arabic_reshaper
from bidi.algorithm import get_display
import os
import requests

# 1. Load Model
model = YOLO("best.pt")

# 2. Arabic letter mapping
arabic_letters = {
    "ain": "ع", "al": "لا", "aleff": "ا", "bb": "ب", "dal": "د", "dha": "ذ",
    "dhad": "ض", "fa": "ف", "gaaf": "ق", "ghain": "غ", "ha": "ه", "haa": "ح",
    "jeem": "ج", "kaaf": "ك", "khaa": "خ", "la": "ل", "laam": "ل", "meem": "م",
    "nun": "ن", "ra": "ر", "saad": "ص", "seen": "س", "sheen": "ش", "ta": "ط",
    "taa": "ت", "thaa": "ث", "thal": "ظ", "toot": "ط", "waw": "و", "ya": "ي",
    "yaa": "ي", "zay": "ز"
}

# --- BULLETPROOF FONT DOWNLOAD ---
FONT_PATH = "Amiri-Regular.ttf"
if not os.path.exists(FONT_PATH):
    print("📥 Downloading Arabic font...")
    try:
        res = requests.get("https://github.com/google/fonts/raw/main/ofl/amiri/Amiri-Regular.ttf")
        with open(FONT_PATH, "wb") as f:
            f.write(res.content)
        print("✅ Font ready!")
    except:
        FONT_PATH = None

def predict_live(img):
    if img is None: return None

    # Speed optimization
    results = model(img, conf=0.3, imgsz=320, verbose=False)
    
    pil_img = Image.fromarray(img)
    draw = ImageDraw.Draw(pil_img)

    # Load font
    try:
        font = ImageFont.truetype(FONT_PATH, 60) if FONT_PATH else ImageFont.load_default()
    except:
        font = ImageFont.load_default()

    for r in results:
        for box in r.boxes:
            x1, y1, x2, y2 = map(int, box.xyxy[0])
            cls_name = model.names[int(box.cls[0])]

            # Arabic Transformation
            raw_text = arabic_letters.get(cls_name, cls_name)
            reshaped_text = arabic_reshaper.reshape(raw_text)
            bidi_text = get_display(reshaped_text)

            # Draw
            draw.rectangle([x1, y1, x2, y2], outline=(0, 255, 0), width=5)
            draw.text((x1, y1 - 75), bidi_text, font=font, fill=(0, 255, 0))

    return np.array(pil_img)

demo = gr.Interface(
    fn=predict_live,
    inputs=gr.Image(sources=["webcam"], streaming=True),
    outputs=gr.Image(label="Translator"),
    live=True
)

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