geethareddy commited on
Commit
71f6c9d
·
verified ·
1 Parent(s): 1ff656a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -25
app.py CHANGED
@@ -9,69 +9,69 @@ import pytz
9
  import numpy as np
10
  import logging
11
 
12
- # Set up logging for debugging and better visibility
13
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
14
 
15
- # Configure Tesseract path (ensure it's correctly set to your Tesseract installation)
16
  try:
17
- pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' # Change path if necessary
18
- pytesseract.get_tesseract_version() # Confirm Tesseract is properly set
19
  logging.info("Tesseract is configured properly.")
20
  except Exception as e:
21
  logging.error(f"Tesseract not found or misconfigured: {str(e)}")
22
 
23
- # Image Preprocessing to clean up the image for better OCR
24
  def preprocess_image(img_cv):
25
- """Preprocess the image to enhance clarity for OCR."""
26
  try:
27
- # Convert image to grayscale for easier processing
28
  gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
29
 
30
- # Enhance the image contrast using CLAHE (Contrast Limited Adaptive Histogram Equalization)
31
  clahe = cv2.createCLAHE(clipLimit=5.0, tileGridSize=(8, 8))
32
  contrast = clahe.apply(gray)
33
 
34
- # Apply Gaussian Blur to reduce noise
35
  blurred = cv2.GaussianBlur(contrast, (5, 5), 0)
36
 
37
- # Apply adaptive thresholding for better image clarity
38
  thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
39
 
40
- # Sharpen the image to emphasize digits
41
  sharpened = cv2.filter2D(thresh, -1, np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]))
42
  return sharpened
43
  except Exception as e:
44
  logging.error(f"Image preprocessing failed: {str(e)}")
45
  return img_cv
46
 
47
- # Function to extract weight from the image using Tesseract OCR
48
  def extract_weight(img):
49
- """Extract weight using Tesseract OCR, focusing on numeric digits."""
50
  try:
51
  if img is None:
52
  logging.error("No image provided for OCR")
53
  return "Not detected", 0.0, None
54
 
55
- # Convert the PIL image to OpenCV format for processing
56
  img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
57
 
58
- # Preprocess the image for better OCR results
59
  processed_img = preprocess_image(img_cv)
60
 
61
- # Tesseract configuration focusing on digits and decimals
62
  custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.'
63
 
64
- # Run OCR on the processed image
65
  text = pytesseract.image_to_string(processed_img, config=custom_config)
66
  logging.info(f"OCR result: '{text}'")
67
 
68
- # Extract only the numeric part (weight)
69
  weight = ''.join(filter(lambda x: x in '0123456789.', text.strip()))
70
  if weight:
71
  try:
72
  weight_float = float(weight)
73
  if weight_float >= 0: # Ensure it's a valid weight
74
- confidence = 95.0 # Set high confidence for valid weight
75
  logging.info(f"Weight detected: {weight} (Confidence: {confidence:.2f}%)")
76
  return weight, confidence, processed_img
77
  except ValueError:
@@ -83,25 +83,25 @@ def extract_weight(img):
83
  logging.error(f"OCR processing failed: {str(e)}")
84
  return "Not detected", 0.0, None
85
 
86
- # Main function to process the uploaded image and display results
87
  def process_image(img):
88
- """Process the uploaded image, extract weight, and display results."""
89
  if img is None:
90
  logging.error("No image uploaded")
91
  return "No image uploaded", None, gr.update(visible=False), gr.update(visible=False)
92
 
93
- # Get the current time in IST format
94
  ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
95
 
96
  # Extract weight and confidence
97
  weight, confidence, processed_img = extract_weight(img)
98
 
99
- # If weight detection failed, display an appropriate message
100
  if weight == "Not detected" or confidence < 95.0:
101
  logging.warning(f"Weight detection failed: {weight} (Confidence: {confidence:.2f}%)")
102
  return f"{weight} (Confidence: {confidence:.2f}%)", ist_time, gr.update(visible=True), gr.update(visible=False)
103
 
104
- # Convert processed image to base64 format for displaying in Gradio
105
  pil_image = Image.fromarray(processed_img)
106
  buffered = io.BytesIO()
107
  pil_image.save(buffered, format="PNG")
@@ -109,7 +109,7 @@ def process_image(img):
109
 
110
  return f"{weight} kg (Confidence: {confidence:.2f}%)", ist_time, img_base64, gr.update(visible=True)
111
 
112
- # Gradio interface setup
113
  with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
114
  gr.Markdown("## ⚖️ Auto Weight Logger")
115
  gr.Markdown("📷 Upload or capture an image of a digital weight scale (max 5MB).")
 
9
  import numpy as np
10
  import logging
11
 
12
+ # Set up logging for better debugging
13
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
14
 
15
+ # Ensure Tesseract is correctly set up
16
  try:
17
+ pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' # Make sure to set the correct path
18
+ pytesseract.get_tesseract_version() # Confirm Tesseract is installed
19
  logging.info("Tesseract is configured properly.")
20
  except Exception as e:
21
  logging.error(f"Tesseract not found or misconfigured: {str(e)}")
22
 
23
+ # Image Preprocessing to enhance OCR accuracy
24
  def preprocess_image(img_cv):
25
+ """Preprocess the image for better OCR accuracy."""
26
  try:
27
+ # Convert to grayscale
28
  gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
29
 
30
+ # Enhance contrast with CLAHE
31
  clahe = cv2.createCLAHE(clipLimit=5.0, tileGridSize=(8, 8))
32
  contrast = clahe.apply(gray)
33
 
34
+ # Apply Gaussian blur to reduce noise
35
  blurred = cv2.GaussianBlur(contrast, (5, 5), 0)
36
 
37
+ # Apply adaptive thresholding
38
  thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
39
 
40
+ # Sharpen the image to emphasize edges
41
  sharpened = cv2.filter2D(thresh, -1, np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]))
42
  return sharpened
43
  except Exception as e:
44
  logging.error(f"Image preprocessing failed: {str(e)}")
45
  return img_cv
46
 
47
+ # Function to extract weight using Tesseract OCR
48
  def extract_weight(img):
49
+ """Extract weight using Tesseract OCR, focused on digits and decimals."""
50
  try:
51
  if img is None:
52
  logging.error("No image provided for OCR")
53
  return "Not detected", 0.0, None
54
 
55
+ # Convert PIL image to OpenCV format
56
  img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
57
 
58
+ # Preprocess the image
59
  processed_img = preprocess_image(img_cv)
60
 
61
+ # Tesseract configuration to focus on digits and decimal points
62
  custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.'
63
 
64
+ # Run OCR
65
  text = pytesseract.image_to_string(processed_img, config=custom_config)
66
  logging.info(f"OCR result: '{text}'")
67
 
68
+ # Extract the valid weight (numbers and decimal points)
69
  weight = ''.join(filter(lambda x: x in '0123456789.', text.strip()))
70
  if weight:
71
  try:
72
  weight_float = float(weight)
73
  if weight_float >= 0: # Ensure it's a valid weight
74
+ confidence = 95.0 # High confidence for valid weight
75
  logging.info(f"Weight detected: {weight} (Confidence: {confidence:.2f}%)")
76
  return weight, confidence, processed_img
77
  except ValueError:
 
83
  logging.error(f"OCR processing failed: {str(e)}")
84
  return "Not detected", 0.0, None
85
 
86
+ # Main function to process the image and display results
87
  def process_image(img):
88
+ """Process the uploaded image and show results."""
89
  if img is None:
90
  logging.error("No image uploaded")
91
  return "No image uploaded", None, gr.update(visible=False), gr.update(visible=False)
92
 
93
+ # Get the current timestamp in IST format
94
  ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
95
 
96
  # Extract weight and confidence
97
  weight, confidence, processed_img = extract_weight(img)
98
 
99
+ # If weight detection fails, show the error message
100
  if weight == "Not detected" or confidence < 95.0:
101
  logging.warning(f"Weight detection failed: {weight} (Confidence: {confidence:.2f}%)")
102
  return f"{weight} (Confidence: {confidence:.2f}%)", ist_time, gr.update(visible=True), gr.update(visible=False)
103
 
104
+ # Convert processed image to base64 for Gradio to display
105
  pil_image = Image.fromarray(processed_img)
106
  buffered = io.BytesIO()
107
  pil_image.save(buffered, format="PNG")
 
109
 
110
  return f"{weight} kg (Confidence: {confidence:.2f}%)", ist_time, img_base64, gr.update(visible=True)
111
 
112
+ # Gradio Interface setup for Hugging Face
113
  with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
114
  gr.Markdown("## ⚖️ Auto Weight Logger")
115
  gr.Markdown("📷 Upload or capture an image of a digital weight scale (max 5MB).")