Sanjayraju30 commited on
Commit
e6e3c3a
·
verified ·
1 Parent(s): 94a9b1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -16
app.py CHANGED
@@ -6,48 +6,44 @@ import re
6
  from datetime import datetime
7
  import pytz
8
 
9
- # Load EasyOCR model (CPU only)
10
  reader = easyocr.Reader(['en'], gpu=False)
11
 
12
- # Image enhancement
13
  def enhance_image(image):
14
- image = image.convert("L") # Grayscale
15
  image = ImageOps.invert(image)
16
  image = ImageEnhance.Contrast(image).enhance(2.5)
17
  image = ImageEnhance.Sharpness(image).enhance(2.5)
18
  image = image.resize((image.width * 2, image.height * 2))
19
  return image
20
 
21
- # Detect weight from image
22
  def detect_weight(image):
23
  try:
24
- processed_image = enhance_image(image)
25
- np_image = np.array(processed_image)
26
 
27
- # Perform OCR
28
- result = reader.readtext(np_image, detail=0)
29
  full_text = " ".join(result)
30
- print("OCR Output:", full_text) # DEBUGGING LINE
31
 
32
- # Match both integer and decimal
33
  match = re.search(r"\d{1,4}(?:\.\d{1,4})?", full_text)
34
  weight = match.group(0) if match else "Not detected"
35
 
36
- # Get IST time
37
  ist = pytz.timezone("Asia/Kolkata")
38
- current_time = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")
39
 
40
- return f"Weight: {weight} kg\nCaptured At: {current_time} (IST)\n\nRaw OCR: {full_text}", image
41
  except Exception as e:
42
  return f"Error: {str(e)}", image
43
 
44
- # Gradio UI
45
  interface = gr.Interface(
46
  fn=detect_weight,
47
  inputs=gr.Image(type="pil", label="Upload or Capture Image"),
48
  outputs=[gr.Textbox(label="Weight Info"), gr.Image(label="Snapshot")],
49
- title="⚖️ Accurate Auto Weight Detector (with Decimal)",
50
- description="Detects weight from digital scale image using EasyOCR (no Tesseract). Shows IST timestamp."
51
  )
52
 
53
  interface.launch()
 
6
  from datetime import datetime
7
  import pytz
8
 
9
+ # Load EasyOCR
10
  reader = easyocr.Reader(['en'], gpu=False)
11
 
12
+ # Enhance image
13
  def enhance_image(image):
14
+ image = image.convert("L")
15
  image = ImageOps.invert(image)
16
  image = ImageEnhance.Contrast(image).enhance(2.5)
17
  image = ImageEnhance.Sharpness(image).enhance(2.5)
18
  image = image.resize((image.width * 2, image.height * 2))
19
  return image
20
 
21
+ # Main detection
22
  def detect_weight(image):
23
  try:
24
+ processed = enhance_image(image)
25
+ np_img = np.array(processed)
26
 
27
+ result = reader.readtext(np_img, detail=0)
 
28
  full_text = " ".join(result)
 
29
 
 
30
  match = re.search(r"\d{1,4}(?:\.\d{1,4})?", full_text)
31
  weight = match.group(0) if match else "Not detected"
32
 
 
33
  ist = pytz.timezone("Asia/Kolkata")
34
+ now = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")
35
 
36
+ return f"Weight: {weight} kg\nCaptured At: {now} (IST)", image
37
  except Exception as e:
38
  return f"Error: {str(e)}", image
39
 
40
+ # UI
41
  interface = gr.Interface(
42
  fn=detect_weight,
43
  inputs=gr.Image(type="pil", label="Upload or Capture Image"),
44
  outputs=[gr.Textbox(label="Weight Info"), gr.Image(label="Snapshot")],
45
+ title="⚖️ Auto Weight Detector",
46
+ description="Detects weight from image (e.g., 52.75 kg) and shows IST time using EasyOCR (no Tesseract)."
47
  )
48
 
49
  interface.launch()