Norakneath commited on
Commit
fe50f9f
·
verified ·
1 Parent(s): 649f113

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -8
app.py CHANGED
@@ -2,14 +2,29 @@ import gradio as gr
2
  from ultralytics import YOLO
3
  from PIL import Image, ImageDraw
4
  import pytesseract
 
5
 
6
- # Set the correct Tesseract path for Hugging Face Spaces
7
  pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
8
 
 
9
  YOLO_MODEL_PATH = "best.pt"
10
  model = YOLO(YOLO_MODEL_PATH, task='detect').to("cpu")
11
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def merge_boxes_into_lines(boxes, y_threshold=10):
 
13
  if len(boxes) == 0:
14
  return []
15
 
@@ -32,6 +47,7 @@ def merge_boxes_into_lines(boxes, y_threshold=10):
32
  return merged_lines
33
 
34
  def detect_and_ocr(image):
 
35
  image = Image.fromarray(image)
36
  original_image = image.copy()
37
 
@@ -50,17 +66,21 @@ def detect_and_ocr(image):
50
 
51
  cropped_line = image.crop((x1, y1, x2, y2))
52
 
53
- ocr_text = pytesseract.image_to_string(cropped_line, lang="eng").strip()
54
- if ocr_text:
55
- extracted_text_lines.append(ocr_text)
 
 
 
 
 
 
56
 
57
- full_text = "\n".join(extracted_text_lines)
58
-
59
  return original_image, full_text
60
 
61
  with gr.Blocks() as iface:
62
- gr.Markdown("# Text Line Detection with OCR")
63
- gr.Markdown("## Upload an image to detect text lines and extract text")
64
 
65
  with gr.Row():
66
  with gr.Column(scale=1):
 
2
  from ultralytics import YOLO
3
  from PIL import Image, ImageDraw
4
  import pytesseract
5
+ import subprocess
6
 
7
+ # Set Tesseract path (Ensure it works on Hugging Face Spaces)
8
  pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
9
 
10
+ # Load YOLO model
11
  YOLO_MODEL_PATH = "best.pt"
12
  model = YOLO(YOLO_MODEL_PATH, task='detect').to("cpu")
13
 
14
+ def check_tesseract():
15
+ """Check if Tesseract is installed and print its version."""
16
+ try:
17
+ tesseract_path = subprocess.check_output(["which", "tesseract"]).decode("utf-8").strip()
18
+ tesseract_version = subprocess.check_output(["tesseract", "--version"]).decode("utf-8").split("\n")[0]
19
+ print(f"Tesseract Path: {tesseract_path}")
20
+ print(f"Tesseract Version: {tesseract_version}")
21
+ return True
22
+ except Exception as e:
23
+ print(f"Tesseract not found: {e}")
24
+ return False
25
+
26
  def merge_boxes_into_lines(boxes, y_threshold=10):
27
+ """Merge bounding boxes if they belong to the same text row."""
28
  if len(boxes) == 0:
29
  return []
30
 
 
47
  return merged_lines
48
 
49
  def detect_and_ocr(image):
50
+ """Detects text lines, draws bounding boxes, and runs OCR if available."""
51
  image = Image.fromarray(image)
52
  original_image = image.copy()
53
 
 
66
 
67
  cropped_line = image.crop((x1, y1, x2, y2))
68
 
69
+ if check_tesseract(): # If Tesseract is installed, run OCR
70
+ try:
71
+ ocr_text = pytesseract.image_to_string(cropped_line, lang="khm").strip()
72
+ if ocr_text:
73
+ extracted_text_lines.append(ocr_text)
74
+ except Exception as e:
75
+ print(f"OCR failed for line {idx}: {e}")
76
+
77
+ full_text = "\n".join(extracted_text_lines) if extracted_text_lines else "⚠️ OCR not available. Showing detected lines only."
78
 
 
 
79
  return original_image, full_text
80
 
81
  with gr.Blocks() as iface:
82
+ gr.Markdown("# Text Line Detection with Khmer OCR")
83
+ gr.Markdown("## Upload an image to detect text lines and extract Khmer text")
84
 
85
  with gr.Row():
86
  with gr.Column(scale=1):