GiantAnalytics commited on
Commit
4cb0d47
·
verified ·
1 Parent(s): 0c480bb

first Version

Browse files
Files changed (1) hide show
  1. appeasy.py +103 -0
appeasy.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import easyocr
3
+ import cv2
4
+ import numpy as np
5
+ from PIL import Image, ImageDraw, ImageFont
6
+ import os
7
+ import requests
8
+ from pathlib import Path
9
+
10
+ # Download and cache the font file
11
+ def get_font():
12
+ font_path = Path("Roboto-Regular.ttf")
13
+ if not font_path.exists():
14
+ font_url = "https://github.com/google/fonts/raw/main/apache/roboto/Roboto-Regular.ttf"
15
+ response = requests.get(font_url)
16
+ font_path.write_bytes(response.content)
17
+ return str(font_path)
18
+
19
+ # Initialize EasyOCR Reader for Arabic and English
20
+ reader = easyocr.Reader(['ar', 'en'], gpu=True) # Set gpu=False if no GPU available
21
+
22
+ def ocr_extract_text(image):
23
+ if image is None:
24
+ return "No image provided", None
25
+
26
+ # Convert to RGB if needed
27
+ if len(image.shape) == 3 and image.shape[2] == 4: # RGBA
28
+ image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
29
+
30
+ # Perform OCR
31
+ results = reader.readtext(image)
32
+
33
+ if not results:
34
+ return "No text detected in the image", image
35
+
36
+ # Prepare text output and confidence scores
37
+ detected_text = []
38
+ for (_, text, confidence) in results:
39
+ detected_text.append(f"{text} (Confidence: {confidence:.2f})")
40
+
41
+ # Create annotated image
42
+ pil_image = Image.fromarray(image)
43
+ draw = ImageDraw.Draw(pil_image)
44
+
45
+ # Get font for annotation
46
+ try:
47
+ font = ImageFont.truetype(get_font(), size=20)
48
+ except Exception as e:
49
+ print(f"Error loading font: {e}")
50
+ font = ImageFont.load_default()
51
+
52
+ # Draw boxes and text
53
+ for (bbox, text, confidence) in results:
54
+ # Convert points to integers
55
+ top_left = tuple(map(int, bbox[0]))
56
+ bottom_right = tuple(map(int, bbox[2]))
57
+
58
+ # Draw rectangle
59
+ draw.rectangle([top_left, bottom_right], outline="red", width=3)
60
+
61
+ # Draw text with confidence
62
+ text_with_conf = f"{text} ({confidence:.2f})"
63
+ draw.text(top_left, text_with_conf, fill="blue", font=font)
64
+
65
+ # Convert back to numpy array
66
+ annotated_image = np.array(pil_image)
67
+
68
+ # Join detected text with proper formatting
69
+ text_output = "\n".join(detected_text)
70
+
71
+ return text_output, annotated_image
72
+
73
+ # Custom CSS for RTL support and better text display
74
+ css = """
75
+ .output-text {
76
+ direction: rtl;
77
+ text-align: right;
78
+ font-family: Arial, sans-serif;
79
+ white-space: pre-wrap;
80
+ }
81
+ """
82
+
83
+ # Create Gradio interface
84
+ iface = gr.Interface(
85
+ fn=ocr_extract_text,
86
+ inputs=gr.Image(type="numpy", label="Upload Image"),
87
+ outputs=[
88
+ gr.Textbox(
89
+ label="Extracted Text (Arabic & English)",
90
+ elem_classes=["output-text"]
91
+ ),
92
+ gr.Image(label="Annotated Image")
93
+ ],
94
+ title="Arabic & English OCR Extractor",
95
+ description="Upload an image containing Arabic and/or English text for OCR processing. The system will detect and extract text in both languages.",
96
+ css=css,
97
+ examples=[], # You can add example images here
98
+ cache_examples=True
99
+ )
100
+
101
+ # Launch the interface
102
+ if __name__ == "__main__":
103
+ iface.launch(debug=True, share=True)