geethareddy commited on
Commit
46a56d8
·
verified ·
1 Parent(s): 49bdc4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -5
app.py CHANGED
@@ -66,14 +66,14 @@ def preprocess_image(img_cv):
66
  try:
67
  # Convert to grayscale
68
  gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
69
- # Enhance contrast
70
  clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
71
  contrast = clahe.apply(gray)
72
  # Reduce noise with Gaussian blur
73
  blurred = cv2.GaussianBlur(contrast, (5, 5), 0)
74
- # Apply adaptive thresholding
75
  thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
76
- # Sharpen the image
77
  kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
78
  sharpened = cv2.filter2D(thresh, -1, kernel)
79
  return sharpened
@@ -97,7 +97,7 @@ def detect_roi(img_cv):
97
  # Find the largest contour (assuming it’s the display)
98
  largest_contour = max(contours, key=cv2.contourArea)
99
  x, y, w, h = cv2.boundingRect(largest_contour)
100
- # Add padding
101
  padding = 10
102
  x = max(0, x - padding)
103
  y = max(0, y - padding)
@@ -239,4 +239,4 @@ with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
239
  """)
240
 
241
  if __name__ == "__main__":
242
- demo.launch()
 
66
  try:
67
  # Convert to grayscale
68
  gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
69
+ # Enhance contrast with CLAHE (Contrast Limited Adaptive Histogram Equalization)
70
  clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
71
  contrast = clahe.apply(gray)
72
  # Reduce noise with Gaussian blur
73
  blurred = cv2.GaussianBlur(contrast, (5, 5), 0)
74
+ # Apply adaptive thresholding for better binary image representation
75
  thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
76
+ # Sharpen the image to bring out more details in the numbers
77
  kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
78
  sharpened = cv2.filter2D(thresh, -1, kernel)
79
  return sharpened
 
97
  # Find the largest contour (assuming it’s the display)
98
  largest_contour = max(contours, key=cv2.contourArea)
99
  x, y, w, h = cv2.boundingRect(largest_contour)
100
+ # Add padding to the detected region to ensure weight is fully captured
101
  padding = 10
102
  x = max(0, x - padding)
103
  y = max(0, y - padding)
 
239
  """)
240
 
241
  if __name__ == "__main__":
242
+ demo.launch()