tarujain8 commited on
Commit
03fb27e
·
1 Parent(s): 2cb0d12

feat: implement OCR service using Tesseract for image text extraction

Browse files
Files changed (1) hide show
  1. backend/services/ocr_service.py +36 -25
backend/services/ocr_service.py CHANGED
@@ -1,25 +1,36 @@
1
- import pytesseract
2
- from PIL import Image
3
- from backend.core.logger import get_logger
4
-
5
- logger = get_logger(__name__)
6
-
7
- # 👇 ADD THIS LINE (VERY IMPORTANT FOR WINDOWS)
8
- # pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
9
-
10
-
11
- def extract_text_from_images(image_paths):
12
- logger.info(f"Starting OCR extraction for {len(image_paths)} images")
13
- texts = []
14
-
15
- for path in image_paths:
16
- try:
17
- logger.info(f"Processing image: {path}")
18
- img = Image.open(path)
19
- text = pytesseract.image_to_string(img)
20
- texts.append(text)
21
- except Exception as e:
22
- logger.error(f"Error processing image {path}: {str(e)}")
23
- texts.append("")
24
-
25
- return "\n".join(texts)
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytesseract
2
+ from PIL import Image
3
+
4
+ from backend.core.logger import get_logger
5
+
6
+ logger = get_logger(__name__)
7
+
8
+
9
+ def extract_text_from_images(image_paths):
10
+ texts = []
11
+
12
+ logger.info(f"OCR STARTED FOR {len(image_paths)} IMAGES")
13
+
14
+ for path in image_paths:
15
+ try:
16
+ logger.info(f"OPENING IMAGE: {path}")
17
+
18
+ img = Image.open(path)
19
+
20
+ logger.info(f"RUNNING OCR ON: {path}")
21
+
22
+ text = pytesseract.image_to_string(img)
23
+
24
+ logger.info(f"OCR RESULT:\n{text[:1000]}")
25
+
26
+ texts.append(text)
27
+
28
+ except Exception as e:
29
+ logger.exception(f"OCR FAILED: {e}")
30
+ texts.append("")
31
+
32
+ final_text = "\n".join(texts)
33
+
34
+ logger.info(f"FINAL OCR TEXT:\n{final_text[:3000]}")
35
+
36
+ return final_text