Rammohan0504 commited on
Commit
1d82556
ยท
verified ยท
1 Parent(s): 0ba4671

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -2,30 +2,25 @@ import gradio as gr
2
  from datetime import datetime
3
  from PIL import ImageFilter, ImageEnhance
4
  import pytz
5
- import numpy as np
6
- import cv2
7
  from ocr_engine import extract_weight_from_image
8
 
9
- def preprocess_image(img):
10
- img = img.filter(ImageFilter.SHARPEN)
11
- enhancer = ImageEnhance.Contrast(img)
12
- img = enhancer.enhance(2.0)
13
- img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
14
- return img_cv
15
-
16
  def process_image(img):
17
  if img is None:
18
  return "No image uploaded", None, None, "No OCR output"
19
 
20
- preprocessed_cv = preprocess_image(img)
21
- weight, confidence, raw_text = extract_weight_from_image(preprocessed_cv)
 
 
 
 
22
 
23
  ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
24
- return f"{weight} kg (Confidence: {confidence}%)", ist_time, img, raw_text
25
 
26
  with gr.Blocks(title="โš–๏ธ Auto Weight Logger") as demo:
27
  gr.Markdown("## โš–๏ธ Auto Weight Logger")
28
- gr.Markdown("๐Ÿ“ท **Upload or capture an image of a digital weight scale.** The app will auto-detect the weight in kilograms using OCR.")
29
 
30
  with gr.Row():
31
  image_input = gr.Image(type="pil", label="Upload / Capture Image")
@@ -33,12 +28,16 @@ with gr.Blocks(title="โš–๏ธ Auto Weight Logger") as demo:
33
 
34
  with gr.Row():
35
  timestamp = gr.Textbox(label="๐Ÿ•’ Captured At (IST)")
36
- snapshot = gr.Image(label="๐Ÿ“ธ Snapshot Image")
37
 
38
  with gr.Row():
39
  debug_output = gr.Textbox(label="๐Ÿชต Raw OCR Output")
40
 
41
  submit = gr.Button("๐Ÿ” Detect Weight")
42
- submit.click(process_image, inputs=image_input, outputs=[output_weight, timestamp, snapshot, debug_output])
 
 
 
 
43
 
44
  demo.launch()
 
2
  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
 
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()