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, languages: List[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 return pytesseract.image_to_string(image=image, lang=', '.join(languages)) def draw_selection_box(image): # Görseli numpy formatına çevir image = np.array(image) # Kullanıcıdan alan seçmesini iste coordinates = cv2.selectROI("Alanı Seçin (ESC ile çıkın)", image, showCrosshair=True) cv2.destroyAllWindows() return list(coordinates) # Gradio UI ayarları title = "Tesseract OCR with Selection" description = "Gradio demo for Tesseract OCR with region selection." article = "
" examples = [ ['examples/eurotext.png', ['eng'], [50, 50, 200, 200]], ['examples/tesseract_sample.png', ['jpn', 'eng'], [30, 40, 150, 120]], ] language_choices = pytesseract.get_languages() 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") lang_input = gr.CheckboxGroup(language_choices, type="value", value=['eng'], label='Language') 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, languages, coordinates): if coordinates: coordinates = [int(coord) for coord in coordinates.split(",")] return tesseract_ocr_with_selection(image_path, languages, coordinates) ocr_button.click( run_with_selection, inputs=[img_input, lang_input, coords_input], outputs=[ocr_output] ) if __name__ == '__main__': demo.launch()