testtest123 commited on
Commit
d61e4dd
·
1 Parent(s): 8d465e5

fix: install tesseract-ocr-eng in Dockerfile and add grayscale contrast preprocessing for image OCR

Browse files
Dockerfile CHANGED
@@ -12,7 +12,7 @@ WORKDIR /app
12
 
13
  # System dependencies for python-docx, tiktoken, Tesseract OCR, etc.
14
  RUN apt-get update && apt-get install -y \
15
- build-essential libpq-dev tesseract-ocr libmagic1 libgl1 && \
16
  rm -rf /var/lib/apt/lists/*
17
 
18
  COPY RAG_FULL_APPLICATION_BACKEND/requirements.txt ./
 
12
 
13
  # System dependencies for python-docx, tiktoken, Tesseract OCR, etc.
14
  RUN apt-get update && apt-get install -y \
15
+ build-essential libpq-dev tesseract-ocr tesseract-ocr-eng tesseract-ocr-osd libmagic1 libgl1 && \
16
  rm -rf /var/lib/apt/lists/*
17
 
18
  COPY RAG_FULL_APPLICATION_BACKEND/requirements.txt ./
RAG_FULL_APPLICATION_BACKEND/app/services/file_parser.py CHANGED
@@ -50,14 +50,21 @@ async def _parse_image(file_path: str, job_id: str, ws_manager: Any, user_id: st
50
  filename = Path(file_path).name
51
  extracted_content = []
52
 
53
- # 1. Tesseract OCR (Fast, Reliable Local OCR)
54
  ocr_text = ""
55
  try:
56
  import pytesseract
57
- from PIL import Image
58
  img = Image.open(file_path)
59
- ocr_text = pytesseract.image_to_string(img).strip()
 
 
 
 
 
 
60
  if ocr_text:
 
61
  extracted_content.append(f"Visual Text Content Extracted via OCR:\n{ocr_text}")
62
  except Exception as e:
63
  logger.warning(f"Tesseract OCR failed: {e}")
 
50
  filename = Path(file_path).name
51
  extracted_content = []
52
 
53
+ # 1. Tesseract OCR (Fast, Reliable Local OCR with grayscale preprocessing)
54
  ocr_text = ""
55
  try:
56
  import pytesseract
57
+ from PIL import Image, ImageOps
58
  img = Image.open(file_path)
59
+ # Convert to grayscale and enhance contrast for reliable OCR
60
+ gray_img = ImageOps.grayscale(img)
61
+ if gray_img.width < 1000:
62
+ gray_img = gray_img.resize((gray_img.width * 2, gray_img.height * 2), Image.Resampling.BILINEAR)
63
+ ocr_text = pytesseract.image_to_string(gray_img).strip()
64
+ if not ocr_text:
65
+ ocr_text = pytesseract.image_to_string(img).strip()
66
  if ocr_text:
67
+ logger.info(f"OCR extracted {len(ocr_text)} characters from {filename}")
68
  extracted_content.append(f"Visual Text Content Extracted via OCR:\n{ocr_text}")
69
  except Exception as e:
70
  logger.warning(f"Tesseract OCR failed: {e}")