Spaces:
Sleeping
Sleeping
gizemsarsinlar
commited on
Commit
•
96aa996
1
Parent(s):
64ebff5
Upload 2 files
Browse files- app.py +64 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List
|
2 |
+
import pytesseract
|
3 |
+
from PIL import Image
|
4 |
+
import gradio as gr
|
5 |
+
import cv2
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
def tesseract_ocr_with_selection(filepath: str, languages: List[str], coordinates: List[int] = None):
|
9 |
+
# Görseli yükle
|
10 |
+
image = Image.open(filepath)
|
11 |
+
|
12 |
+
if coordinates:
|
13 |
+
# Koordinatlara göre kırp
|
14 |
+
x1, y1, x2, y2 = coordinates
|
15 |
+
image = image.crop((x1, y1, x2, y2))
|
16 |
+
|
17 |
+
# OCR işlemi
|
18 |
+
return pytesseract.image_to_string(image=image, lang=', '.join(languages))
|
19 |
+
|
20 |
+
def draw_selection_box(image):
|
21 |
+
# Görseli numpy formatına çevir
|
22 |
+
image = np.array(image)
|
23 |
+
# Kullanıcıdan alan seçmesini iste
|
24 |
+
coordinates = cv2.selectROI("Alanı Seçin (ESC ile çıkın)", image, showCrosshair=True)
|
25 |
+
cv2.destroyAllWindows()
|
26 |
+
return list(coordinates)
|
27 |
+
|
28 |
+
# Gradio UI ayarları
|
29 |
+
title = "Tesseract OCR with Selection"
|
30 |
+
description = "Gradio demo for Tesseract OCR with region selection."
|
31 |
+
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>"
|
32 |
+
|
33 |
+
examples = [
|
34 |
+
['examples/eurotext.png', ['eng'], [50, 50, 200, 200]],
|
35 |
+
['examples/tesseract_sample.png', ['jpn', 'eng'], [30, 40, 150, 120]],
|
36 |
+
]
|
37 |
+
|
38 |
+
language_choices = pytesseract.get_languages()
|
39 |
+
|
40 |
+
with gr.Blocks() as demo:
|
41 |
+
with gr.Row():
|
42 |
+
gr.Markdown("# Tesseract OCR with Selection")
|
43 |
+
with gr.Row():
|
44 |
+
img_input = gr.Image(type="filepath", label="Input Image")
|
45 |
+
lang_input = gr.CheckboxGroup(language_choices, type="value", value=['eng'], label='Language')
|
46 |
+
coords_input = gr.Textbox(label="Selection Coordinates (x1, y1, x2, y2)", placeholder="50, 50, 200, 200")
|
47 |
+
with gr.Row():
|
48 |
+
ocr_button = gr.Button("Run OCR with Selection")
|
49 |
+
with gr.Row():
|
50 |
+
ocr_output = gr.Textbox(label="OCR Result")
|
51 |
+
|
52 |
+
def run_with_selection(image_path, languages, coordinates):
|
53 |
+
if coordinates:
|
54 |
+
coordinates = [int(coord) for coord in coordinates.split(",")]
|
55 |
+
return tesseract_ocr_with_selection(image_path, languages, coordinates)
|
56 |
+
|
57 |
+
ocr_button.click(
|
58 |
+
run_with_selection,
|
59 |
+
inputs=[img_input, lang_input, coords_input],
|
60 |
+
outputs=[ocr_output]
|
61 |
+
)
|
62 |
+
|
63 |
+
if __name__ == '__main__':
|
64 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.41.1
|
2 |
+
pytesseract==0.3.10
|
3 |
+
Pillow==10.0.0
|
4 |
+
opencv-python==4.8.0.74
|