Sanjayraju30 commited on
Commit
9d1fcb0
·
verified ·
1 Parent(s): adf9604

Update src/ocr_engine.py

Browse files
Files changed (1) hide show
  1. src/ocr_engine.py +1 -4
src/ocr_engine.py CHANGED
@@ -1,5 +1,5 @@
1
  import os
2
- os.system("apt-get update && apt-get install -y libgl1-mesa-glx")
3
 
4
  import cv2
5
  import numpy as np
@@ -11,12 +11,9 @@ def extract_weight_from_image(pil_img):
11
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
12
  blur = cv2.GaussianBlur(gray, (3, 3), 0)
13
  _, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
14
-
15
  config = '--psm 7 -c tessedit_char_whitelist=0123456789.'
16
  data = pytesseract.image_to_data(thresh, config=config, output_type=pytesseract.Output.DICT)
17
-
18
  extracted_text = ''.join(filter(lambda x: x in '0123456789.', ''.join(data['text'])))
19
  confidences = [int(conf) for conf in data['conf'] if conf.isdigit()]
20
  avg_conf = sum(confidences)/len(confidences) if confidences else 0
21
-
22
  return extracted_text.strip(), avg_conf
 
1
  import os
2
+ os.system("apt-get update && apt-get install -y libgl1-mesa-glx") # ← This is the fix!
3
 
4
  import cv2
5
  import numpy as np
 
11
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
12
  blur = cv2.GaussianBlur(gray, (3, 3), 0)
13
  _, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
 
14
  config = '--psm 7 -c tessedit_char_whitelist=0123456789.'
15
  data = pytesseract.image_to_data(thresh, config=config, output_type=pytesseract.Output.DICT)
 
16
  extracted_text = ''.join(filter(lambda x: x in '0123456789.', ''.join(data['text'])))
17
  confidences = [int(conf) for conf in data['conf'] if conf.isdigit()]
18
  avg_conf = sum(confidences)/len(confidences) if confidences else 0
 
19
  return extracted_text.strip(), avg_conf