Tesseract_OCR / app.py
gizemsarsinlar's picture
Update app.py
4cbfffc verified
raw
history blame
2.55 kB
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()