Sanjayraju30 commited on
Commit
0bb13f0
·
verified ·
1 Parent(s): 9f2ca40

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +8 -26
ocr_engine.py CHANGED
@@ -3,18 +3,12 @@ import numpy as np
3
  import cv2
4
  import re
5
  import logging
6
- from mmocr.utils.ocr import MMOCR
7
 
8
  # Set up logging for debugging
9
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
10
 
11
- # Initialize OCR engines
12
  easyocr_reader = easyocr.Reader(['en'], gpu=False)
13
- try:
14
- mmocr_reader = MMOCR(det='DB_r18', recog='CRNN')
15
- except:
16
- mmocr_reader = None
17
- logging.warning("MMOCR initialization failed, falling back to EasyOCR only")
18
 
19
  def estimate_blur(img):
20
  """Estimate image blur using Laplacian variance"""
@@ -75,23 +69,11 @@ def extract_weight_from_image(pil_img):
75
  best_conf = 0.0
76
 
77
  # EasyOCR detection
78
- easyocr_results = easyocr_reader.readtext(processed, detail=1, paragraph=False)
79
- if not easyocr_results: # Fallback to original image if no results
80
- easyocr_results = easyocr_reader.readtext(img, detail=1, paragraph=False)
81
-
82
- # MMOCR detection (if available)
83
- mmocr_results = []
84
- if mmocr_reader:
85
- try:
86
- mmocr_result = mmocr_reader.readtext(processed)
87
- mmocr_results = [(bbox, text, score) for bbox, text, score in mmocr_result]
88
- except:
89
- logging.warning("MMOCR processing failed, using EasyOCR results only")
90
-
91
- # Combine results
92
- all_results = easyocr_results + mmocr_results
93
-
94
- for (bbox, text, conf) in all_results:
95
  original_text = text
96
  text = text.lower().strip()
97
 
@@ -102,10 +84,10 @@ def extract_weight_from_image(pil_img):
102
  text = text.replace("g", "9").replace("G", "6")
103
  text = text.replace("l", "1").replace("I", "1")
104
  text = text.replace("b", "8").replace("B", "8")
105
- text = text.replace("kgs", "").replace("kg", "").replace("k9", "").replace("k", "")
106
  text = re.sub(r"[^\d\.]", "", text)
107
 
108
- # Stricter regex for weight (0.0 to 9999.999)
109
  if re.fullmatch(r"\d{1,4}(\.\d{0,3})?", text):
110
  if conf > best_conf and conf > conf_threshold:
111
  best_weight = text
 
3
  import cv2
4
  import re
5
  import logging
 
6
 
7
  # Set up logging for debugging
8
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
9
 
10
+ # Initialize EasyOCR
11
  easyocr_reader = easyocr.Reader(['en'], gpu=False)
 
 
 
 
 
12
 
13
  def estimate_blur(img):
14
  """Estimate image blur using Laplacian variance"""
 
69
  best_conf = 0.0
70
 
71
  # EasyOCR detection
72
+ results = easyocr_reader.readtext(processed, detail=1, paragraph=False)
73
+ if not results: # Fallback to original image if no results
74
+ results = easyocr_reader.readtext(img, detail=1, paragraph=False)
75
+
76
+ for (bbox, text, conf) in results:
 
 
 
 
 
 
 
 
 
 
 
 
77
  original_text = text
78
  text = text.lower().strip()
79
 
 
84
  text = text.replace("g", "9").replace("G", "6")
85
  text = text.replace("l", "1").replace("I", "1")
86
  text = text.replace("b", "8").replace("B", "8")
87
+ text = text.replace("kgs", "").replace("kg", "").replace("k", "")
88
  text = re.sub(r"[^\d\.]", "", text)
89
 
90
+ # Regex for weight (0.0 to 9999.999)
91
  if re.fullmatch(r"\d{1,4}(\.\d{0,3})?", text):
92
  if conf > best_conf and conf > conf_threshold:
93
  best_weight = text