Rammohan0504 commited on
Commit
094cd34
·
verified ·
1 Parent(s): 26023f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -17
app.py CHANGED
@@ -3,61 +3,82 @@ import easyocr
3
  import cv2
4
  import numpy as np
5
  import re
 
6
 
7
- # Initialize EasyOCR reader
8
  reader = easyocr.Reader(['en'], gpu=False)
9
 
10
- # Step 1: Preprocessing Function
 
 
 
11
  def preprocess(img):
12
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
13
-
14
- # Apply sharpening for deblurring
15
  sharpen_kernel = np.array([[-1, -1, -1],
16
  [-1, 9, -1],
17
  [-1, -1, -1]])
18
  sharp = cv2.filter2D(gray, -1, sharpen_kernel)
19
-
20
- # Apply adaptive thresholding
21
  thresh = cv2.adaptiveThreshold(
22
- sharp, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 4)
 
 
 
23
 
24
- # Strengthen characters using dilation
25
  kernel = np.ones((2, 2), np.uint8)
26
  dilated = cv2.dilate(thresh, kernel, iterations=1)
27
 
 
 
 
 
28
  return dilated
29
 
30
- # Step 2: Extract weight using regex
31
  def extract_weight(results):
32
  for result in results:
33
  text = result[0] if isinstance(result, (list, tuple)) else ""
34
  if isinstance(text, str):
35
- match = re.search(r'\b\d{2,4}(\.\d{1,2})?\b', text)
36
  if match:
37
  return match.group()
38
  return None
39
 
40
- # Step 3: Main OCR Function
41
  def get_weight(img):
42
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
43
 
44
- # Resize small (distant) images
45
  if img.shape[0] < 500:
46
  img = cv2.resize(img, None, fx=2.0, fy=2.0, interpolation=cv2.INTER_CUBIC)
47
 
48
- processed = preprocess(img)
49
- results = reader.readtext(processed)
 
 
 
 
 
50
  weight = extract_weight(results)
 
 
 
 
 
51
 
52
  return f"Detected Weight: {weight if weight else 'Not found'}"
53
 
54
- # Step 4: Gradio Interface
55
  demo = gr.Interface(
56
  fn=get_weight,
57
  inputs=gr.Image(type="numpy", label="Upload Image"),
58
  outputs=gr.Textbox(label="Detected Weight"),
59
- title="Auto Weight Logger (Blur & Distance Aware)",
60
- description="Extracts weight from distant or blurry display images using OCR + preprocessing."
61
  )
62
 
63
  if __name__ == "__main__":
 
3
  import cv2
4
  import numpy as np
5
  import re
6
+ import os
7
 
8
+ # Initialize EasyOCR Reader
9
  reader = easyocr.Reader(['en'], gpu=False)
10
 
11
+ # Save processed image for inspection
12
+ DEBUG_SAVE_IMAGE = True # Turn this off in production
13
+
14
+ # Preprocessing Function
15
  def preprocess(img):
16
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
17
+
18
+ # Sharpen for deblurring
19
  sharpen_kernel = np.array([[-1, -1, -1],
20
  [-1, 9, -1],
21
  [-1, -1, -1]])
22
  sharp = cv2.filter2D(gray, -1, sharpen_kernel)
23
+
24
+ # Adaptive Thresholding
25
  thresh = cv2.adaptiveThreshold(
26
+ sharp, 255,
27
+ cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
28
+ cv2.THRESH_BINARY_INV, 13, 2
29
+ )
30
 
31
+ # Dilate to enhance characters
32
  kernel = np.ones((2, 2), np.uint8)
33
  dilated = cv2.dilate(thresh, kernel, iterations=1)
34
 
35
+ if DEBUG_SAVE_IMAGE:
36
+ os.makedirs("debug", exist_ok=True)
37
+ cv2.imwrite("debug/processed_debug.png", dilated)
38
+
39
  return dilated
40
 
41
+ # Extract weight with regex
42
  def extract_weight(results):
43
  for result in results:
44
  text = result[0] if isinstance(result, (list, tuple)) else ""
45
  if isinstance(text, str):
46
+ match = re.search(r'\b\d{2,4}(\.\d{1,2})?\s?(kg|lb|KG|LB)?\b', text)
47
  if match:
48
  return match.group()
49
  return None
50
 
51
+ # Main logic
52
  def get_weight(img):
53
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
54
 
55
+ # Upscale low-res images (distant capture)
56
  if img.shape[0] < 500:
57
  img = cv2.resize(img, None, fx=2.0, fy=2.0, interpolation=cv2.INTER_CUBIC)
58
 
59
+ pre = preprocess(img)
60
+
61
+ # Try OCR
62
+ results = reader.readtext(pre)
63
+ print("📝 OCR Results:", results) # Log what OCR sees
64
+
65
+ # Fallback: try raw image if nothing found
66
  weight = extract_weight(results)
67
+ if not weight:
68
+ print("🔁 Trying fallback: raw image")
69
+ fallback_results = reader.readtext(img)
70
+ print("📝 OCR Fallback:", fallback_results)
71
+ weight = extract_weight(fallback_results)
72
 
73
  return f"Detected Weight: {weight if weight else 'Not found'}"
74
 
75
+ # Gradio UI
76
  demo = gr.Interface(
77
  fn=get_weight,
78
  inputs=gr.Image(type="numpy", label="Upload Image"),
79
  outputs=gr.Textbox(label="Detected Weight"),
80
+ title="Auto Weight Logger (Smart OCR)",
81
+ description="Extracts weight even from blurry or distant display images."
82
  )
83
 
84
  if __name__ == "__main__":