Sanjayraju30 commited on
Commit
7dd52ba
·
verified ·
1 Parent(s): 2061dc4

AutoWeightLogger1/ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +0 -41
ocr_engine.py DELETED
@@ -1,41 +0,0 @@
1
- import cv2
2
- import numpy as np
3
- import re
4
- from PIL import Image
5
-
6
- def extract_weight_from_image(pil_img):
7
- try:
8
- img = np.array(pil_img)
9
-
10
- # Convert to grayscale
11
- gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
12
-
13
- # Threshold image
14
- _, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
15
-
16
- # Invert if needed
17
- if np.mean(thresh > 127) < 0.5:
18
- thresh = cv2.bitwise_not(thresh)
19
-
20
- # Resize to make digits bigger
21
- scale_factor = 4
22
- resized = cv2.resize(thresh, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)
23
-
24
- # OCR-style region crop: focus on left part of display
25
- height, width = resized.shape
26
- digit_region = resized[0:height, 0:int(width * 0.7)] # ignore 'kg'
27
-
28
- # Use pytesseract as fallback OCR for just digits
29
- import pytesseract
30
- config = "--psm 7 -c tessedit_char_whitelist=0123456789."
31
- result = pytesseract.image_to_string(digit_region, config=config)
32
- print("Raw OCR:", result)
33
-
34
- match = re.search(r"(\d{1,4}(?:\.\d{1,2})?)", result)
35
- if match:
36
- return f"{match.group()} kg", 100.0
37
- else:
38
- return "No weight detected kg", 0.0
39
-
40
- except Exception as e:
41
- return f"Error: {str(e)}", 0.0