gizemsarsinlar commited on
Commit
a185729
·
verified ·
1 Parent(s): c6295ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -49
app.py CHANGED
@@ -1,56 +1,48 @@
 
 
1
  import pytesseract
2
  from PIL import Image
3
- import gradio as gr
4
- import cv2
5
- import numpy as np
6
- import os
7
-
8
- def select_roi_and_ocr(filepath: str):
9
- if not os.path.exists(filepath):
10
- return "Dosya mevcut değil. Lütfen tekrar yükleyin."
11
- # Görseli yükle
12
- image = cv2.imread(filepath)
13
- image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Gradio ile uyumlu hale getirmek için
14
-
15
- # Kullanıcıdan alan seçmesini iste
16
- roi = cv2.selectROI("Alanı Seçin (ESC ile çıkın)", image_rgb, showCrosshair=True)
17
- cv2.destroyAllWindows()
18
-
19
- # Koordinatları al
20
- x, y, w, h = roi
21
- if w == 0 or h == 0: # Hiçbir şey seçilmemişse
22
- return "Hiçbir alan seçilmedi!"
23
-
24
- # Seçilen alanı kırp
25
- cropped_image = image[y:y+h, x:x+w]
26
- cropped_image_pil = Image.fromarray(cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)) # OCR için PIL formatına dönüştür
27
-
28
- # OCR işlemi
29
- text = pytesseract.image_to_string(cropped_image_pil, lang="eng")
30
- return text
31
-
32
- # Gradio arayüzü
33
- with gr.Blocks() as demo:
34
- with gr.Row():
35
- gr.Markdown("## Tesseract OCR with Mouse Selection Box")
36
 
37
- with gr.Row():
38
- img_input = gr.Image(type="filepath", label="Upload Image")
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  with gr.Row():
41
- ocr_button = gr.Button("Run OCR with Mouse Selection")
42
-
43
- with gr.Row():
44
- ocr_output = gr.Textbox(label="OCR Result")
45
-
46
- def run_ocr_with_mouse_selection(image_path):
47
- return select_roi_and_ocr(image_path)
48
-
49
- ocr_button.click(
50
- run_ocr_with_mouse_selection,
51
- inputs=[img_input],
52
- outputs=[ocr_output]
 
 
 
 
 
53
  )
54
 
55
- if __name__ == "__main__":
56
- demo.launch(debug=True)
 
 
 
1
+ from typing import List
2
+
3
  import pytesseract
4
  from PIL import Image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ import gradio as gr
 
7
 
8
+ def tesseract_ocr(filepath: str, languages: List[str]=None):
9
+ image = Image.open(filepath)
10
+ return pytesseract.image_to_string(image=image, lang=', '.join(languages) if languages else None)
11
+
12
+ title = "Tesseract OCR"
13
+ description = "Gradio demo for Tesseract. Tesseract is an open source text recognition (OCR) Engine."
14
+ 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>"
15
+ examples = [
16
+ ["examples/weird_unicode_math_symbols.png", []],
17
+ ["examples/eurotext.png", ["eng"]],
18
+ ["examples/tesseract_sample.png", ["jpn", "eng"]],
19
+ ["examples/chi.jpg", ["HanS", "HanT"]],
20
+ ]
21
+
22
+ with gr.Blocks(title=title) as demo:
23
+ gr.Markdown(f'<h1 style="text-align: center; margin-bottom: 1rem;">{title}</h1>')
24
+ gr.Markdown(description)
25
  with gr.Row():
26
+ with gr.Column():
27
+ image = gr.Image(type="filepath", label="Input")
28
+ language_choices = pytesseract.get_languages()
29
+ with gr.Accordion("Languages", open=False):
30
+ languages = gr.CheckboxGroup(language_choices, type="value", value=["eng"], label='language')
31
+ with gr.Row():
32
+ btn_clear = gr.ClearButton([image, languages])
33
+ btn_submit = gr.Button(value="Submit", variant="primary")
34
+ with gr.Column():
35
+ text = gr.Textbox(label="Output")
36
+
37
+ btn_submit.click(tesseract_ocr, inputs=[image, languages], outputs=text, api_name="tesseract-ocr")
38
+ btn_clear.add(text)
39
+
40
+ gr.Examples(
41
+ examples=examples,
42
+ inputs=[image, languages],
43
  )
44
 
45
+ gr.Markdown(article)
46
+
47
+ if __name__ == '__main__':
48
+ demo.launch()