Sanjayraju30 commited on
Commit
9b5d129
·
verified ·
1 Parent(s): d8a4c6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -10
app.py CHANGED
@@ -6,30 +6,32 @@ from datetime import datetime
6
  import re
7
  from PIL import Image
8
 
9
- ocr = PaddleOCR(use_angle_cls=True, lang='en') # Better for clean numbers
 
10
 
11
  def detect_weight(image):
12
  if image is None:
13
  return "No image uploaded", "N/A", None
14
 
15
- # Convert to OpenCV format
 
16
  image_np = np.array(image)
17
 
18
- # Preprocessing: Convert to grayscale, enhance contrast
19
  gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
20
- gray = cv2.equalizeHist(gray)
21
- gray = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB) # Convert back to 3 channel for PaddleOCR
22
 
23
  # Run OCR
24
- result = ocr.ocr(gray, cls=True)
25
 
26
  best_match = None
27
  best_conf = 0
28
 
 
29
  for line in result:
30
  for box in line:
31
- text = box[1][0]
32
- conf = box[1][1]
33
  match = re.search(r"\d+\.\d+", text)
34
  if match and conf > best_conf:
35
  best_match = match.group()
@@ -37,10 +39,11 @@ def detect_weight(image):
37
 
38
  if best_match:
39
  now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
40
- return f"Weight: {best_match} kg (Confidence: {round(best_conf*100, 2)}%)", now, image
41
  else:
42
  return "No weight detected kg (Confidence: 0.0%)", "N/A", image
43
 
 
44
  gr.Interface(
45
  fn=detect_weight,
46
  inputs=gr.Image(type="pil", label="Upload or Capture Weight Image"),
@@ -50,5 +53,5 @@ gr.Interface(
50
  gr.Image(label="Snapshot")
51
  ],
52
  title="Auto Weight Logger",
53
- description="Upload or capture a weight scale image. The app will detect and log the weight automatically."
54
  ).launch()
 
6
  import re
7
  from PIL import Image
8
 
9
+ # Initialize PaddleOCR (only once)
10
+ ocr = PaddleOCR(use_angle_cls=True, lang='en') # Use English OCR model
11
 
12
  def detect_weight(image):
13
  if image is None:
14
  return "No image uploaded", "N/A", None
15
 
16
+ # Convert PIL Image to OpenCV format (NumPy array)
17
+ image = image.convert("RGB")
18
  image_np = np.array(image)
19
 
20
+ # Preprocess: Grayscale + contrast enhancement (optional)
21
  gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
22
+ gray_eq = cv2.equalizeHist(gray)
23
+ processed_image = cv2.cvtColor(gray_eq, cv2.COLOR_GRAY2RGB) # convert back to RGB for PaddleOCR
24
 
25
  # Run OCR
26
+ result = ocr.ocr(processed_image, cls=True)
27
 
28
  best_match = None
29
  best_conf = 0
30
 
31
+ # Search for a decimal number like 25.52
32
  for line in result:
33
  for box in line:
34
+ text, conf = box[1]
 
35
  match = re.search(r"\d+\.\d+", text)
36
  if match and conf > best_conf:
37
  best_match = match.group()
 
39
 
40
  if best_match:
41
  now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
42
+ return f"Weight: {best_match} kg (Confidence: {round(best_conf * 100, 2)}%)", now, image
43
  else:
44
  return "No weight detected kg (Confidence: 0.0%)", "N/A", image
45
 
46
+ # Gradio UI
47
  gr.Interface(
48
  fn=detect_weight,
49
  inputs=gr.Image(type="pil", label="Upload or Capture Weight Image"),
 
53
  gr.Image(label="Snapshot")
54
  ],
55
  title="Auto Weight Logger",
56
+ description="Upload or capture a digital scale image. This app detects the weight automatically using AI."
57
  ).launch()