sprakhil commited on
Commit
71deb6b
·
1 Parent(s): 25b842d

implemeted ocr

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import easyocr
3
+ import gradio as gr
4
+ from PIL import Image
5
+
6
+ reader = easyocr.Reader(['en', 'hi'], gpu=False)
7
+
8
+ def convert_hindi_numerals_to_arabic(text):
9
+ hindi_to_arabic = {
10
+ '०': '0', '१': '1', '२': '2', '३': '3', '४': '4',
11
+ '५': '5', '६': '6', '७': '7', '८': '8', '९': '9'
12
+ }
13
+ for hindi, arabic in hindi_to_arabic.items():
14
+ text = text.replace(hindi, arabic)
15
+ return text
16
+
17
+ def extract_text(image_path):
18
+ img = Image.open(image_path)
19
+
20
+ temp_jpg_path = "temp_image.jpg"
21
+ img.convert("RGB").save(temp_jpg_path,"JPEG")
22
+
23
+ result = reader.readtext(temp_jpg_path, detail=0)
24
+ extracted_text = " ".join(result)
25
+ extracted_text = convert_hindi_numerals_to_arabic(extracted_text)
26
+
27
+ os.remove(temp_jpg_path)
28
+
29
+ return extracted_text
30
+
31
+ def search_text(image_path, keyword):
32
+ extracted_text = extract_text(image_path)
33
+ if keyword.lower() in extracted_text.lower():
34
+ return f"Keyword '{keyword}' found in the extracted text.", extracted_text
35
+ else:
36
+ return f"Keyword '{keyword}' not found in the extracted text.", extracted_text
37
+
38
+ def create_interface():
39
+ interface = gr.Interface(
40
+ fn=search_text,
41
+ inputs=[
42
+ gr.Image(type="filepath", label="Upload Image"), # Changed to filepath
43
+ gr.Textbox(lines=1, placeholder="Enter keyword to search", label="Keyword")
44
+ ],
45
+ outputs=[
46
+ gr.Textbox(label="Search Result"),
47
+ gr.Textbox(label="Extracted Text")
48
+ ],
49
+ title="OCR and Keyword Search Application",
50
+ description="Upload an image containing text in English or Hindi. Enter a keyword to search within the extracted text.",
51
+ )
52
+
53
+ return interface
54
+
55
+ if __name__ == "__main__":
56
+ interface = create_interface()
57
+ interface.launch(share=True)