YOUKKASS commited on
Commit
da1975c
·
verified ·
1 Parent(s): 0cbe373

Update modules/renderer.py

Browse files
Files changed (1) hide show
  1. modules/renderer.py +30 -1
modules/renderer.py CHANGED
@@ -2,6 +2,35 @@ from PIL import Image, ImageDraw, ImageFont
2
  import os
3
  import arabic_reshaper
4
  from bidi.algorithm import get_display
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  def get_font(config):
7
  """الحصول على خط مناسب للغة العربية"""
@@ -38,7 +67,7 @@ def render_text(image_path, boxes, texts, save_path="output.png", config=None):
38
 
39
  # رسم كل نص في المربع المناسب
40
  for box, text in zip(boxes, texts):
41
- if not text: # تخطي النصوص الفارغة
42
  continue
43
 
44
  # معالجة النص العربي للعرض الصحيح
 
2
  import os
3
  import arabic_reshaper
4
  from bidi.algorithm import get_display
5
+ import cv2
6
+ import numpy as np
7
+
8
+ def preprocess_image(image_path):
9
+ """معالجة مسبقة للصورة لتحسين جودة OCR"""
10
+ try:
11
+ # قراءة الصورة
12
+ image = cv2.imread(image_path)
13
+ if image is None:
14
+ return image_path
15
+
16
+ # تحويل إلى تدرج الرمادي
17
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
18
+
19
+ # زيادة التباين
20
+ enhanced = cv2.convertScaleAbs(gray, alpha=1.5, beta=30)
21
+
22
+ # إزالة الضوضاء
23
+ denoised = cv2.medianBlur(enhanced, 3)
24
+
25
+ # حفظ الصورة المحسنة
26
+ enhanced_path = "enhanced_image.png"
27
+ cv2.imwrite(enhanced_path, denoised)
28
+
29
+ return enhanced_path
30
+
31
+ except Exception as e:
32
+ print(f"❌ خطأ في معالجة الصورة: {e}")
33
+ return image_path
34
 
35
  def get_font(config):
36
  """الحصول على خط مناسب للغة العربية"""
 
67
 
68
  # رسم كل نص في المربع المناسب
69
  for box, text in zip(boxes, texts):
70
+ if not text or text == "[نص غير معروف]": # تخطي النصوص الفارغة
71
  continue
72
 
73
  # معالجة النص العربي للعرض الصحيح