mostafasmart commited on
Commit
2c20728
·
1 Parent(s): 221a7a1

Add application file

Browse files
Files changed (2) hide show
  1. app.py +56 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import easyocr
3
+ import requests
4
+ import numpy as np
5
+ from PIL import Image, UnidentifiedImageError
6
+ from io import BytesIO
7
+ import json
8
+
9
+ # إعداد قارئ EasyOCR
10
+ reader = easyocr.Reader(['en'], gpu=False)
11
+
12
+ def extract_numbers_from_url(image_url):
13
+ try:
14
+ # تحميل الصورة
15
+ response = requests.get(image_url, timeout=10)
16
+ response.raise_for_status()
17
+
18
+ content_type = response.headers.get('Content-Type')
19
+ if not content_type or not content_type.startswith('image'):
20
+ return json.dumps({"error": "الرابط لا يشير إلى صورة."})
21
+
22
+ image = Image.open(BytesIO(response.content)).convert("RGB")
23
+ image_np = np.array(image)
24
+
25
+ # قراءة النصوص باستخدام EasyOCR
26
+ results = reader.readtext(image_np)
27
+
28
+ # تصفية النتائج التي تحتوي على أرقام
29
+ digit_boxes = []
30
+ for bbox, text, confidence in results:
31
+ if any(char.isdigit() for char in text):
32
+ digit_boxes.append({
33
+ "text": text,
34
+ "confidence": round(float(confidence), 3),
35
+ "box": [[round(x, 2), round(y, 2)] for (x, y) in bbox]
36
+ })
37
+
38
+ return json.dumps(digit_boxes, indent=2)
39
+
40
+ except requests.exceptions.RequestException as e:
41
+ return json.dumps({"error": f"خطأ في تحميل الصورة: {str(e)}"})
42
+ except UnidentifiedImageError:
43
+ return json.dumps({"error": "الصورة غير قابلة للقراءة."})
44
+ except Exception as e:
45
+ return json.dumps({"error": f"حدث خطأ: {str(e)}"})
46
+
47
+ # واجهة Gradio
48
+ iface = gr.Interface(
49
+ fn=extract_numbers_from_url,
50
+ inputs=gr.Textbox(lines=2, placeholder="أدخل رابط الصورة هنا...", label="رابط الصورة"),
51
+ outputs="json",
52
+ title="كشف الأرقام باستخدام EasyOCR",
53
+ description="أدخل رابط صورة لاستخراج إحداثيات المناطق التي تحتوي على أرقام."
54
+ )
55
+
56
+ iface.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ easyocr
3
+ pillow
4
+ numpy
5
+ requests
6
+ matplotlib
7
+ opencv-python-headless