File size: 4,322 Bytes
d5841ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
Text Detection Demo with Gradio
Extract text from images using OCR
"""
import gradio as gr
import cv2
import pytesseract
import numpy as np
from PIL import Image
import os

# Set Tesseract path (will be overridden in cloud deployment)
if os.path.exists(r'C:\Program Files\Tesseract-OCR\tesseract.exe'):
    pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'


def extract_text_from_image(image):
    """
    Extract text from an uploaded image
    Args:
        image: PIL Image or numpy array
    Returns:
        tuple: (processed_image, extracted_text)
    """
    try:
        # Convert PIL Image to numpy array if needed
        if isinstance(image, Image.Image):
            image = np.array(image)
        
        # Convert RGB to BGR for OpenCV
        if len(image.shape) == 3 and image.shape[2] == 3:
            img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        else:
            img = image
        
        # Convert to grayscale
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        
        # Apply thresholding for better OCR
        _, threshold = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        
        # Extract text using Tesseract
        text = pytesseract.image_to_string(threshold)
        
        # Clean up the text
        text = text.strip()
        
        if not text:
            text = "⚠️ No text detected in the image.\n\nTips:\n- Make sure the image contains clear text\n- Try an image with higher resolution\n- Ensure good contrast between text and background"
        
        # Convert processed image back to RGB for display
        processed_display = cv2.cvtColor(threshold, cv2.COLOR_GRAY2RGB)
        
        return processed_display, text
    
    except Exception as e:
        error_msg = f"❌ Error processing image: {str(e)}\n\nPlease try another image."
        return image, error_msg


# Create Gradio interface
with gr.Blocks(theme=gr.themes.Soft(), title="Text Detection Demo") as demo:
    
    gr.Markdown(
        """
        # πŸ“ Text Detection Demo
        ### Extract text from any image using OCR
        
        Upload an image containing text, and the AI will extract all readable text from it.
        Perfect for documents, screenshots, photos of signs, and more!
        """
    )
    
    with gr.Row():
        with gr.Column():
            input_image = gr.Image(
                label="Upload Image",
                type="pil",
                height=400
            )
            
            extract_btn = gr.Button(
                "πŸ” Extract Text",
                variant="primary",
                size="lg"
            )
            
            gr.Markdown(
                """
                ### πŸ’‘ Tips for best results:
                - Use clear, high-resolution images
                - Ensure good lighting and contrast
                - Avoid blurry or distorted text
                - Works with printed and digital text
                """
            )
        
        with gr.Column():
            output_image = gr.Image(
                label="Processed Image (Thresholded)",
                height=400
            )
            
            output_text = gr.Textbox(
                label="Extracted Text",
                lines=10,
                placeholder="Extracted text will appear here...",
                show_copy_button=True
            )
    
    # Example images section
    gr.Markdown("### πŸ“Έ Try these examples:")
    gr.Examples(
        examples=[
            ["inputs/test1.jpg"] if os.path.exists("inputs/test1.jpg") else None,
            ["inputs/test2.jpg"] if os.path.exists("inputs/test2.jpg") else None,
        ],
        inputs=input_image,
        label="Sample Images"
    )
    
    # Connect the button to the function
    extract_btn.click(
        fn=extract_text_from_image,
        inputs=input_image,
        outputs=[output_image, output_text]
    )
    
    # Footer
    gr.Markdown(
        """
        ---
        Made with ❀️ using Gradio and Tesseract OCR
        """
    )

# Launch the app
if __name__ == "__main__":
    demo.launch(
        share=True,  # Creates a public shareable link
        server_name="0.0.0.0",  # Allow external connections
        server_port=7860
    )