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

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +25 -24
ocr_engine.py CHANGED
@@ -5,53 +5,54 @@ 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
 
49
  for detection in result:
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,5 +64,5 @@ def extract_weight_from_image(pil_image):
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
 
5
  import easyocr
6
  import os
7
 
8
+ # βœ… Initialize OCR reader only once
9
  reader = easyocr.Reader(['en'], gpu=False)
10
 
11
  def preprocess_image(image):
12
+ """
13
+ Preprocess the image to improve OCR detection.
14
+ Converts to grayscale and applies adaptive threshold.
15
+ """
16
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
17
+ # Apply adaptive threshold to isolate digits better
18
+ thresh = cv2.adaptiveThreshold(
19
+ gray, 255,
20
+ cv2.ADAPTIVE_THRESH_MEAN_C,
21
+ cv2.THRESH_BINARY_INV,
22
+ 11, 10
23
+ )
24
  return thresh
25
 
26
  def extract_weight_from_image(pil_image):
27
  try:
28
+ # βœ… Step 1: Convert to OpenCV format
29
  image = np.array(pil_image.convert("RGB"))
30
 
31
+ # βœ… Step 2: Preprocess image
 
 
 
32
  processed = preprocess_image(image)
33
 
34
+ # βœ… Step 3: Optional - Save debug image
 
35
  debug_path = "debug_processed_image.png"
36
+ Image.fromarray(processed).save(debug_path)
37
+ print(f"[DEBUG] Saved preprocessed image to {debug_path}")
38
 
39
+ # βœ… Step 4: Run EasyOCR
40
  result = reader.readtext(processed)
41
 
42
+ print("πŸ” OCR Results:")
43
  for r in result:
44
+ print(f" β€’ Text: '{r[1]}' | Confidence: {r[2]*100:.2f}%")
45
 
46
+ # βœ… Step 5: Look for a decimal number like 53.25
47
  weight = None
48
  confidence = 0.0
49
 
50
  for detection in result:
51
+ text = detection[1].replace(",", ".") # Handle comma decimal (if any)
52
  conf = detection[2]
53
 
54
+ # Look for numbers like 53.25 or 100
55
+ match = re.search(r"\b\d{1,3}(\.\d{1,2})?\b", text)
56
  if match:
57
  weight = match.group()
58
  confidence = conf
 
64
  return "No weight detected", 0.0
65
 
66
  except Exception as e:
67
+ print(f"❌ OCR Error: {e}")
68
  return f"Error: {str(e)}", 0.0