Sanjayraju30 commited on
Commit
c43e287
·
verified ·
1 Parent(s): 199b65c

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +32 -24
ocr_engine.py CHANGED
@@ -1,28 +1,36 @@
1
- import cv2
2
- import pytesseract
3
- import numpy as np
4
- from PIL import Image
5
 
6
- def preprocess_image(pil_image):
7
- image = np.array(pil_image.convert("RGB"))
8
- gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
9
- blurred = cv2.GaussianBlur(gray, (3, 3), 0)
10
- _, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
11
- return thresh
12
 
13
- def extract_weight(pil_image):
14
- try:
15
- processed_img = preprocess_image(pil_image)
16
- config = "--psm 7 -c tessedit_char_whitelist=0123456789."
17
- text = pytesseract.image_to_string(processed_img, config=config)
18
- print("OCR Raw:", text)
19
 
20
- numbers = ''.join(filter(lambda x: x in "0123456789.", text))
21
- if not numbers:
22
- return "No valid weight detected"
 
23
 
24
- weight_val = float(numbers)
25
- unit = "kg" if weight_val >= 20 else "grams"
26
- return f"{weight_val} {unit}"
27
- except Exception as e:
28
- return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
2
+ from PIL import Image, ImageEnhance
3
+ import re
 
4
 
5
+ # Load TrOCR model
6
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
7
+ model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
 
 
 
8
 
9
+ def extract_weight(image: Image.Image) -> str:
10
+ # Step 1: Preprocess image
11
+ image = image.convert("L") # grayscale
12
+ image = ImageEnhance.Contrast(image).enhance(2.0)
13
+ image = ImageEnhance.Sharpness(image).enhance(2.5)
14
+ image = image.convert("RGB")
15
 
16
+ # Step 2: Run Hugging Face OCR
17
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
18
+ generated_ids = model.generate(pixel_values, max_length=32)
19
+ full_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
20
 
21
+ print("OCR Output:", full_text)
22
+
23
+ # Step 3: Extract numeric weight
24
+ cleaned = full_text.lower().replace(" ", "")
25
+ match = re.search(r"(\d+(\.\d+)?)", cleaned)
26
+ weight = match.group(1) if match else None
27
+
28
+ # Step 4: Decide unit
29
+ if any(u in cleaned for u in ["kg", "kgs", "kilo"]):
30
+ unit = "kg"
31
+ elif any(u in cleaned for u in ["g", "gram", "grams"]):
32
+ unit = "grams"
33
+ else:
34
+ unit = "kg" if weight and float(weight) >= 20 else "grams"
35
+
36
+ return f"{weight} {unit}" if weight else "No valid weight detected"