Sanjayraju30 commited on
Commit
570a997
·
verified ·
1 Parent(s): afbef94

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +19 -45
ocr_engine.py CHANGED
@@ -5,60 +5,34 @@ import re
5
  import logging
6
  from PIL import Image
7
 
8
- # Setup logging
9
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
10
 
11
- def preprocess_strong(img):
12
- """Sharpen and enhance contrast for blurry weight images."""
13
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
14
-
15
- # Resize up for OCR (scale x2)
16
- h, w = gray.shape
17
- gray = cv2.resize(gray, (w * 2, h * 2), interpolation=cv2.INTER_CUBIC)
18
-
19
- # CLAHE for contrast enhancement
20
- clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
21
- enhanced = clahe.apply(gray)
22
-
23
- # Strong sharpening
24
- kernel = np.array([[0, -1, 0],
25
- [-1, 5, -1],
26
- [0, -1, 0]])
27
- sharpened = cv2.filter2D(enhanced, -1, kernel)
28
-
29
- return sharpened
30
 
31
  def extract_weight_from_image(pil_img):
32
- """Extract weight from an image using multiple Tesseract strategies."""
33
  try:
34
  img = np.array(pil_img)
35
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
36
-
37
- processed = preprocess_strong(img)
38
-
39
- # OCR configs
40
- configs = [
41
- r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.',
42
- r'--oem 3 --psm 11 -c tessedit_char_whitelist=0123456789.'
43
- ]
44
-
45
- for config in configs:
46
- raw_text = pytesseract.image_to_string(processed, config=config)
47
- logging.info(f"[Tesseract Output {config}] Raw text: {raw_text}")
48
- cleaned = raw_text.strip().replace('\n', '').replace(' ', '')
49
- cleaned = re.sub(r"[^\d.]", "", cleaned)
50
- if cleaned.count('.') > 1:
51
- cleaned = cleaned.replace('.', '', cleaned.count('.') - 1)
52
- if cleaned.startswith('.'):
53
- cleaned = '0' + cleaned
54
-
55
- if cleaned and re.fullmatch(r"\d*\.?\d*", cleaned):
56
- value = float(cleaned)
57
- if 0.001 <= value <= 5000:
58
- return str(round(value, 2)), 90.0
59
-
60
  return "Not detected", 0.0
61
 
62
  except Exception as e:
63
- logging.error(f"OCR failed: {e}")
64
  return "Not detected", 0.0
 
5
  import logging
6
  from PIL import Image
7
 
 
8
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
9
 
10
+ def preprocess_image(img):
 
11
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
12
+ resized = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
13
+ blurred = cv2.GaussianBlur(resized, (3, 3), 0)
14
+ thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
15
+ cv2.THRESH_BINARY_INV, 11, 2)
16
+ return thresh
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  def extract_weight_from_image(pil_img):
 
19
  try:
20
  img = np.array(pil_img)
21
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
22
+ processed = preprocess_image(img)
23
+
24
+ config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.kg'
25
+ raw_text = pytesseract.image_to_string(processed, config=config)
26
+ logging.info(f"OCR Raw Output: {raw_text}")
27
+
28
+ cleaned = raw_text.replace(" ", "").replace("\n", "")
29
+ match = re.search(r"(\d+\.?\d*)", cleaned)
30
+ if match:
31
+ value = float(match.group(1))
32
+ if 0 < value <= 5000:
33
+ return str(value), 90.0
 
 
 
 
 
 
 
 
 
 
 
 
34
  return "Not detected", 0.0
35
 
36
  except Exception as e:
37
+ logging.error(f"OCR error: {e}")
38
  return "Not detected", 0.0