Sanjayraju30 commited on
Commit
513f893
·
verified ·
1 Parent(s): 0cf36c5

Update ocr_engine.py

Browse files
Files changed (1) hide show
  1. ocr_engine.py +14 -15
ocr_engine.py CHANGED
@@ -1,23 +1,22 @@
1
- import cv2
2
- import pytesseract
3
- import numpy as np
4
  from PIL import Image
5
 
6
- def extract_weight(pil_image: Image.Image) -> str:
7
- # Convert to OpenCV format
8
- img = np.array(pil_image.convert("RGB"))
9
- gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
10
 
11
- # Enhance image for OCR
12
- gray = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
13
- blurred = cv2.GaussianBlur(gray, (5, 5), 0)
14
- _, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
15
 
16
- # OCR with config
17
- config = "--psm 7 -c tessedit_char_whitelist=0123456789."
18
- text = pytesseract.image_to_string(thresh, config=config)
19
 
20
- # Extract digits and decimal
 
 
 
21
  weight = ''.join(filter(lambda x: x in '0123456789.', text))
22
 
23
  return weight.strip() if weight else "No valid weight detected"
 
1
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
 
 
2
  from PIL import Image
3
 
4
+ # Load model + processor once
5
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
6
+ model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
 
7
 
8
+ def extract_weight(image: Image.Image) -> str:
9
+ # Convert image to RGB just in case
10
+ image = image.convert("RGB")
 
11
 
12
+ # Preprocess + generate prediction
13
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
14
+ generated_ids = model.generate(pixel_values)
15
 
16
+ # Decode output
17
+ text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
18
+
19
+ # Keep only numbers + decimal
20
  weight = ''.join(filter(lambda x: x in '0123456789.', text))
21
 
22
  return weight.strip() if weight else "No valid weight detected"