mostafasmart commited on
Commit
d9ed74d
·
1 Parent(s): 0895b46

Add application filesfSs

Browse files
Files changed (1) hide show
  1. app.py +18 -22
app.py CHANGED
@@ -7,29 +7,32 @@ import requests
7
  from io import BytesIO
8
  import json
9
 
 
10
  reader = easyocr.Reader(['en'])
11
 
12
- def calculate_area(coords):
13
- return (coords['x_max'] - coords['x_min']) * (coords['y_max'] - coords['y_min'])
14
-
15
  def detect_digits(image_url):
16
  try:
 
17
  response = requests.get(image_url)
18
  response.raise_for_status()
19
 
 
20
  image = Image.open(BytesIO(response.content)).convert('RGB')
21
  img_np = np.array(image)
22
  img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
23
 
 
24
  results = reader.readtext(img_cv)
25
 
 
26
  output = []
27
  for bbox, text, conf in results:
28
  if any(c.isdigit() for c in text):
 
29
  tl = tuple(map(int, bbox[0]))
30
  br = tuple(map(int, bbox[2]))
31
 
32
- entry = {
33
  "text": text,
34
  "confidence": float(conf),
35
  "coordinates": {
@@ -38,31 +41,24 @@ def detect_digits(image_url):
38
  "x_max": br[0],
39
  "y_max": br[1]
40
  }
41
- }
42
- entry["area"] = calculate_area(entry["coordinates"])
43
- output.append(entry)
44
-
45
- # ترتيب النتائج حسب المساحة تنازلياً
46
- sorted_output = sorted(output, key=lambda x: x['area'], reverse=True)
47
-
48
- # أخذ أكبر 3 نتائج فقط
49
- top_three = sorted_output[:3]
50
-
51
- # إزالة حقل المساحة من النتيجة النهائية
52
- for item in top_three:
53
- item.pop('area', None)
54
 
55
- return json.dumps(top_three, indent=2, ensure_ascii=False)
56
 
57
  except Exception as e:
58
  return json.dumps({"error": str(e)})
59
 
 
60
  interface = gr.Interface(
61
  fn=detect_digits,
62
- inputs=gr.Textbox(label="رابط الصورة", placeholder="أدخل رابط الصورة هنا..."),
63
- outputs=gr.JSON(label="النتائج"),
64
- title="كشف المناطق الرقمية",
65
- description="أدخل رابط صورة لاكتشاف أكبر 3 مناطق تحتوي على أرقام"
 
 
 
 
66
  )
67
 
68
  interface.launch()
 
7
  from io import BytesIO
8
  import json
9
 
10
+ # Initialize EasyOCR once
11
  reader = easyocr.Reader(['en'])
12
 
 
 
 
13
  def detect_digits(image_url):
14
  try:
15
+ # Download the image
16
  response = requests.get(image_url)
17
  response.raise_for_status()
18
 
19
+ # Convert to OpenCV format
20
  image = Image.open(BytesIO(response.content)).convert('RGB')
21
  img_np = np.array(image)
22
  img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
23
 
24
+ # Perform text detection
25
  results = reader.readtext(img_cv)
26
 
27
+ # Process results
28
  output = []
29
  for bbox, text, conf in results:
30
  if any(c.isdigit() for c in text):
31
+ # Extract coordinates
32
  tl = tuple(map(int, bbox[0]))
33
  br = tuple(map(int, bbox[2]))
34
 
35
+ output.append({
36
  "text": text,
37
  "confidence": float(conf),
38
  "coordinates": {
 
41
  "x_max": br[0],
42
  "y_max": br[1]
43
  }
44
+ })
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
+ return json.dumps(output, indent=2, ensure_ascii=False)
47
 
48
  except Exception as e:
49
  return json.dumps({"error": str(e)})
50
 
51
+ # Create Gradio interface
52
  interface = gr.Interface(
53
  fn=detect_digits,
54
+ inputs=gr.Textbox(
55
+ label="Image URL",
56
+ placeholder="Enter image URL here..."
57
+ ),
58
+ outputs=gr.JSON(label="Detection Results"),
59
+ title="Digit Region Detection",
60
+ description="Upload an image URL to detect regions containing numbers",
61
+ allow_flagging="never"
62
  )
63
 
64
  interface.launch()