Sanjayraju30 commited on
Commit
d9656e4
·
verified ·
1 Parent(s): 30ff189

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -61
app.py CHANGED
@@ -15,12 +15,7 @@ import os
15
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
16
 
17
  # Configure Tesseract path for Hugging Face
18
- try:
19
- pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
20
- pytesseract.get_tesseract_version() # Test Tesseract availability
21
- logging.info("Tesseract is available")
22
- except Exception as e:
23
- logging.error(f"Tesseract not found or misconfigured: {str(e)}")
24
 
25
  # Salesforce configuration (use environment variables in production)
26
  SF_USERNAME = os.getenv("SF_USERNAME", "your_salesforce_username")
@@ -61,58 +56,27 @@ def resize_image(img, max_size_mb=5):
61
  logging.error(f"Image resizing failed: {str(e)}")
62
  return img, None
63
 
64
- def preprocess_image(img_cv):
65
- """Preprocess image for OCR: convert to grayscale, reduce noise, adjust contrast, and apply adaptive thresholding."""
66
- try:
67
- # Convert to grayscale
68
- gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
69
- # Reduce noise with Gaussian blur
70
- blurred = cv2.GaussianBlur(gray, (5, 5), 0)
71
- # Adjust contrast
72
- clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
73
- contrast = clahe.apply(blurred)
74
- # Apply adaptive thresholding
75
- thresh = cv2.adaptiveThreshold(contrast, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
76
- return thresh
77
- except Exception as e:
78
- logging.error(f"Image preprocessing failed: {str(e)}")
79
- return img_cv
80
-
81
  def extract_weight(img):
82
- """Extract weight from image using Tesseract OCR with multiple PSM modes."""
83
  try:
84
- if img is None:
85
- logging.error("No image provided for OCR")
86
- return "Not detected", 0.0
87
-
88
  # Convert PIL image to OpenCV format
89
  img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
90
- # Preprocess image
91
- processed_img = preprocess_image(img_cv)
92
-
93
- # Try multiple PSM modes for better detection
94
- psm_modes = [
95
- ('--psm 7 digits', 'Single line, digits only'),
96
- ('--psm 6 digits', 'Single block, digits only'),
97
- ('--psm 10 digits', 'Single character, digits only')
98
- ]
99
-
100
- for config, desc in psm_modes:
101
- text = pyt validatingesseract.image_to_string(processed_img, config=config)
102
- logging.info(f"OCR attempt with {desc}: Raw text = '{text}'")
103
- weight = ''.join(filter(lambda x: x in '0123456789.', text.strip()))
104
- try:
105
- weight_float = float(weight)
106
- if weight_float > 0:
107
- confidence = 95.0 # Simplified confidence for valid numbers
108
- logging.info(f"Weight detected: {weight} (Confidence: {confidence:.2f}%)")
109
- return weight, confidence
110
- except ValueError:
111
- logging.warning(f"Invalid number format: {weight}")
112
- continue
113
-
114
- logging.error("All OCR attempts failed to detect a valid weight")
115
- return "Not detected", 0.0
116
  except Exception as e:
117
  logging.error(f"OCR processing failed: {str(e)}")
118
  return "Not detected", 0.0
@@ -120,24 +84,20 @@ def extract_weight(img):
120
  def process_image(img):
121
  """Process uploaded or captured image and extract weight."""
122
  if img is None:
123
- logging.error("No image provided")
124
  return "No image uploaded", None, None, None, gr.update(visible=False), gr.update(visible=False)
125
 
126
  ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
127
  img, img_bytes = resize_image(img)
128
  if img_bytes is None:
129
- logging.error("Image resizing failed")
130
  return "Image processing failed", ist_time, img, None, gr.update(visible=False), gr.update(visible=False)
131
 
132
  weight, confidence = extract_weight(img)
133
 
134
  if weight == "Not detected" or confidence < 95.0:
135
- logging.warning(f"Weight detection failed: {weight} (Confidence: {confidence:.2f}%)")
136
  return f"{weight} (Confidence: {confidence:.2f}%)", ist_time, img, None, gr.update(visible=True), gr.update(visible=False)
137
 
138
  img_buffer = io.BytesIO(img_bytes)
139
  img_base64 = base64.b64encode(img_buffer.getvalue()).decode()
140
- logging.info(f"Weight detected successfully: {weight} kg")
141
  return f"{weight} kg (Confidence: {confidence:.2f}%)", ist_time, img, img_base64, gr.update(visible=True), gr.update(visible=True)
142
 
143
  def save_to_salesforce(weight_text, img_base64):
@@ -145,7 +105,6 @@ def save_to_salesforce(weight_text, img_base64):
145
  try:
146
  sf = connect_to_salesforce()
147
  if sf is None:
148
- logging.error("Salesforce connection failed")
149
  return "Failed to connect to Salesforce"
150
 
151
  weight = float(weight_text.split(" ")[0])
@@ -196,11 +155,10 @@ with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
196
 
197
  gr.Markdown("""
198
  ### Instructions
199
- - Upload a clear, well-lit image of a digital weight scale display (7-segment font preferred).
200
  - Ensure the image is < 5MB (automatically resized if larger).
201
  - Review the detected weight and click 'Confirm and Save to Salesforce' to log the data.
202
  - Works on desktop and mobile browsers.
203
- - If weight detection fails, check the image for glare or low contrast and try again.
204
  """)
205
 
206
  if __name__ == "__main__":
 
15
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
16
 
17
  # Configure Tesseract path for Hugging Face
18
+ pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
 
 
 
 
 
19
 
20
  # Salesforce configuration (use environment variables in production)
21
  SF_USERNAME = os.getenv("SF_USERNAME", "your_salesforce_username")
 
56
  logging.error(f"Image resizing failed: {str(e)}")
57
  return img, None
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  def extract_weight(img):
60
+ """Extract weight from image using Tesseract OCR."""
61
  try:
 
 
 
 
62
  # Convert PIL image to OpenCV format
63
  img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
64
+ gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
65
+ # Preprocess image for better OCR accuracy
66
+ _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
67
+ # Configure Tesseract for 7-segment display (digits only, single line)
68
+ config = '--psm 7 digits'
69
+ text = pytesseract.image_to_string(thresh, config=config)
70
+ # Extract numeric values (digits and decimal point)
71
+ weight = ''.join(filter(lambda x: x in '0123456789.', text))
72
+ # Validate weight (ensure it’s a valid number)
73
+ try:
74
+ weight_float = float(weight)
75
+ # Simplified confidence: 95% if valid number, else 0%
76
+ confidence = 95.0 if weight_float > 0 else 0.0
77
+ return weight, confidence
78
+ except ValueError:
79
+ return "Not detected", 0.0
 
 
 
 
 
 
 
 
 
 
80
  except Exception as e:
81
  logging.error(f"OCR processing failed: {str(e)}")
82
  return "Not detected", 0.0
 
84
  def process_image(img):
85
  """Process uploaded or captured image and extract weight."""
86
  if img is None:
 
87
  return "No image uploaded", None, None, None, gr.update(visible=False), gr.update(visible=False)
88
 
89
  ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
90
  img, img_bytes = resize_image(img)
91
  if img_bytes is None:
 
92
  return "Image processing failed", ist_time, img, None, gr.update(visible=False), gr.update(visible=False)
93
 
94
  weight, confidence = extract_weight(img)
95
 
96
  if weight == "Not detected" or confidence < 95.0:
 
97
  return f"{weight} (Confidence: {confidence:.2f}%)", ist_time, img, None, gr.update(visible=True), gr.update(visible=False)
98
 
99
  img_buffer = io.BytesIO(img_bytes)
100
  img_base64 = base64.b64encode(img_buffer.getvalue()).decode()
 
101
  return f"{weight} kg (Confidence: {confidence:.2f}%)", ist_time, img, img_base64, gr.update(visible=True), gr.update(visible=True)
102
 
103
  def save_to_salesforce(weight_text, img_base64):
 
105
  try:
106
  sf = connect_to_salesforce()
107
  if sf is None:
 
108
  return "Failed to connect to Salesforce"
109
 
110
  weight = float(weight_text.split(" ")[0])
 
155
 
156
  gr.Markdown("""
157
  ### Instructions
158
+ - Upload a clear, well-lit image of a digital weight scale display.
159
  - Ensure the image is < 5MB (automatically resized if larger).
160
  - Review the detected weight and click 'Confirm and Save to Salesforce' to log the data.
161
  - Works on desktop and mobile browsers.
 
162
  """)
163
 
164
  if __name__ == "__main__":