Sanjayraju30 commited on
Commit
4e7dfd4
·
verified ·
1 Parent(s): a9619bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -40
app.py CHANGED
@@ -1,49 +1,50 @@
1
  import gradio as gr
2
- from PIL import Image, ImageEnhance, ImageOps
3
- import numpy as np
4
  import easyocr
5
  import re
 
 
 
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()
 
 
 
1
  import gradio as gr
 
 
2
  import easyocr
3
  import re
4
+ import cv2
5
+ import numpy as np
6
+ from PIL import Image
7
  from datetime import datetime
 
8
 
 
9
  reader = easyocr.Reader(['en'], gpu=False)
10
 
 
 
 
 
 
 
 
 
 
 
11
  def detect_weight(image):
12
+ if image is None:
13
+ return "No image uploaded", "N/A", None
14
+
15
+ # Convert PIL to OpenCV
16
+ image_np = np.array(image)
17
+ gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
18
+
19
+ # Optional: Enhance contrast
20
+ gray = cv2.equalizeHist(gray)
21
+
22
+ # OCR
23
+ results = reader.readtext(gray, detail=1)
24
+
25
+ weight = None
26
+ max_conf = 0
27
+ for (bbox, text, conf) in results:
28
+ text = text.strip()
29
+ match = re.search(r'\d+\.\d+', text)
30
+ if match and conf > max_conf:
31
+ weight = match.group()
32
+ max_conf = conf
33
+
34
+ if weight:
35
+ now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
36
+ return f"Weight: {weight} kg (Confidence: {round(max_conf*100, 2)}%)", now, image
37
+ else:
38
+ return "No weight detected kg (Confidence: 0.0%)", "N/A", image
39
+
40
+ gr.Interface(
41
  fn=detect_weight,
42
+ inputs=gr.Image(type="pil", label="Upload or Capture Weight Image"),
43
+ outputs=[
44
+ gr.Text(label="Detected Weight"),
45
+ gr.Text(label="Captured At (IST)"),
46
+ gr.Image(label="Snapshot")
47
+ ],
48
+ title="Auto Weight Logger",
49
+ description="Upload or capture a weight scale image. The app will detect and log the weight automatically."
50
+ ).launch()