gizemsarsinlar commited on
Commit
7c963f3
1 Parent(s): 4cbfffc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -46
app.py CHANGED
@@ -1,69 +1,53 @@
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 (varsayılan dil: İngilizce)
18
- return pytesseract.image_to_string(image=image, lang='eng')
19
 
20
- def parse_coordinates(coord_input: str):
21
- """
22
- Kullanıcıdan alınan koordinat stringini doğrula ve liste olarak döndür.
23
- """
24
- try:
25
- # Koordinatları virgül ile ayır ve tam sayıya çevir
26
- coords = [int(coord.strip()) for coord in coord_input.split(",")]
27
- if len(coords) != 4:
28
- raise ValueError("Lütfen tam olarak 4 koordinat girin (örnek: x1, y1, x2, y2).")
29
- return coords
30
- except ValueError:
31
- raise ValueError("Hatalı koordinat formatı. Lütfen şu formatı kullanın: x1, y1, x2, y2.")
32
 
33
- # Gradio UI ayarları
34
- title = "Tesseract OCR with Selection"
35
- description = "Gradio demo for Tesseract OCR with region selection (default language: English)."
36
- 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>"
37
 
38
- # examples = [
39
- # ['examples/eurotext.png', "50, 50, 200, 200"],
40
- # ['examples/tesseract_sample.png', "30, 40, 150, 120"],
41
- # ]
42
 
 
 
 
 
 
43
  with gr.Blocks() as demo:
44
  with gr.Row():
45
- gr.Markdown("# Tesseract OCR with Selection")
 
46
  with gr.Row():
47
- img_input = gr.Image(type="filepath", label="Input Image")
48
- coords_input = gr.Textbox(label="Selection Coordinates (x1, y1, x2, y2)", placeholder="50, 50, 200, 200")
49
  with gr.Row():
50
- ocr_button = gr.Button("Run OCR with Selection")
 
51
  with gr.Row():
52
  ocr_output = gr.Textbox(label="OCR Result")
53
-
54
- def run_with_selection(image_path, coordinates):
55
- try:
56
- # Koordinatları doğrula ve ayrıştır
57
- coords = parse_coordinates(coordinates)
58
- return tesseract_ocr_with_selection(image_path, coords)
59
- except ValueError as e:
60
- return str(e) # Kullanıcıya hata mesajı göster
61
 
62
  ocr_button.click(
63
- run_with_selection,
64
- inputs=[img_input, coords_input],
65
  outputs=[ocr_output]
66
  )
67
 
68
- if __name__ == '__main__':
69
  demo.launch()
 
 
1
  import pytesseract
2
  from PIL import Image
3
  import gradio as gr
4
  import cv2
5
  import numpy as np
6
 
7
+ def select_roi_and_ocr(filepath: str):
8
  # Görseli yükle
9
+ image = cv2.imread(filepath)
10
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Gradio ile uyumlu hale getirmek için
 
 
 
 
 
 
 
11
 
12
+ # Kullanıcıdan alan seçmesini iste
13
+ roi = cv2.selectROI("Alanı Seçin (ESC ile çıkın)", image_rgb, showCrosshair=True)
14
+ cv2.destroyAllWindows()
 
 
 
 
 
 
 
 
 
15
 
16
+ # Koordinatları al
17
+ x, y, w, h = roi
18
+ if w == 0 or h == 0: # Hiçbir şey seçilmemişse
19
+ return "Hiçbir alan seçilmedi!"
20
 
21
+ # Seçilen alanı kırp
22
+ cropped_image = image[y:y+h, x:x+w]
23
+ cropped_image_pil = Image.fromarray(cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)) # OCR için PIL formatına dönüştür
 
24
 
25
+ # OCR işlemi
26
+ text = pytesseract.image_to_string(cropped_image_pil, lang="eng")
27
+ return text
28
+
29
+ # Gradio arayüzü
30
  with gr.Blocks() as demo:
31
  with gr.Row():
32
+ gr.Markdown("## Tesseract OCR with Mouse Selection Box")
33
+
34
  with gr.Row():
35
+ img_input = gr.Image(type="filepath", label="Upload Image")
36
+
37
  with gr.Row():
38
+ ocr_button = gr.Button("Run OCR with Mouse Selection")
39
+
40
  with gr.Row():
41
  ocr_output = gr.Textbox(label="OCR Result")
42
+
43
+ def run_ocr_with_mouse_selection(image_path):
44
+ return select_roi_and_ocr(image_path)
 
 
 
 
 
45
 
46
  ocr_button.click(
47
+ run_ocr_with_mouse_selection,
48
+ inputs=[img_input],
49
  outputs=[ocr_output]
50
  )
51
 
52
+ if __name__ == "__main__":
53
  demo.launch()