Sanjayraju30 commited on
Commit
27ed571
·
verified ·
1 Parent(s): 2bcb746

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -37
app.py CHANGED
@@ -1,65 +1,51 @@
1
  import gradio as gr
2
  from PIL import Image, ImageEnhance, ImageOps
3
- import torch
4
- from transformers import TrOCRProcessor, VisionEncoderDecoderModel
5
  from datetime import datetime
6
  import pytz
7
- import re
8
 
9
- # Load model and processor
10
- processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
11
- model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1")
12
 
13
- # Preprocess image to enhance OCR accuracy
14
  def enhance_image(image):
15
- # Convert to grayscale
16
- image = image.convert("L")
17
- # Invert for better contrast
18
- image = ImageOps.invert(image)
19
- # Increase contrast and sharpness
20
- image = ImageEnhance.Contrast(image).enhance(2.5)
21
- image = ImageEnhance.Sharpness(image).enhance(3.0)
22
- # Resize (bigger = easier to read digits clearly)
23
- image = image.resize((image.width * 3, image.height * 3))
24
- # Convert back to RGB for model
25
- return image.convert("RGB")
26
-
27
- # Extract accurate decimal weight
28
  def detect_weight(image):
29
  try:
30
- # Enhance image
31
  processed_image = enhance_image(image)
32
 
33
- # OCR using Hugging Face
34
- pixel_values = processor(images=processed_image, return_tensors="pt").pixel_values
35
- # Use slightly longer decoding to improve accuracy
36
- generated_ids = model.generate(
37
- pixel_values,
38
- max_length=64,
39
- num_beams=4, # Beam search to improve precision
40
- early_stopping=True
41
- )
42
- generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
43
 
44
- # Extract full decimal weight like 52.75 or 18.89
45
- match = re.search(r"(\d{1,4}(?:\.\d{1,4})?)", generated_text)
46
  weight = match.group(1) if match else "Not detected"
47
 
48
- # Timestamp in IST
49
  ist = pytz.timezone('Asia/Kolkata')
50
  current_time = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")
51
 
52
  return f"Weight: {weight} kg\nCaptured At: {current_time} (IST)", image
 
53
  except Exception as e:
54
  return f"Error: {str(e)}", image
55
 
56
- # Gradio UI
57
  interface = gr.Interface(
58
  fn=detect_weight,
59
  inputs=gr.Image(type="pil", label="Upload or Capture Image"),
60
  outputs=[gr.Textbox(label="Weight Info"), gr.Image(label="Snapshot")],
61
- title="⚖️ Auto Weight Detector (Decimal Accurate)",
62
- description="Detects full weight including decimals (e.g., 52.75 kg) from digital scale image using Hugging Face OCR."
63
  )
64
 
65
  interface.launch()
 
1
  import gradio as gr
2
  from PIL import Image, ImageEnhance, ImageOps
3
+ import easyocr
4
+ import re
5
  from datetime import datetime
6
  import pytz
 
7
 
8
+ # Initialize EasyOCR reader
9
+ reader = easyocr.Reader(['en'])
 
10
 
11
+ # Preprocessing to enhance image for OCR
12
  def enhance_image(image):
13
+ image = image.convert("L") # Grayscale
14
+ image = ImageOps.invert(image) # Invert
15
+ image = ImageEnhance.Contrast(image).enhance(2.0)
16
+ image = ImageEnhance.Sharpness(image).enhance(2.5)
17
+ image = image.resize((image.width * 2, image.height * 2)) # Enlarge
18
+ return image
19
+
20
+ # Weight detection function
 
 
 
 
 
21
  def detect_weight(image):
22
  try:
 
23
  processed_image = enhance_image(image)
24
 
25
+ # OCR using EasyOCR
26
+ result = reader.readtext(np.array(processed_image), detail=0)
27
+ full_text = " ".join(result)
 
 
 
 
 
 
 
28
 
29
+ # Extract number with decimal point
30
+ match = re.search(r"(\d{1,4}\.\d{1,4})", full_text)
31
  weight = match.group(1) if match else "Not detected"
32
 
33
+ # Get IST time
34
  ist = pytz.timezone('Asia/Kolkata')
35
  current_time = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")
36
 
37
  return f"Weight: {weight} kg\nCaptured At: {current_time} (IST)", image
38
+
39
  except Exception as e:
40
  return f"Error: {str(e)}", image
41
 
42
+ # Gradio interface
43
  interface = gr.Interface(
44
  fn=detect_weight,
45
  inputs=gr.Image(type="pil", label="Upload or Capture Image"),
46
  outputs=[gr.Textbox(label="Weight Info"), gr.Image(label="Snapshot")],
47
+ title="⚖️ Accurate Auto Weight Detector",
48
+ description="Detects full decimal weight (e.g., 52.75 kg) using EasyOCR and shows timestamp (IST)"
49
  )
50
 
51
  interface.launch()