Sanjayraju30 commited on
Commit
b902588
Β·
verified Β·
1 Parent(s): d07963e

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +20 -23
ocr_engine.py CHANGED
@@ -1,55 +1,52 @@
1
- import numpy as np
2
  import re
3
  import cv2
 
4
  from PIL import Image
5
- import easyocr
6
 
7
- # βœ… Initialize EasyOCR Reader once
8
- reader = easyocr.Reader(['en'], gpu=False)
9
 
10
  def preprocess_image(image):
11
  """
12
- Convert to grayscale and apply adaptive thresholding
13
- to enhance contrast for digital scale OCR.
14
  """
15
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
16
- thresh = cv2.adaptiveThreshold(
17
  gray, 255,
18
- cv2.ADAPTIVE_THRESH_MEAN_C,
19
  cv2.THRESH_BINARY_INV,
20
- 11, 10
21
  )
22
- return thresh
23
 
24
  def extract_weight_from_image(pil_image):
25
  try:
26
- # βœ… Convert PIL image to OpenCV format
27
  image = np.array(pil_image.convert("RGB"))
28
 
29
  # βœ… Preprocess image
30
  processed = preprocess_image(image)
31
 
32
- # βœ… Optional: Save debug image for troubleshooting
33
  debug_path = "debug_processed_image.png"
34
  Image.fromarray(processed).save(debug_path)
35
- print(f"[DEBUG] Preprocessed image saved to: {debug_path}")
36
 
37
- # βœ… Perform OCR using EasyOCR
38
- result = reader.readtext(processed)
39
 
40
- print("πŸ” OCR Results:")
41
- for detection in result:
42
- print(f" β€’ Text: '{detection[1]}' | Confidence: {detection[2]*100:.2f}%")
 
 
43
 
44
- # βœ… Extract first matching numeric value
45
- for detection in result:
46
- text = detection[1].replace(",", ".") # normalize decimal
47
- conf = detection[2]
48
  match = re.search(r"\b\d{1,4}(\.\d{1,2})?\b", text)
49
  if match:
50
  return match.group(), round(conf * 100, 2)
51
 
52
- # ❌ No weight found
53
  return "No weight detected", 0.0
54
 
55
  except Exception as e:
 
 
1
  import re
2
  import cv2
3
+ import numpy as np
4
  from PIL import Image
5
+ from paddleocr import PaddleOCR
6
 
7
+ # βœ… Initialize PaddleOCR once
8
+ ocr = PaddleOCR(use_angle_cls=False, lang='en', show_log=False)
9
 
10
  def preprocess_image(image):
11
  """
12
+ Convert to grayscale and enhance contrast to help OCR.
 
13
  """
14
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
15
+ processed = cv2.adaptiveThreshold(
16
  gray, 255,
17
+ cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
18
  cv2.THRESH_BINARY_INV,
19
+ 11, 2
20
  )
21
+ return processed
22
 
23
  def extract_weight_from_image(pil_image):
24
  try:
25
+ # βœ… Convert to OpenCV format
26
  image = np.array(pil_image.convert("RGB"))
27
 
28
  # βœ… Preprocess image
29
  processed = preprocess_image(image)
30
 
31
+ # Optional: Save debug image
32
  debug_path = "debug_processed_image.png"
33
  Image.fromarray(processed).save(debug_path)
34
+ print(f"[DEBUG] Saved preprocessed image to {debug_path}")
35
 
36
+ # βœ… Run OCR on original (RGB) image (not preprocessed)
37
+ result = ocr.ocr(image, cls=False)
38
 
39
+ print("πŸ” PaddleOCR Results:")
40
+ for line in result[0]:
41
+ text = line[1][0]
42
+ conf = line[1][1]
43
+ print(f" β€’ Text: '{text}' | Confidence: {conf*100:.2f}%")
44
 
45
+ # Try to extract number like 53.25 or 100
 
 
 
46
  match = re.search(r"\b\d{1,4}(\.\d{1,2})?\b", text)
47
  if match:
48
  return match.group(), round(conf * 100, 2)
49
 
 
50
  return "No weight detected", 0.0
51
 
52
  except Exception as e: