Sanjayraju30 commited on
Commit
2c62dab
·
verified ·
1 Parent(s): 7c5b4e0

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +8 -4
ocr_engine.py CHANGED
@@ -9,6 +9,7 @@ def extract_weight_from_image(pil_img):
9
  img = pil_img.convert("RGB")
10
  img_np = np.array(img)
11
  img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
 
12
 
13
  # Convert to grayscale
14
  gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
@@ -21,12 +22,15 @@ def extract_weight_from_image(pil_img):
21
  # Resize to enhance small text
22
  resized = cv2.resize(processed, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
23
 
24
- # OCR config tuned for digit blocks
 
 
 
 
25
  config = r'--oem 3 --psm 7 -c tessedit_char_whitelist=0123456789.'
26
 
27
  # Run OCR
28
- text = pytesseract.image_to_string(resized, config=config)
29
-
30
  print("🔍 RAW OCR OUTPUT:", repr(text))
31
 
32
  # Clean the text
@@ -38,4 +42,4 @@ def extract_weight_from_image(pil_img):
38
 
39
  except Exception as e:
40
  print("❌ OCR Error:", str(e))
41
- return "", 0
 
9
  img = pil_img.convert("RGB")
10
  img_np = np.array(img)
11
  img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
12
+ print("🧼 Image converted to OpenCV format.")
13
 
14
  # Convert to grayscale
15
  gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
 
22
  # Resize to enhance small text
23
  resized = cv2.resize(processed, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
24
 
25
+ # Optional: Apply dilation to enhance contours
26
+ kernel = np.ones((2, 2), np.uint8)
27
+ dilated = cv2.dilate(resized, kernel, iterations=1)
28
+
29
+ # OCR config tuned for digits
30
  config = r'--oem 3 --psm 7 -c tessedit_char_whitelist=0123456789.'
31
 
32
  # Run OCR
33
+ text = pytesseract.image_to_string(dilated, config=config)
 
34
  print("🔍 RAW OCR OUTPUT:", repr(text))
35
 
36
  # Clean the text
 
42
 
43
  except Exception as e:
44
  print("❌ OCR Error:", str(e))
45
+ return "", 0