Sanjayraju30 commited on
Commit
363a646
·
verified ·
1 Parent(s): 5182e47

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +14 -17
ocr_engine.py CHANGED
@@ -5,30 +5,27 @@ import cv2
5
 
6
  reader = easyocr.Reader(['en'], gpu=False)
7
 
8
- def extract_weight_from_image(pil_image):
9
  try:
10
- image = np.array(pil_image)
11
 
12
- # Grayscale conversion
13
- gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
14
 
15
- # Resize to make digits clearer
16
- resized = cv2.resize(gray, None, fx=3, fy=3, interpolation=cv2.INTER_CUBIC)
17
 
18
- # Bilateral Filter to reduce noise
19
- filtered = cv2.bilateralFilter(resized, 11, 17, 17)
 
20
 
21
- # Adaptive Thresholding
22
- thresh = cv2.adaptiveThreshold(filtered, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
23
- cv2.THRESH_BINARY, 11, 2)
24
-
25
- # OCR
26
  result = reader.readtext(thresh, detail=0)
27
- text = " ".join(result)
28
- print("OCR Output:", text)
29
 
30
- # Extract weight like 25.52 or 123
31
- match = re.search(r'\d{1,4}(\.\d{1,2})?', text)
32
  if match:
33
  return match.group(), 95.0
34
  else:
 
5
 
6
  reader = easyocr.Reader(['en'], gpu=False)
7
 
8
+ def extract_weight_from_image(pil_img):
9
  try:
10
+ img = np.array(pil_img)
11
 
12
+ # Convert to grayscale
13
+ gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
14
 
15
+ # Resize to make characters larger
16
+ resized = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
17
 
18
+ # Apply blur and threshold to improve text clarity
19
+ blurred = cv2.GaussianBlur(resized, (5, 5), 0)
20
+ _, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
21
 
22
+ # OCR detection
 
 
 
 
23
  result = reader.readtext(thresh, detail=0)
24
+ combined_text = " ".join(result)
25
+ print("OCR Text:", combined_text)
26
 
27
+ # Use regex to extract weight like 52.30, 002.50 etc.
28
+ match = re.search(r"\b\d{1,4}\.?\d{0,2}\b", combined_text)
29
  if match:
30
  return match.group(), 95.0
31
  else: