gizemsarsinlar commited on
Commit
bcc6570
·
verified ·
1 Parent(s): 83ed590

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -64
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, 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()
 
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()