Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,58 @@
|
|
| 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 |
-
#
|
| 9 |
-
reader = easyocr.Reader(['en'])
|
| 10 |
|
| 11 |
-
#
|
| 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 |
-
#
|
| 21 |
def detect_weight(image):
|
| 22 |
try:
|
|
|
|
| 23 |
processed_image = enhance_image(image)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
full_text = " ".join(result)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
return f"Error: {str(e)}", image
|
| 41 |
|
| 42 |
-
# Gradio
|
| 43 |
interface = gr.Interface(
|
| 44 |
fn=detect_weight,
|
| 45 |
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
|
| 46 |
-
outputs=[
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
interface.launch()
|
|
|
|
| 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 model (no Tesseract needed)
|
| 10 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
| 11 |
|
| 12 |
+
# Enhance image before OCR
|
| 13 |
def enhance_image(image):
|
| 14 |
image = image.convert("L") # Grayscale
|
| 15 |
+
image = ImageOps.invert(image) # Invert black ↔ white
|
| 16 |
image = ImageEnhance.Contrast(image).enhance(2.0)
|
| 17 |
image = ImageEnhance.Sharpness(image).enhance(2.5)
|
| 18 |
image = image.resize((image.width * 2, image.height * 2)) # Enlarge
|
| 19 |
return image
|
| 20 |
|
| 21 |
+
# Main function to extract weight
|
| 22 |
def detect_weight(image):
|
| 23 |
try:
|
| 24 |
+
# Enhance
|
| 25 |
processed_image = enhance_image(image)
|
| 26 |
|
| 27 |
+
# Convert to array
|
| 28 |
+
np_image = np.array(processed_image)
|
|
|
|
| 29 |
|
| 30 |
+
# OCR
|
| 31 |
+
result = reader.readtext(np_image, detail=0)
|
| 32 |
+
text = " ".join(result)
|
| 33 |
|
| 34 |
+
# Extract decimal weight like 52.75
|
| 35 |
+
match = re.search(r"\d{1,4}\.\d{1,4}", text)
|
| 36 |
+
weight = match.group(0) if match else "Not detected"
|
| 37 |
|
| 38 |
+
# IST Time
|
| 39 |
+
ist = pytz.timezone("Asia/Kolkata")
|
| 40 |
+
timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")
|
| 41 |
|
| 42 |
+
return f"Weight: {weight} kg\nCaptured At: {timestamp} (IST)", image
|
| 43 |
except Exception as e:
|
| 44 |
return f"Error: {str(e)}", image
|
| 45 |
|
| 46 |
+
# Gradio Interface
|
| 47 |
interface = gr.Interface(
|
| 48 |
fn=detect_weight,
|
| 49 |
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
|
| 50 |
+
outputs=[
|
| 51 |
+
gr.Textbox(label="Weight Info"),
|
| 52 |
+
gr.Image(label="Snapshot")
|
| 53 |
+
],
|
| 54 |
+
title="⚖️ Auto Weight Detector (Decimal Accurate)",
|
| 55 |
+
description="Detects exact weight including decimals (e.g., 52.75 kg) using EasyOCR. Shows IST time."
|
| 56 |
)
|
| 57 |
|
| 58 |
interface.launch()
|