Sanjayraju30 commited on
Commit
b18d0cd
·
verified ·
1 Parent(s): 301eb4d

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +40 -40
ocr_engine.py CHANGED
@@ -5,62 +5,62 @@ import re
5
  import logging
6
  from PIL import Image
7
 
8
- # Set up logging
9
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
10
 
11
- def preprocess_for_ocr(img):
12
- """Apply grayscale, blur, and threshold to prepare image for OCR."""
 
13
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
14
- blurred = cv2.GaussianBlur(gray, (5, 5), 0)
15
 
16
- # Adaptive threshold
17
- thresh = cv2.adaptiveThreshold(
18
- blurred, 255,
19
- cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
20
- cv2.THRESH_BINARY,
21
- 11, 2
22
- )
 
 
 
 
 
 
 
 
23
 
24
- # Invert to make text white on black
25
- inverted = cv2.bitwise_not(thresh)
26
- return inverted
27
 
28
  def extract_weight_from_image(pil_img):
29
- """Extract weight reading from an image using pytesseract."""
30
  try:
31
- # Convert PIL to OpenCV
32
  img = np.array(pil_img)
33
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
34
 
35
- # Preprocess
36
- processed_img = preprocess_for_ocr(img)
37
-
38
- # Tesseract config
39
- config = r'--oem 3 --psm 7 -c tessedit_char_whitelist=0123456789.'
40
-
41
- # Run OCR
42
- text = pytesseract.image_to_string(processed_img, config=config)
43
 
44
- # Clean text
45
- text = text.strip().replace('\n', '').replace(' ', '')
46
- text = re.sub(r"[^\d.]", "", text)
 
 
47
 
48
- # Handle multiple dots
49
- if text.count('.') > 1:
50
- text = text.replace('.', '', text.count('.') - 1)
 
 
 
 
 
 
51
 
52
- if text.startswith('.'):
53
- text = '0' + text
 
 
54
 
55
- # Validate
56
- if text and re.fullmatch(r"\d*\.?\d*", text):
57
- value = float(text)
58
- if 0.001 <= value <= 5000:
59
- return text, 90.0 # Return with fixed confidence
60
- else:
61
- logging.warning(f"Detected weight out of range: {value}")
62
  return "Not detected", 0.0
63
 
64
  except Exception as e:
65
- logging.error(f"OCR error: {str(e)}")
66
  return "Not detected", 0.0
 
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
+
12
+ def preprocess_strong(img):
13
+ """Sharpen and enhance contrast for blurry weight images."""
14
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
15
 
16
+ # Resize up for OCR (scale x2)
17
+ h, w = gray.shape
18
+ gray = cv2.resize(gray, (w * 2, h * 2), interpolation=cv2.INTER_CUBIC)
19
+
20
+ # CLAHE for contrast enhancement
21
+ clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
22
+ enhanced = clahe.apply(gray)
23
+
24
+ # Strong sharpening
25
+ kernel = np.array([[0, -1, 0],
26
+ [-1, 5, -1],
27
+ [0, -1, 0]])
28
+ sharpened = cv2.filter2D(enhanced, -1, kernel)
29
+
30
+ return sharpened
31
 
 
 
 
32
 
33
  def extract_weight_from_image(pil_img):
34
+ """Extract weight from an image using multiple Tesseract strategies."""
35
  try:
 
36
  img = np.array(pil_img)
37
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
38
 
39
+ processed = preprocess_strong(img)
 
 
 
 
 
 
 
40
 
41
+ # OCR configs
42
+ configs = [
43
+ r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.',
44
+ r'--oem 3 --psm 11 -c tessedit_char_whitelist=0123456789.'
45
+ ]
46
 
47
+ for config in configs:
48
+ raw_text = pytesseract.image_to_string(processed, config=config)
49
+ logging.info(f"[Tesseract Output {config}] Raw text: {raw_text}")
50
+ cleaned = raw_text.strip().replace('\n', '').replace(' ', '')
51
+ cleaned = re.sub(r"[^\d.]", "", cleaned)
52
+ if cleaned.count('.') > 1:
53
+ cleaned = cleaned.replace('.', '', cleaned.count('.') - 1)
54
+ if cleaned.startswith('.'):
55
+ cleaned = '0' + cleaned
56
 
57
+ if cleaned and re.fullmatch(r"\d*\.?\d*", cleaned):
58
+ value = float(cleaned)
59
+ if 0.001 <= value <= 5000:
60
+ return str(round(value, 2)), 90.0
61
 
 
 
 
 
 
 
 
62
  return "Not detected", 0.0
63
 
64
  except Exception as e:
65
+ logging.error(f"OCR failed: {e}")
66
  return "Not detected", 0.0