Rammohan0504 commited on
Commit
0ba4671
·
verified ·
1 Parent(s): 8a3eb82

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +19 -13
ocr_engine.py CHANGED
@@ -1,34 +1,40 @@
1
  import cv2
2
  import easyocr
3
  import re
 
 
 
4
 
 
5
  reader = easyocr.Reader(['en'], gpu=False)
 
6
 
7
- def extract_weight_from_image(img_cv):
8
- # Resize for clarity
9
- img = cv2.resize(img_cv, None, fx=2.0, fy=2.0, interpolation=cv2.INTER_CUBIC)
 
10
 
11
- # Convert to grayscale
12
- if len(img.shape) == 3:
13
- img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
14
 
15
- # Adaptive threshold to boost contrast
16
- img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
 
17
  cv2.THRESH_BINARY, 11, 3)
18
 
19
- # Morphological close to unify digits
20
  kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
21
  img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
22
 
23
- # OCR detection
24
  results = reader.readtext(img)
25
 
26
- # Filter only short, confident numeric candidates
27
  filtered = [text for _, text, conf in results if len(text.strip()) <= 5 and conf > 0.4]
28
  raw_text = " ".join(filtered)
29
-
30
  matches = re.findall(r"\d+\.\d+|\d+", raw_text)
 
31
  weight = matches[0] if matches else "0.0"
32
  confidence = int(results[0][2] * 100) if results else 0
33
 
34
- return weight, confidence, raw_text
 
1
  import cv2
2
  import easyocr
3
  import re
4
+ from dblur import Deblurrer
5
+ import numpy as np
6
+ from PIL import Image
7
 
8
+ # Initialize EasyOCR reader and Restormer deblurrer
9
  reader = easyocr.Reader(['en'], gpu=False)
10
+ restormer = Deblurrer(model_name="restormer")
11
 
12
+ def deblur_image(pil_img):
13
+ img_np = np.array(pil_img)
14
+ deblurred_np = restormer.deblur(img_np)
15
+ return Image.fromarray(deblurred_np)
16
 
17
+ def extract_weight_from_image(pil_img):
18
+ # Deblur and convert to OpenCV format
19
+ pil_img = deblur_image(pil_img)
20
+ img_cv = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2GRAY)
21
 
22
+ # Resize and apply thresholding
23
+ img = cv2.resize(img_cv, None, fx=2.0, fy=2.0, interpolation=cv2.INTER_CUBIC)
24
+ img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
25
  cv2.THRESH_BINARY, 11, 3)
26
 
 
27
  kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
28
  img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
29
 
30
+ # OCR
31
  results = reader.readtext(img)
32
 
 
33
  filtered = [text for _, text, conf in results if len(text.strip()) <= 5 and conf > 0.4]
34
  raw_text = " ".join(filtered)
 
35
  matches = re.findall(r"\d+\.\d+|\d+", raw_text)
36
+
37
  weight = matches[0] if matches else "0.0"
38
  confidence = int(results[0][2] * 100) if results else 0
39
 
40
+ return weight, confidence, raw_text, pil_img # return deblurred image too