| import cv2 | |
| import numpy as np | |
| import gradio as gr | |
| import pytesseract | |
| def ocr_image(img): | |
| """Performs OCR on an image using OpenCV and PyTesseract.""" | |
| # Preprocess image (optional) | |
| gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
| thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] | |
| # OCR using PyTesseract | |
| text = pytesseract.image_to_string(thresh) | |
| return text | |
| # Create the Gradio interface | |
| iface = gr.Interface( | |
| fn=ocr_image, | |
| inputs="image", | |
| outputs="text", | |
| title="OCR with OpenCV and PyTesseract" | |
| ) | |
| iface.launch() | |