ahmedda commited on
Commit
70fa7d7
·
verified ·
1 Parent(s): e781b5c

Update ui.py

Browse files
Files changed (1) hide show
  1. ui.py +16 -38
ui.py CHANGED
@@ -1,43 +1,32 @@
1
  import streamlit as st
2
  import tensorflow as tf
3
  from PIL import Image
4
- import io, base64, numpy as np
5
  import pandas as pd
6
  import re
7
 
8
- # إعداد الصفحة لتكون بشكل احترافي
9
  st.set_page_config(page_title="مترجم لغة الإشارة", layout="centered")
10
 
11
- # دالة تنظيف النص العربي لضمان تطابق الكلمات مع الإكسل
12
  def normalize_arabic(text):
13
  text = str(text).strip()
14
  text = re.sub("[إأآ]", "ا", text)
15
  text = re.sub("ة", "ه", text)
16
  return text
17
 
18
- # تحميل الموديل والبيانات مرة واحدة فقط (لتسريع الأداء)
19
  @st.cache_resource
20
- def load_all_assets():
21
- # تحميل ملف الإكسل الخاص بالكلمات
22
  df = pd.read_excel("KARSL-100_Labels (1).xlsx")
23
- # إنشاء قاموس لربط الكلمة بالـ ID
24
  word_to_id = {normalize_arabic(row['Sign-Arabic']): row['SignID'] for _, row in df.iterrows()}
25
- # تحميل موديل التنبؤ بالصور
26
  model = tf.keras.models.load_model("word_to_sign_model.h5", compile=False)
27
  return word_to_id, model
28
 
29
- # تنفيذ عملية التحميل
30
  try:
31
- word_to_id, text_to_sign_model = load_all_assets()
32
  except Exception as e:
33
- st.error(f"خطأ في تحميل الملفات الأساسية: {e}")
34
 
35
- # واجهة المستخدم الرسومية
36
  st.title("📂 مترجم النص إلى لغة إشارة")
37
- st.write("اكتب الكلمة لتحويلها إلى صورة إشارة (مثال: هيكل عظمي، مناعة، قصبة هوائية)")
38
-
39
- # حقل إدخال الكلمة
40
- word_input = st.text_input("أدخل الكلمة بالعربية:", key="word_input")
41
 
42
  if st.button("تحويل"):
43
  if word_input:
@@ -45,38 +34,27 @@ if st.button("تحويل"):
45
  label_id = word_to_id.get(clean_text)
46
 
47
  if label_id is not None:
48
- # 1. تجهيز المدخلات للموديل (One-Hot Encoding)
49
  one_hot_input = np.zeros((1, 100), dtype=np.float32)
50
  idx = int(label_id) - 1 if int(label_id) > 0 else 0
51
- if idx < 100:
52
- one_hot_input[0, idx] = 1.0
53
 
54
- # 2. التنبؤ بالصورة باستخدام الموديل
55
- with st.spinner('جاري معالجة الصورة...'):
56
- prediction = text_to_sign_model.predict(one_hot_input)
57
 
58
- # 3. معالجة مصفوفة الصورة (هنا حل مشكلة الخط الأسود)
59
  img_data = prediction[0]
 
 
 
60
 
61
- # تحويل القيم لمجال الألوان الطبيعي (0-255)
62
- if img_data.max() <= 1.0:
63
- img_data = (img_data * 255).astype("uint8")
64
- else:
65
- img_data = img_data.astype("uint8")
66
-
67
- # ضبط الأبعاد إذا كانت الصورة Grayscale (أبيض وأسود)
68
  if len(img_data.shape) == 3 and img_data.shape[-1] == 1:
69
  img_data = img_data.reshape(img_data.shape[0], img_data.shape[1])
70
 
71
- # تحويل المصفوفة لصورة PIL وضبط نمط الألوان
72
- img = Image.fromarray(img_data)
73
- if img.mode != 'L':
74
- img = img.convert('L')
75
 
76
- # عرض النتيجة
77
  st.success(f"✅ تم العثور على إشارة: {word_input}")
78
  st.image(img, caption=f"إشارة {word_input}", width=400)
79
  else:
80
- st.error(f"❌ الكلمة '{word_input}' غير موجودة في القاموس.")
81
- else:
82
- st.warning("⚠️ يرجى كتابة كلمة أولاً.")
 
1
  import streamlit as st
2
  import tensorflow as tf
3
  from PIL import Image
4
+ import numpy as np
5
  import pandas as pd
6
  import re
7
 
 
8
  st.set_page_config(page_title="مترجم لغة الإشارة", layout="centered")
9
 
 
10
  def normalize_arabic(text):
11
  text = str(text).strip()
12
  text = re.sub("[إأآ]", "ا", text)
13
  text = re.sub("ة", "ه", text)
14
  return text
15
 
 
16
  @st.cache_resource
17
+ def load_all():
 
18
  df = pd.read_excel("KARSL-100_Labels (1).xlsx")
 
19
  word_to_id = {normalize_arabic(row['Sign-Arabic']): row['SignID'] for _, row in df.iterrows()}
 
20
  model = tf.keras.models.load_model("word_to_sign_model.h5", compile=False)
21
  return word_to_id, model
22
 
 
23
  try:
24
+ word_to_id, text_to_sign_model = load_all()
25
  except Exception as e:
26
+ st.error(f"خطأ في تحميل الملفات: {e}")
27
 
 
28
  st.title("📂 مترجم النص إلى لغة إشارة")
29
+ word_input = st.text_input("أدخل الكلمة بالعربية:", placeholder="مثلاً: هيكل عظمي")
 
 
 
30
 
31
  if st.button("تحويل"):
32
  if word_input:
 
34
  label_id = word_to_id.get(clean_text)
35
 
36
  if label_id is not None:
 
37
  one_hot_input = np.zeros((1, 100), dtype=np.float32)
38
  idx = int(label_id) - 1 if int(label_id) > 0 else 0
39
+ if idx < 100: one_hot_input[0, idx] = 1.0
 
40
 
41
+ # التنبؤ
42
+ prediction = text_to_sign_model.predict(one_hot_input)
 
43
 
44
+ # --- معالجة الصورة للعرض (الحل هنا) ---
45
  img_data = prediction[0]
46
+ # 1. تحجيم القيم لـ 0-255
47
+ img_data = (img_data - img_data.min()) / (img_data.max() - img_data.min() + 1e-5)
48
+ img_data = (img_data * 255).astype("uint8")
49
 
50
+ # 2. التأكد من الأبعاد
 
 
 
 
 
 
51
  if len(img_data.shape) == 3 and img_data.shape[-1] == 1:
52
  img_data = img_data.reshape(img_data.shape[0], img_data.shape[1])
53
 
54
+ # 3. تحويلها لصورة واضحة
55
+ img = Image.fromarray(img_data).convert("L")
 
 
56
 
 
57
  st.success(f"✅ تم العثور على إشارة: {word_input}")
58
  st.image(img, caption=f"إشارة {word_input}", width=400)
59
  else:
60
+ st.error("الكلمة غير موجودة في القاموس")