Sanjayraju30 commited on
Commit
42886cd
·
verified ·
1 Parent(s): dfab32b

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +5 -10
ocr_engine.py CHANGED
@@ -1,32 +1,27 @@
1
- from mmocr.apis import MMOCRInferencer
2
  import numpy as np
3
  import cv2
4
  import re
5
  from PIL import Image
6
 
7
- # Initialize MMOCR
8
- ocr = MMOCRInferencer(det='DBNet', recog='SAR', device='cpu') # or 'cuda' if GPU available
9
 
10
  def extract_weight_from_image(pil_img):
11
  try:
12
- # Convert PIL to OpenCV image (BGR)
13
  img = np.array(pil_img.convert("RGB"))[:, :, ::-1]
14
 
15
- # Run MMOCR inference
16
- result = ocr(img)
17
 
18
  raw_texts = []
19
  weight_candidates = []
20
  fallback_weight = None
21
  fallback_conf = 0.0
22
 
23
- for item in result['predictions'][0]:
24
- text = item['text']
25
- conf = item.get('score', 0.8) # Fallback confidence
26
  original = text
27
  cleaned = text.lower().strip()
28
 
29
- # Fix common OCR misreads
30
  cleaned = cleaned.replace(",", ".")
31
  cleaned = cleaned.replace("o", "0").replace("O", "0")
32
  cleaned = cleaned.replace("s", "5").replace("S", "5")
 
1
+ from mmocr.utils.ocr import MMOCR
2
  import numpy as np
3
  import cv2
4
  import re
5
  from PIL import Image
6
 
7
+ # Load MMOCR (det + recog)
8
+ ocr = MMOCR(det='DBPANet', recog='SAR', device='cpu') # CPU mode for Hugging Face
9
 
10
  def extract_weight_from_image(pil_img):
11
  try:
 
12
  img = np.array(pil_img.convert("RGB"))[:, :, ::-1]
13
 
14
+ result = ocr.readtext(img, print_result=False, output=None)[0]['result']
 
15
 
16
  raw_texts = []
17
  weight_candidates = []
18
  fallback_weight = None
19
  fallback_conf = 0.0
20
 
21
+ for text, conf in result:
 
 
22
  original = text
23
  cleaned = text.lower().strip()
24
 
 
25
  cleaned = cleaned.replace(",", ".")
26
  cleaned = cleaned.replace("o", "0").replace("O", "0")
27
  cleaned = cleaned.replace("s", "5").replace("S", "5")