File size: 2,538 Bytes
bcc6570
 
 
 
 
 
 
732231b
bcc6570
 
 
 
 
 
 
 
732231b
 
bcc6570
0e74751
 
 
 
 
 
 
 
 
 
 
 
 
bcc6570
 
732231b
78fdfd0
bcc6570
78fdfd0
0e74751
 
78fdfd0
bcc6570
 
 
 
 
 
 
 
 
 
 
 
732231b
0e74751
 
 
 
 
 
 
bcc6570
 
732231b
bcc6570
 
 
 
 
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
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')

def parse_coordinates(coord_input: str):
    """
    Kullanıcıdan alınan koordinat stringini doğrula ve liste olarak döndür.
    """
    try:
        # Koordinatları virgül ile ayır ve tam sayıya çevir
        coords = [int(coord.strip()) for coord in coord_input.split(",")]
        if len(coords) != 4:
            raise ValueError("Lütfen tam olarak 4 koordinat girin (örnek: x1, y1, x2, y2).")
        return coords
    except ValueError:
        raise ValueError("Hatalı koordinat formatı. Lütfen şu formatı kullanın: x1, y1, x2, y2.")

# Gradio UI ayarları
title = "Tesseract OCR with Selection"
description = "Gradio demo for Tesseract OCR with region selection (default language: English)."
article = "<p style='text-align: center'><a href='https://tesseract-ocr.github.io/' target='_blank'>Tesseract documentation</a> | <a href='https://github.com/tesseract-ocr/tesseract' target='_blank'>Github Repo</a></p>"

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):
        try:
            # Koordinatları doğrula ve ayrıştır
            coords = parse_coordinates(coordinates)
            return tesseract_ocr_with_selection(image_path, coords)
        except ValueError as e:
            return str(e)  # Kullanıcıya hata mesajı göster

    ocr_button.click(
        run_with_selection,
        inputs=[img_input, coords_input],
        outputs=[ocr_output]
    )

if __name__ == '__main__':
    demo.launch()