Rammohan0504 commited on
Commit
8a3eb82
·
verified ·
1 Parent(s): 25728eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -11
app.py CHANGED
@@ -4,33 +4,25 @@ from PIL import ImageFilter, ImageEnhance
4
  import pytz
5
  import numpy as np
6
  import cv2
7
- from ocr_engine import extract_weight_from_image # Modify if needed
8
 
9
- # Preprocess image to improve OCR accuracy
10
  def preprocess_image(img):
11
- # PIL enhancements
12
  img = img.filter(ImageFilter.SHARPEN)
13
  enhancer = ImageEnhance.Contrast(img)
14
  img = enhancer.enhance(2.0)
 
 
15
 
16
- # Convert to OpenCV format
17
- cv_img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
18
- return cv_img
19
-
20
- # Process image and extract weight using OCR
21
  def process_image(img):
22
  if img is None:
23
  return "No image uploaded", None, None, "No OCR output"
24
 
25
  preprocessed_cv = preprocess_image(img)
26
-
27
- # Extract data from your OCR engine (should handle OpenCV input)
28
  weight, confidence, raw_text = extract_weight_from_image(preprocessed_cv)
29
 
30
  ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
31
  return f"{weight} kg (Confidence: {confidence}%)", ist_time, img, raw_text
32
 
33
- # Gradio UI setup
34
  with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
35
  gr.Markdown("## ⚖️ Auto Weight Logger")
36
  gr.Markdown("📷 **Upload or capture an image of a digital weight scale.** The app will auto-detect the weight in kilograms using OCR.")
 
4
  import pytz
5
  import numpy as np
6
  import cv2
7
+ from ocr_engine import extract_weight_from_image
8
 
 
9
  def preprocess_image(img):
 
10
  img = img.filter(ImageFilter.SHARPEN)
11
  enhancer = ImageEnhance.Contrast(img)
12
  img = enhancer.enhance(2.0)
13
+ img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
14
+ return img_cv
15
 
 
 
 
 
 
16
  def process_image(img):
17
  if img is None:
18
  return "No image uploaded", None, None, "No OCR output"
19
 
20
  preprocessed_cv = preprocess_image(img)
 
 
21
  weight, confidence, raw_text = extract_weight_from_image(preprocessed_cv)
22
 
23
  ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
24
  return f"{weight} kg (Confidence: {confidence}%)", ist_time, img, raw_text
25
 
 
26
  with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
27
  gr.Markdown("## ⚖️ Auto Weight Logger")
28
  gr.Markdown("📷 **Upload or capture an image of a digital weight scale.** The app will auto-detect the weight in kilograms using OCR.")