Rammohan0504 commited on
Commit
ae63d6b
·
verified ·
1 Parent(s): 8e6ee74

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -14
app.py CHANGED
@@ -3,24 +3,27 @@ from datetime import datetime
3
  from PIL import ImageFilter, ImageEnhance
4
  import pytz
5
  from ocr_engine import extract_weight_from_image
 
 
 
 
 
 
6
 
7
  def process_image(img):
8
  if img is None:
9
  return "No image uploaded", None, None, "No OCR output"
10
 
11
- # Enhance image before deblurring
12
- img = img.filter(ImageFilter.SHARPEN)
13
- enhancer = ImageEnhance.Contrast(img)
14
- img = enhancer.enhance(2.0)
15
-
16
- weight, confidence, raw_text, deblurred_img = extract_weight_from_image(img)
17
 
18
  ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
19
- return f"{weight} kg (Confidence: {confidence}%)", ist_time, deblurred_img, raw_text
20
 
21
  with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
22
  gr.Markdown("## ⚖️ Auto Weight Logger")
23
- gr.Markdown("📷 **Upload or capture an image of a digital weight scale.** This version uses Restormer to deblur before OCR.")
24
 
25
  with gr.Row():
26
  image_input = gr.Image(type="pil", label="Upload / Capture Image")
@@ -28,16 +31,14 @@ with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
28
 
29
  with gr.Row():
30
  timestamp = gr.Textbox(label="🕒 Captured At (IST)")
31
- snapshot = gr.Image(label="📸 Deblurred Snapshot")
32
 
33
  with gr.Row():
34
  debug_output = gr.Textbox(label="🪵 Raw OCR Output")
35
 
36
  submit = gr.Button("🔍 Detect Weight")
37
- submit.click(
38
- process_image,
39
- inputs=image_input,
40
- outputs=[output_weight, timestamp, snapshot, debug_output]
41
- )
42
 
43
  demo.launch()
 
3
  from PIL import ImageFilter, ImageEnhance
4
  import pytz
5
  from ocr_engine import extract_weight_from_image
6
+ from PIL import Image
7
+
8
+ def enhance_image(pil_img):
9
+ pil_img = pil_img.filter(ImageFilter.SHARPEN)
10
+ enhancer = ImageEnhance.Contrast(pil_img)
11
+ return enhancer.enhance(2.0)
12
 
13
  def process_image(img):
14
  if img is None:
15
  return "No image uploaded", None, None, "No OCR output"
16
 
17
+ enhanced = enhance_image(img)
18
+ weight, confidence, raw_text, processed_img_cv = extract_weight_from_image(enhanced)
19
+ processed_img = Image.fromarray(processed_img_cv)
 
 
 
20
 
21
  ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
22
+ return f"{weight} kg (Confidence: {confidence}%)", ist_time, processed_img, raw_text
23
 
24
  with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
25
  gr.Markdown("## ⚖️ Auto Weight Logger")
26
+ gr.Markdown("📷 **Upload or capture an image of a digital weight scale.** This app sharpens images and extracts weights using EasyOCR.")
27
 
28
  with gr.Row():
29
  image_input = gr.Image(type="pil", label="Upload / Capture Image")
 
31
 
32
  with gr.Row():
33
  timestamp = gr.Textbox(label="🕒 Captured At (IST)")
34
+ snapshot = gr.Image(label="🧪 Processed Snapshot")
35
 
36
  with gr.Row():
37
  debug_output = gr.Textbox(label="🪵 Raw OCR Output")
38
 
39
  submit = gr.Button("🔍 Detect Weight")
40
+ submit.click(process_image,
41
+ inputs=image_input,
42
+ outputs=[output_weight, timestamp, snapshot, debug_output])
 
 
43
 
44
  demo.launch()