Spaces:
Sleeping
Sleeping
gizemsarsinlar
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,62 @@
|
|
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,
|
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
|
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 |
-
|
31 |
-
|
32 |
-
|
33 |
-
examples
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
with gr.Row():
|
42 |
-
gr.
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
with gr.Row():
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
if __name__ == '__main__':
|
64 |
-
demo.launch()
|
|
|
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, 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)
|
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 |
+
|
31 |
+
# examples = [
|
32 |
+
# ['examples/eurotext.png', ['eng'], [50, 50, 200, 200]],
|
33 |
+
# ['examples/tesseract_sample.png', ['jpn', 'eng'], [30, 40, 150, 120]],
|
34 |
+
# ]
|
35 |
+
|
36 |
+
# language_choices = pytesseract.get_languages()
|
37 |
+
|
38 |
+
with gr.Blocks() as demo:
|
39 |
+
with gr.Row():
|
40 |
+
gr.Markdown("# Tesseract OCR with Selection")
|
41 |
+
with gr.Row():
|
42 |
+
img_input = gr.Image(type="filepath", label="Input Image")
|
43 |
+
lang_input = gr.CheckboxGroup(type="value", value=['eng'], label='Language')
|
44 |
+
coords_input = gr.Textbox(label="Selection Coordinates (x1, y1, x2, y2)", placeholder="50, 50, 200, 200")
|
45 |
+
with gr.Row():
|
46 |
+
ocr_button = gr.Button("Run OCR with Selection")
|
47 |
+
with gr.Row():
|
48 |
+
ocr_output = gr.Textbox(label="OCR Result")
|
49 |
+
|
50 |
+
def run_with_selection(image_path, coordinates):
|
51 |
+
if coordinates:
|
52 |
+
coordinates = [int(coord) for coord in coordinates.split(",")]
|
53 |
+
return tesseract_ocr_with_selection(image_path, coordinates)
|
54 |
+
|
55 |
+
ocr_button.click(
|
56 |
+
run_with_selection,
|
57 |
+
inputs=[img_input, lang_input, coords_input],
|
58 |
+
outputs=[ocr_output]
|
59 |
+
)
|
60 |
+
|
61 |
+
if __name__ == '__main__':
|
62 |
+
demo.launch()
|
|
|
|