Rammohan0504 commited on
Commit
26023f5
·
verified ·
1 Parent(s): 72d3be0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -23
app.py CHANGED
@@ -4,61 +4,60 @@ import cv2
4
  import numpy as np
5
  import re
6
 
7
- # Initialize OCR reader
8
  reader = easyocr.Reader(['en'], gpu=False)
9
 
10
- # Preprocessing function to enhance OCR accuracy
11
  def preprocess(img):
12
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
13
 
14
- # Sharpening kernel (helps deblur weak edges)
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
- # Adaptive thresholding for binarization
21
- thresh = cv2.adaptiveThreshold(sharp, 255,
22
- cv2.ADAPTIVE_THRESH_MEAN_C,
23
- cv2.THRESH_BINARY_INV, 11, 4)
24
 
25
- # Dilation to strengthen character boundaries
26
  kernel = np.ones((2, 2), np.uint8)
27
  dilated = cv2.dilate(thresh, kernel, iterations=1)
28
 
29
  return dilated
30
 
31
- # Extract weight using regex
32
  def extract_weight(results):
33
- for text, _, _ in results:
34
- match = re.search(r'\b\d{2,4}(\.\d{1,2})?\b', text)
35
- if match:
36
- return match.group()
 
 
37
  return None
38
 
39
- # Complete OCR pipeline
40
  def get_weight(img):
41
- # Convert RGB to BGR
42
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
43
 
44
- # Resize small or distant images (heuristic)
45
  if img.shape[0] < 500:
46
- img = cv2.resize(img, None, fx=2.0, fy=2.0,
47
- interpolation=cv2.INTER_CUBIC)
48
 
49
- preprocessed = preprocess(img)
50
- results = reader.readtext(preprocessed)
51
  weight = extract_weight(results)
52
 
53
  return f"Detected Weight: {weight if weight else 'Not found'}"
54
 
55
- # Gradio UI
56
  demo = gr.Interface(
57
  fn=get_weight,
58
  inputs=gr.Image(type="numpy", label="Upload Image"),
59
  outputs=gr.Textbox(label="Detected Weight"),
60
  title="Auto Weight Logger (Blur & Distance Aware)",
61
- description="Extracts weight from digital display images using OCR & preprocessing."
62
  )
63
 
64
  if __name__ == "__main__":
 
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__":