Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,50 @@
|
|
| 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
|
| 10 |
reader = easyocr.Reader(['en'], gpu=False)
|
| 11 |
|
| 12 |
-
# Enhance image
|
| 13 |
-
def enhance_image(image):
|
| 14 |
-
image = image.convert("L")
|
| 15 |
-
image = ImageOps.invert(image)
|
| 16 |
-
image = ImageEnhance.Contrast(image).enhance(2.5)
|
| 17 |
-
image = ImageEnhance.Sharpness(image).enhance(2.5)
|
| 18 |
-
image = image.resize((image.width * 2, image.height * 2))
|
| 19 |
-
return image
|
| 20 |
-
|
| 21 |
-
# Main detection
|
| 22 |
def detect_weight(image):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
fn=detect_weight,
|
| 43 |
-
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
|
| 44 |
-
outputs=[
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import easyocr
|
| 3 |
import re
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
from datetime import datetime
|
|
|
|
| 8 |
|
|
|
|
| 9 |
reader = easyocr.Reader(['en'], gpu=False)
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def detect_weight(image):
|
| 12 |
+
if image is None:
|
| 13 |
+
return "No image uploaded", "N/A", None
|
| 14 |
+
|
| 15 |
+
# Convert PIL to OpenCV
|
| 16 |
+
image_np = np.array(image)
|
| 17 |
+
gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
|
| 18 |
+
|
| 19 |
+
# Optional: Enhance contrast
|
| 20 |
+
gray = cv2.equalizeHist(gray)
|
| 21 |
+
|
| 22 |
+
# OCR
|
| 23 |
+
results = reader.readtext(gray, detail=1)
|
| 24 |
+
|
| 25 |
+
weight = None
|
| 26 |
+
max_conf = 0
|
| 27 |
+
for (bbox, text, conf) in results:
|
| 28 |
+
text = text.strip()
|
| 29 |
+
match = re.search(r'\d+\.\d+', text)
|
| 30 |
+
if match and conf > max_conf:
|
| 31 |
+
weight = match.group()
|
| 32 |
+
max_conf = conf
|
| 33 |
+
|
| 34 |
+
if weight:
|
| 35 |
+
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 36 |
+
return f"Weight: {weight} kg (Confidence: {round(max_conf*100, 2)}%)", now, image
|
| 37 |
+
else:
|
| 38 |
+
return "No weight detected kg (Confidence: 0.0%)", "N/A", image
|
| 39 |
+
|
| 40 |
+
gr.Interface(
|
| 41 |
fn=detect_weight,
|
| 42 |
+
inputs=gr.Image(type="pil", label="Upload or Capture Weight Image"),
|
| 43 |
+
outputs=[
|
| 44 |
+
gr.Text(label="Detected Weight"),
|
| 45 |
+
gr.Text(label="Captured At (IST)"),
|
| 46 |
+
gr.Image(label="Snapshot")
|
| 47 |
+
],
|
| 48 |
+
title="Auto Weight Logger",
|
| 49 |
+
description="Upload or capture a weight scale image. The app will detect and log the weight automatically."
|
| 50 |
+
).launch()
|