Sahar7888 commited on
Commit
a6be34e
·
verified ·
1 Parent(s): 5c96950

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +36 -0
  2. requirements.txt +6 -0
  3. sample.png +0 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import pytesseract
3
+ import gradio as gr
4
+
5
+ # OCR processing function
6
+ def extract_text_from_image(image):
7
+ try:
8
+ # Open the uploaded image from Gradio
9
+ image = image.resize((300, 150)) # Resize as required
10
+
11
+ # Tesseract custom config
12
+ custom_config = r'-l eng --oem 3 --psm 6'
13
+
14
+ # Extracting text using pytesseract
15
+ text = pytesseract.image_to_string(image, config=custom_config)
16
+
17
+ return text
18
+ except Exception as e:
19
+ return f"Error occurred: {e}"
20
+
21
+ # Load example image from local directory (make sure the path is correct)
22
+ example_image_path = "/content/sample.png" # Replace with the actual path
23
+ example_image = Image.open(example_image_path)
24
+
25
+ # Gradio Interface
26
+ iface = gr.Interface(
27
+ fn=extract_text_from_image, # Function to process OCR
28
+ inputs=gr.Image(type="pil", label="Upload Image"), # User uploads an image file
29
+ outputs="text", # Output extracted text
30
+ title="Image to Text OCR",
31
+ description="Upload an image file to extract text using OCR. Use the example image for testing.",
32
+ examples=[[example_image]] # Preload the example image
33
+ )
34
+
35
+ # Launch the interface
36
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+
2
+ pillow
3
+ opencv-python
4
+ gradio
5
+ pip==24.2
6
+ pytesseract
sample.png ADDED