from typing import List import pytesseract from PIL import Image import gradio as gr import cv2 import numpy as np def tesseract_ocr_with_selection(filepath: str, coordinates: List[int] = None): # Görseli yükle image = Image.open(filepath) if coordinates: # Koordinatlara göre kırp x1, y1, x2, y2 = coordinates image = image.crop((x1, y1, x2, y2)) # OCR işlemi (varsayılan dil: İngilizce) return pytesseract.image_to_string(image=image, lang='eng') # Gradio UI ayarları title = "Tesseract OCR with Selection" description = "Gradio demo for Tesseract OCR with region selection (default language: English)." article = "
" examples = [ ['examples/eurotext.png', [50, 50, 200, 200]], ['examples/tesseract_sample.png', [30, 40, 150, 120]], ] with gr.Blocks() as demo: with gr.Row(): gr.Markdown("# Tesseract OCR with Selection") with gr.Row(): img_input = gr.Image(type="filepath", label="Input Image") coords_input = gr.Textbox(label="Selection Coordinates (x1, y1, x2, y2)", placeholder="50, 50, 200, 200") with gr.Row(): ocr_button = gr.Button("Run OCR with Selection") with gr.Row(): ocr_output = gr.Textbox(label="OCR Result") def run_with_selection(image_path, coordinates): if coordinates: coordinates = [int(coord) for coord in coordinates.split(",")] return tesseract_ocr_with_selection(image_path, coordinates) ocr_button.click( run_with_selection, inputs=[img_input, coords_input], outputs=[ocr_output] ) if __name__ == '__main__': demo.launch()