Sanjayraju30 commited on
Commit
c28abeb
·
verified ·
1 Parent(s): cfcd3d5

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +28 -10
ocr_engine.py CHANGED
@@ -1,32 +1,48 @@
1
- import easyocr
2
  import re
3
  import cv2
4
- import numpy as np
5
  from PIL import Image
 
 
6
 
7
- # Initialize EasyOCR reader
8
  reader = easyocr.Reader(['en'], gpu=False)
9
 
10
  def preprocess_image(image):
 
11
  # Convert to grayscale
12
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
13
 
14
- # Blur + Otsu thresholding
15
- blur = cv2.GaussianBlur(gray, (3, 3), 0)
16
- _, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
17
 
18
  return thresh
19
 
20
  def extract_weight_from_image(pil_image):
21
  try:
22
- # Convert PIL image to OpenCV image
23
  image = np.array(pil_image.convert("RGB"))
 
 
 
 
 
24
  processed = preprocess_image(image)
25
 
26
- # OCR
 
 
 
 
 
 
27
  result = reader.readtext(processed)
28
- print("OCR Results:", result) # for debugging
29
 
 
 
 
 
 
30
  weight = None
31
  confidence = 0.0
32
 
@@ -34,7 +50,8 @@ def extract_weight_from_image(pil_image):
34
  text = detection[1]
35
  conf = detection[2]
36
 
37
- match = re.search(r"\b\d+(\.\d+)?\b", text) # more flexible matching
 
38
  if match:
39
  weight = match.group()
40
  confidence = conf
@@ -46,4 +63,5 @@ def extract_weight_from_image(pil_image):
46
  return "No weight detected", 0.0
47
 
48
  except Exception as e:
 
49
  return f"Error: {str(e)}", 0.0
 
1
+ import numpy as np
2
  import re
3
  import cv2
 
4
  from PIL import Image
5
+ import easyocr
6
+ import os
7
 
8
+ # Initialize OCR Reader
9
  reader = easyocr.Reader(['en'], gpu=False)
10
 
11
  def preprocess_image(image):
12
+ """Preprocess the image to improve OCR accuracy"""
13
  # Convert to grayscale
14
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
15
 
16
+ # Apply threshold to isolate digits
17
+ _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
 
18
 
19
  return thresh
20
 
21
  def extract_weight_from_image(pil_image):
22
  try:
23
+ # Convert PIL to OpenCV format
24
  image = np.array(pil_image.convert("RGB"))
25
+
26
+ # Print image shape for debugging
27
+ print("Image shape:", image.shape)
28
+
29
+ # Preprocess for better OCR accuracy
30
  processed = preprocess_image(image)
31
 
32
+ # Save debug image
33
+ debug_img = Image.fromarray(processed)
34
+ debug_path = "debug_processed_image.png"
35
+ debug_img.save(debug_path)
36
+ print(f"✅ Processed image saved to: {debug_path}")
37
+
38
+ # Run OCR on processed image
39
  result = reader.readtext(processed)
 
40
 
41
+ print("✅ OCR Results:")
42
+ for r in result:
43
+ print(f"Text: '{r[1]}' | Confidence: {r[2] * 100:.2f}%")
44
+
45
+ # Try to find numeric weight
46
  weight = None
47
  confidence = 0.0
48
 
 
50
  text = detection[1]
51
  conf = detection[2]
52
 
53
+ # Match numbers like 53.25 or 45
54
+ match = re.search(r"\b\d+(\.\d+)?\b", text)
55
  if match:
56
  weight = match.group()
57
  confidence = conf
 
63
  return "No weight detected", 0.0
64
 
65
  except Exception as e:
66
+ print("❌ Exception during OCR:", str(e))
67
  return f"Error: {str(e)}", 0.0