Sanjayraju30 commited on
Commit
1279f5b
·
verified ·
1 Parent(s): ddf0c54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -24
app.py CHANGED
@@ -1,37 +1,41 @@
1
  import gradio as gr
 
 
 
2
  from ocr_engine import extract_weight_from_image
3
- from utils import get_ist_time
4
 
5
- def process_image(image):
6
- try:
7
- if image is None:
8
- return "No image provided", "-", None, "-"
9
-
10
- weight, confidence = extract_weight_from_image(image)
11
- timestamp = get_ist_time()
12
 
13
- if not weight:
14
- return "No weight detected", timestamp, image, "-"
15
 
16
- sf_url = f"https://your-salesforce-site.com/log-weight?weight={weight}&time={timestamp}"
17
- return f"{weight} g (Confidence: {confidence}%)", timestamp, image, sf_url
18
 
19
- except Exception as e:
20
- return f"Error: {str(e)}", "-", None, "-"
 
 
 
 
 
21
 
22
- iface = gr.Interface(
 
 
 
23
  fn=process_image,
24
- inputs=gr.Image(type="pil", label="Upload or Capture Snapshot"),
25
  outputs=[
26
- gr.Textbox(label="Detected Weight"),
27
- gr.Textbox(label="Captured At (IST)"),
28
- gr.Image(label="Snapshot Image"),
29
- gr.Textbox(label="Salesforce Log URL"),
30
  ],
31
- title="⚖️ Auto Weight Logger",
32
- description="Upload or capture a digital scale image to extract the weight.",
33
- live=True,
34
  )
35
 
36
  if __name__ == "__main__":
37
- iface.launch()
 
1
  import gradio as gr
2
+ from PIL import Image
3
+ from datetime import datetime
4
+ import pytz
5
  from ocr_engine import extract_weight_from_image
 
6
 
7
+ def process_image(img):
8
+ # Convert image to RGB in case it's RGBA or grayscale
9
+ img = img.convert("RGB")
 
 
 
 
10
 
11
+ # Get IST timestamp
12
+ ist = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S")
13
 
14
+ # Run OCR
15
+ weight, confidence = extract_weight_from_image(img)
16
 
17
+ # Format output
18
+ result = f"📅 Captured At (IST): {ist}\n"
19
+ if confidence > 0:
20
+ result += f"⚖️ Detected Weight: **{weight}**\n"
21
+ result += f"✅ Confidence: `{confidence:.2f}`"
22
+ else:
23
+ result += "❌ No weight detected. Try a clearer image."
24
 
25
+ return img, result
26
+
27
+ # Gradio UI
28
+ demo = gr.Interface(
29
  fn=process_image,
30
+ inputs=gr.Image(type="pil", label="Upload or Capture Image"),
31
  outputs=[
32
+ gr.Image(type="pil", label="Snapshot"),
33
+ gr.Markdown(label="Weight Result")
 
 
34
  ],
35
+ title="⚖️ Auto Weight Logger (PaddleOCR)",
36
+ description="Upload or capture an image of a weighing scale. This app uses PaddleOCR to detect the weight value.",
37
+ allow_flagging="never"
38
  )
39
 
40
  if __name__ == "__main__":
41
+ demo.launch()