Sanjayraju30 commited on
Commit
57ffefc
·
verified ·
1 Parent(s): 0263481

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -10
app.py CHANGED
@@ -3,28 +3,27 @@ import cv2
3
  import numpy as np
4
  from paddleocr import PaddleOCR
5
  from datetime import datetime
 
6
  import re
7
  from PIL import Image
8
 
9
- # Initialize PaddleOCR once
10
- ocr = PaddleOCR(use_angle_cls=True, lang='en') # English OCR model
11
 
12
  def detect_weight(image):
13
  try:
14
  if image is None:
15
  return "No image uploaded", "N/A", None
16
 
17
- # Convert PIL image to RGB numpy array
18
  image = image.convert("RGB")
19
  image_np = np.array(image)
20
 
21
- # Convert to grayscale and enhance contrast
22
  gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
23
  enhanced = cv2.equalizeHist(gray)
24
  rgb_image = cv2.cvtColor(enhanced, cv2.COLOR_GRAY2RGB)
25
 
26
- # Perform OCR
27
- result = ocr.ocr(rgb_image, cls=True)
28
 
29
  best_match = None
30
  best_conf = 0
@@ -38,16 +37,14 @@ def detect_weight(image):
38
  best_conf = conf
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
  except Exception as e:
47
- # Show error in output instead of crashing
48
  return f"Error: {str(e)}", "Error", None
49
 
50
- # Gradio UI
51
  gr.Interface(
52
  fn=detect_weight,
53
  inputs=gr.Image(type="pil", label="Upload or Capture Weight Image"),
 
3
  import numpy as np
4
  from paddleocr import PaddleOCR
5
  from datetime import datetime
6
+ from pytz import timezone
7
  import re
8
  from PIL import Image
9
 
10
+ # Initialize PaddleOCR
11
+ ocr = PaddleOCR(use_angle_cls=True, lang='en')
12
 
13
  def detect_weight(image):
14
  try:
15
  if image is None:
16
  return "No image uploaded", "N/A", None
17
 
 
18
  image = image.convert("RGB")
19
  image_np = np.array(image)
20
 
 
21
  gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
22
  enhanced = cv2.equalizeHist(gray)
23
  rgb_image = cv2.cvtColor(enhanced, cv2.COLOR_GRAY2RGB)
24
 
25
+ # OCR (without cls= argument for compatibility)
26
+ result = ocr.ocr(rgb_image)
27
 
28
  best_match = None
29
  best_conf = 0
 
37
  best_conf = conf
38
 
39
  if best_match:
40
+ now_ist = datetime.now(timezone('Asia/Kolkata')).strftime("%Y-%m-%d %H:%M:%S IST")
41
+ return f"Weight: {best_match} kg (Confidence: {round(best_conf * 100, 2)}%)", now_ist, image
42
  else:
43
  return "No weight detected kg (Confidence: 0.0%)", "N/A", image
44
 
45
  except Exception as e:
 
46
  return f"Error: {str(e)}", "Error", None
47
 
 
48
  gr.Interface(
49
  fn=detect_weight,
50
  inputs=gr.Image(type="pil", label="Upload or Capture Weight Image"),