EngReem85 commited on
Commit
3f28629
·
verified ·
1 Parent(s): 51403ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -24,18 +24,14 @@ def predict_sketch(image):
24
  تستقبل الصورة من لوحة الرسم، تعالجها، وتعيد الاحتمالات.
25
  """
26
  try:
27
- # تحويل الصورة إلى تدرج رمادي
28
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
29
- # تغيير الحجم إلى 28×28
30
  resized = cv2.resize(gray, (28, 28))
31
- # قلب الألوان (نفترض أن الخلفية سوداء والرسم أبيض)
32
  inverted = 255 - resized
33
- # تطبيع القيم إلى [0,1]
34
  normalized = inverted.astype('float32') / 255.0
35
- # إعادة التشكيل لتناسب CNN
36
  reshaped = normalized.reshape(1, 28, 28, 1)
37
 
38
- # التنبؤ
39
  prediction = model.predict(reshaped, verbose=0)[0]
40
  return {str(i): float(prediction[i]) for i in range(10)}
41
  except Exception as e:
@@ -47,13 +43,13 @@ def predict_sketch(image):
47
  def random_example():
48
  idx = np.random.randint(0, len(test_images))
49
  img = test_images[idx].reshape(28, 28)
50
- # تحويل إلى 3 قنوات (RGB) لأن Sketchpad يتوقع صورة ملونة
51
  img_rgb = np.stack([img] * 3, axis=2)
52
  info_text = f"🖼️ مثال عشوائي رقم {idx} | الرقم الحقيقي: {test_labels[idx]}"
53
  return img_rgb, info_text
54
 
55
  # ============================================
56
- # 5. بناء واجهة Gradio (باستخدام Blocks)
57
  # ============================================
58
  with gr.Blocks(title="MNIST Digit Recognizer") as demo:
59
  gr.Markdown("# 🧠 التعرف على الأرقام المكتوبة بخط اليد")
@@ -61,13 +57,13 @@ with gr.Blocks(title="MNIST Digit Recognizer") as demo:
61
 
62
  with gr.Row():
63
  with gr.Column(scale=1):
64
- # 🔥 تم إزالة image_size واستخدام width/height (مدعومة في جميع الإصدارات)
65
- sketch = gr.Sketchpad(
 
66
  label="✏️ ارسم هنا",
67
- brush_color="#FFFFFF",
68
- brush_radius=12,
69
  height=280,
70
- width=280
 
71
  )
72
  with gr.Row():
73
  submit_btn = gr.Button("🔮 توقع", variant="primary")
@@ -82,7 +78,7 @@ with gr.Blocks(title="MNIST Digit Recognizer") as demo:
82
  random_btn.click(fn=random_example, inputs=[], outputs=[sketch, info])
83
 
84
  # ============================================
85
- # 6. تشغيل التطبيق (ننقل theme إلى هنا)
86
  # ============================================
87
  if __name__ == "__main__":
88
- demo.launch(share=True, theme=gr.themes.Soft())
 
24
  تستقبل الصورة من لوحة الرسم، تعالجها، وتعيد الاحتمالات.
25
  """
26
  try:
27
+ # الصورة قادمة بصيغة (height, width, 3) RGB
28
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
 
29
  resized = cv2.resize(gray, (28, 28))
30
+ # قلب الألوان (افتراض أن الخلفية سوداء والرسم أبيض)
31
  inverted = 255 - resized
 
32
  normalized = inverted.astype('float32') / 255.0
 
33
  reshaped = normalized.reshape(1, 28, 28, 1)
34
 
 
35
  prediction = model.predict(reshaped, verbose=0)[0]
36
  return {str(i): float(prediction[i]) for i in range(10)}
37
  except Exception as e:
 
43
  def random_example():
44
  idx = np.random.randint(0, len(test_images))
45
  img = test_images[idx].reshape(28, 28)
46
+ # تحويل إلى 3 قنوات (RGB) لأن مكون Image يتوقع صورة ملونة
47
  img_rgb = np.stack([img] * 3, axis=2)
48
  info_text = f"🖼️ مثال عشوائي رقم {idx} | الرقم الحقيقي: {test_labels[idx]}"
49
  return img_rgb, info_text
50
 
51
  # ============================================
52
+ # 5. بناء واجهة Gradio باستخدام gr.Image (مع خاصية الرسم)
53
  # ============================================
54
  with gr.Blocks(title="MNIST Digit Recognizer") as demo:
55
  gr.Markdown("# 🧠 التعرف على الأرقام المكتوبة بخط اليد")
 
57
 
58
  with gr.Row():
59
  with gr.Column(scale=1):
60
+ # 🔥 استخدام gr.Image مع sources=['sketch'] لإنشاء لوحة رسم
61
+ sketch = gr.Image(
62
+ sources=['sketch'],
63
  label="✏️ ارسم هنا",
 
 
64
  height=280,
65
+ width=280,
66
+ type="numpy" # تعيد الصورة كمصفوفة NumPy
67
  )
68
  with gr.Row():
69
  submit_btn = gr.Button("🔮 توقع", variant="primary")
 
78
  random_btn.click(fn=random_example, inputs=[], outputs=[sketch, info])
79
 
80
  # ============================================
81
+ # 6. تشغيل التطبيق
82
  # ============================================
83
  if __name__ == "__main__":
84
+ demo.launch(share=True)