Sakshiw1 commited on
Commit
66f6eb4
·
verified ·
1 Parent(s): 53dd0fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -32
app.py CHANGED
@@ -1,32 +1,37 @@
1
- import gradio as gr
2
- import cv2
3
- import pytesseract
4
-
5
- # Function to process the image
6
- def preprocess_image_for_tesseract(image):
7
- # Convert the image to grayscale
8
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
9
- return gray
10
-
11
- def ocr_and_search(image, keyword):
12
- processed_image = preprocess_image_for_tesseract(image)
13
-
14
- # Extract text in both Hindi and English
15
- extracted_text = pytesseract.image_to_string(processed_image, lang='hin+eng')
16
-
17
- # Search for the keyword in the extracted text (case insensitive)
18
- search_results = [line for line in extracted_text.split('\n') if keyword.lower() in line.lower()]
19
-
20
- return extracted_text, search_results
21
-
22
- # Create Gradio interface
23
- iface = gr.Interface(
24
- fn=ocr_and_search,
25
- inputs=[gr.Image(type="numpy"), gr.Textbox(label="Keyword")],
26
- outputs=["text", "text"],
27
- title="OCR and Keyword Search",
28
- description="Upload an image with text and search for a keyword."
29
- )
30
-
31
- # Launch the interface
32
- iface.launch(share=True)
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import easyocr
4
+
5
+ # Initialize EasyOCR Reader
6
+ reader = easyocr.Reader(['en', 'hi'])
7
+
8
+ # Function to process the image
9
+ def preprocess_image(image):
10
+ # Convert the image to BGR format for EasyOCR
11
+ return cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
12
+
13
+ def ocr_and_search(image, keyword):
14
+ processed_image = preprocess_image(image)
15
+
16
+ # Extract text in both Hindi and English
17
+ results = reader.readtext(processed_image)
18
+
19
+ # Combine extracted text into a single string
20
+ extracted_text = "\n".join([result[1] for result in results])
21
+
22
+ # Search for the keyword in the extracted text (case insensitive)
23
+ search_results = [line for line in extracted_text.split('\n') if keyword.lower() in line.lower()]
24
+
25
+ return extracted_text, search_results
26
+
27
+ # Create Gradio interface
28
+ iface = gr.Interface(
29
+ fn=ocr_and_search,
30
+ inputs=[gr.Image(type="numpy"), gr.Textbox(label="Keyword")],
31
+ outputs=["text", "text"],
32
+ title="OCR and Keyword Search",
33
+ description="Upload an image with text and search for a keyword."
34
+ )
35
+
36
+ # Launch the interface
37
+ iface.launch(share=True)