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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -23
app.py CHANGED
@@ -6,53 +6,48 @@ import re
6
  from datetime import datetime
7
  import pytz
8
 
9
- # Load EasyOCR model (no Tesseract needed)
10
  reader = easyocr.Reader(['en'], gpu=False)
11
 
12
- # Enhance image before OCR
13
  def enhance_image(image):
14
  image = image.convert("L") # Grayscale
15
- image = ImageOps.invert(image) # Invert black ↔ white
16
- image = ImageEnhance.Contrast(image).enhance(2.0)
17
  image = ImageEnhance.Sharpness(image).enhance(2.5)
18
- image = image.resize((image.width * 2, image.height * 2)) # Enlarge
19
  return image
20
 
21
- # Main function to extract weight
22
  def detect_weight(image):
23
  try:
24
- # Enhance
25
  processed_image = enhance_image(image)
26
-
27
- # Convert to array
28
  np_image = np.array(processed_image)
29
 
30
- # OCR
31
  result = reader.readtext(np_image, detail=0)
32
- text = " ".join(result)
 
33
 
34
- # Extract decimal weight like 52.75
35
- match = re.search(r"\d{1,4}\.\d{1,4}", text)
36
  weight = match.group(0) if match else "Not detected"
37
 
38
- # IST Time
39
  ist = pytz.timezone("Asia/Kolkata")
40
- timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")
41
 
42
- return f"Weight: {weight} kg\nCaptured At: {timestamp} (IST)", image
43
  except Exception as e:
44
  return f"Error: {str(e)}", image
45
 
46
- # Gradio Interface
47
  interface = gr.Interface(
48
  fn=detect_weight,
49
  inputs=gr.Image(type="pil", label="Upload or Capture Image"),
50
- outputs=[
51
- gr.Textbox(label="Weight Info"),
52
- gr.Image(label="Snapshot")
53
- ],
54
- title="⚖️ Auto Weight Detector (Decimal Accurate)",
55
- description="Detects exact weight including decimals (e.g., 52.75 kg) using EasyOCR. Shows IST time."
56
  )
57
 
58
  interface.launch()
 
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()