Spaces:
Sleeping
Sleeping
| """ | |
| 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 | |
| ) | |