Sanjayraju30 commited on
Commit
b544f2d
·
verified ·
1 Parent(s): 1a5f8fd

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +8 -80
ocr_engine.py CHANGED
@@ -1,83 +1,11 @@
1
- import pytesseract
2
- import numpy as np
3
  import cv2
4
- import re
5
  from PIL import Image
6
- import logging
7
-
8
- # Set up logging
9
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
10
-
11
- def preprocess_image(img):
12
- """Preprocess image for robust OCR."""
13
- try:
14
- # Convert to OpenCV format
15
- img = np.array(img)
16
- img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
17
-
18
- # Convert to grayscale
19
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
20
-
21
- # Estimate brightness for adaptive processing
22
- brightness = np.mean(gray)
23
-
24
- # Apply CLAHE for contrast enhancement
25
- clahe_clip = 4.0 if brightness < 100 else 2.0
26
- clahe = cv2.createCLAHE(clipLimit=clahe_clip, tileGridSize=(8, 8))
27
- enhanced = clahe.apply(gray)
28
-
29
- # Apply adaptive thresholding
30
- block_size = max(11, min(31, int(img.shape[0] / 20) * 2 + 1))
31
- thresh = cv2.adaptiveThreshold(
32
- enhanced, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, block_size, 2
33
- )
34
-
35
- # Noise reduction
36
- kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
37
- thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
38
-
39
- return thresh
40
- except Exception as e:
41
- logging.error(f"Preprocessing failed: {str(e)}")
42
- return img
43
 
44
- def extract_weight_from_image(pil_img):
45
- """Extract weight from any digital scale image."""
46
- try:
47
- # Convert PIL image to OpenCV
48
- img = np.array(pil_img)
49
- img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
50
-
51
- # Preprocess image
52
- thresh = preprocess_image(img)
53
-
54
- # Try multiple Tesseract configurations
55
- configs = [
56
- r'--oem 3 --psm 7 -c tessedit_char_whitelist=0123456789.', # Single line
57
- r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.' # Block of text
58
- ]
59
- for config in configs:
60
- text = pytesseract.image_to_string(thresh, config=config)
61
- logging.info(f"Tesseract raw output (config {config}): {text}")
62
-
63
- # Clean and validate text
64
- text = re.sub(r"[^\d\.]", "", text)
65
- if text.count('.') > 1:
66
- text = text.replace('.', '', text.count('.') - 1)
67
- text = text.strip('.')
68
- if text and re.fullmatch(r"^\d*\.?\d*$", text):
69
- text = text.lstrip('0') or '0'
70
- confidence = 95.0 if len(text.replace('.', '')) >= 3 else 90.0
71
- try:
72
- weight = float(text)
73
- if 0.001 <= weight <= 5000:
74
- logging.info(f"Detected weight: {text} kg, Confidence: {confidence:.2f}%")
75
- return text, confidence
76
- except ValueError:
77
- logging.warning(f"Invalid weight format: {text}")
78
-
79
- logging.info("No valid weight detected.")
80
- return "Not detected", 0.0
81
- except Exception as e:
82
- logging.error(f"Weight extraction failed: {str(e)}")
83
- return "Not detected", 0.0
 
 
 
1
  import cv2
2
+ import pytesseract
3
  from PIL import Image
4
+ import numpy as np
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ def extract_weight(img_path):
7
+ """Extract weight from image path using Tesseract OCR."""
8
+ img = cv2.imread(img_path)
9
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
10
+ text = pytesseract.image_to_string(gray, config='--psm 7 digits')
11
+ return ''.join(filter(lambda x: x in '0123456789.', text))