Sanjayraju30 commited on
Commit
6a65c1f
·
verified ·
1 Parent(s): 37bea37

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +19 -53
ocr_engine.py CHANGED
@@ -1,55 +1,21 @@
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
- ocr = MMOCR(det='DBPANet', recog='SAR', device='cpu')
8
-
9
- def extract_weight_from_image(pil_img):
10
- try:
11
- img = np.array(pil_img.convert("RGB"))[:, :, ::-1]
12
- result = ocr.readtext(img, print_result=False, output=None)[0]['result']
13
-
14
- raw_texts = []
15
- weight_candidates = []
16
- fallback_weight = None
17
- fallback_conf = 0.0
18
-
19
- for text, conf in result:
20
- original = text
21
- cleaned = text.lower().strip()
22
- cleaned = cleaned.replace(",", ".")
23
- cleaned = cleaned.replace("o", "0").replace("O", "0")
24
- cleaned = cleaned.replace("s", "5").replace("S", "5")
25
- cleaned = cleaned.replace("g", "9").replace("G", "6")
26
- cleaned = cleaned.replace("kg", "").replace("kgs", "")
27
- cleaned = re.sub(r"[^0-9\.]", "", cleaned)
28
-
29
- raw_texts.append(f"{original} → {cleaned} (conf: {round(conf, 2)})")
30
-
31
- if cleaned and cleaned.replace(".", "").isdigit() and not fallback_weight:
32
- fallback_weight = cleaned
33
- fallback_conf = conf
34
-
35
- if cleaned.count(".") <= 1 and re.fullmatch(r"\d{2,4}(\.\d{1,3})?", cleaned):
36
- weight_candidates.append((cleaned, conf))
37
-
38
- if weight_candidates:
39
- best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
40
- elif fallback_weight:
41
- best_weight, best_conf = fallback_weight, fallback_conf
42
- else:
43
- return "Not detected", 0.0, "\n".join(raw_texts)
44
-
45
- if "." in best_weight:
46
- int_part, dec_part = best_weight.split(".")
47
- int_part = int_part.lstrip("0") or "0"
48
- best_weight = f"{int_part}.{dec_part}"
49
- else:
50
- best_weight = best_weight.lstrip("0") or "0"
51
-
52
- return best_weight, round(best_conf * 100, 2), "\n".join(raw_texts)
53
-
54
- except Exception as e:
55
- return f"Error: {str(e)}", 0.0, "OCR failed"
 
1
  from mmocr.utils.ocr import MMOCR
 
 
 
 
2
 
3
+ # Load MMOCR model
4
+ mmocr = MMOCR(det='DB_r18', recog='CRNN', det_config=None, recog_config=None)
5
+
6
+ def extract_weight_from_image(image):
7
+ result = mmocr.readtext(image, output=None)
8
+ texts = [item['text'] for item in result]
9
+ debug_text = "\n".join(texts)
10
+
11
+ # Find the first valid float that could represent a weight
12
+ for text in texts:
13
+ if any(char.isdigit() for char in text):
14
+ cleaned = text.replace('kg', '').replace('KG', '').strip()
15
+ try:
16
+ weight = float(cleaned)
17
+ return str(weight), debug_text
18
+ except:
19
+ continue
20
+
21
+ return None, debug_text