GiantAnalytics commited on
Commit
ef9ba78
·
verified ·
1 Parent(s): e880c5e

first version

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import paddle
3
+ from paddleocr import PaddleOCR, draw_ocr
4
+ import cv2
5
+ from PIL import Image
6
+ import numpy as np
7
+
8
+ # Initialize PaddleOCR for Arabic
9
+ ocr = PaddleOCR(use_angle_cls=True, lang='ar')
10
+
11
+ def ocr_extract_text(image):
12
+ # Perform OCR on the uploaded image
13
+ result = ocr.ocr(image, cls=True)
14
+
15
+ # Initialize a list to store detected text
16
+ detected_text = []
17
+
18
+ # Loop through the results and extract text
19
+ for line in result[0]: # result[0] contains the OCR results for text regions
20
+ text = line[1][0] # Detected text
21
+ detected_text.append(text)
22
+
23
+ # Draw the OCR results on the image
24
+ boxes = [line[0] for line in result[0]] # Bounding box coordinates
25
+ texts = [line[1][0] for line in result[0]] # Detected text
26
+ scores = [line[1][1] for line in result[0]] # Confidence scores
27
+
28
+ annotated_image = draw_ocr(image, boxes, texts, scores, font_path="path/to/arabic/font.ttf")
29
+
30
+ # Convert the annotated image to a format that Gradio can display
31
+ annotated_image = Image.fromarray(np.uint8(annotated_image))
32
+
33
+ # Return the text and annotated image
34
+ return "\n".join(detected_text), annotated_image
35
+
36
+ # Define Gradio interface with the button to trigger OCR
37
+ def start_ocr(image):
38
+ # Trigger OCR extraction when button is clicked
39
+ return ocr_extract_text(image)
40
+
41
+ iface = gr.Interface(
42
+ fn=start_ocr, # Function to trigger OCR extraction on button click
43
+ inputs=gr.Image(type="pil"), # Image input only
44
+ outputs=[gr.Textbox(label="Extracted Text"), gr.Image(label="Annotated Image")], # Outputs
45
+ live=False, # Set live=False as we want to trigger the process with a button click
46
+ title="Arabic OCR Extractor", # Title of the interface
47
+ description="Upload an Arabic document or image, and click 'Start Extracting Text' to extract the text using OCR.", # Description
48
+ allow_flagging="never" # Prevent flagging if it's not required
49
+ )
50
+
51
+ if __name__ == "__main__":
52
+ # This ensures Gradio runs in debug mode and with queue for better debugging
53
+ iface.queue().launch(debug=True)