Sanjayraju30 commited on
Commit
07d4fed
·
verified ·
1 Parent(s): 9aa3367

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +17 -27
ocr_engine.py CHANGED
@@ -1,33 +1,23 @@
1
- try:
2
- from paddleocr import PaddleOCR
3
- import re
4
- ocr = PaddleOCR(use_angle_cls=True, lang='en')
5
- except Exception as e:
6
- import streamlit as st
7
- st.error(f"❌ OCR Engine Load Failed: {e}")
8
- ocr = None
9
 
10
- def extract_weight_from_image(image):
11
- if ocr is None:
12
- return ("OCR not initialized", 0.0)
13
 
14
- result = ocr.ocr(image, cls=True)
15
- debug_texts = []
16
 
17
- if not result or not result[0]:
18
- return ("No weight detected", 0.0)
19
 
20
- for line in result[0]:
21
- text, confidence = line[1][0], line[1][1]
22
- debug_texts.append(f"{text} (Conf: {confidence:.2f})")
23
- match = re.search(r'(\d+\.?\d*)\s*(kg|g)?', text.lower())
24
  if match:
25
- weight = match.group(1)
26
- unit = match.group(2) if match.group(2) else "g"
27
- return (f"{weight} {unit}", confidence)
28
 
29
- print("OCR Debug Output:")
30
- for t in debug_texts:
31
- print(t)
32
-
33
- return ("No weight detected", 0.0)
 
1
+ import easyocr
2
+ import re
 
 
 
 
 
 
3
 
4
+ # Load OCR reader once
5
+ reader = easyocr.Reader(['en'])
 
6
 
7
+ def extract_weight_from_image(pil_image):
8
+ results = reader.readtext(pil_image)
9
 
10
+ weight = None
11
+ confidence = 0.0
12
 
13
+ for (bbox, text, conf) in results:
14
+ match = re.search(r'(\d+(\.\d+)?)\s?g', text.lower())
 
 
15
  if match:
16
+ weight = match.group(1) + " g"
17
+ confidence = conf
18
+ break
19
 
20
+ if weight:
21
+ return weight, confidence
22
+ else:
23
+ return "No weight detected", 0.0