Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,48 +6,44 @@ import re
|
|
| 6 |
from datetime import datetime
|
| 7 |
import pytz
|
| 8 |
|
| 9 |
-
# Load EasyOCR
|
| 10 |
reader = easyocr.Reader(['en'], gpu=False)
|
| 11 |
|
| 12 |
-
#
|
| 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 |
-
#
|
| 22 |
def detect_weight(image):
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
result = reader.readtext(np_image, detail=0)
|
| 29 |
full_text = " ".join(result)
|
| 30 |
-
print("OCR Output:", full_text) # DEBUGGING LINE
|
| 31 |
|
| 32 |
-
# Match both integer and decimal
|
| 33 |
match = re.search(r"\d{1,4}(?:\.\d{1,4})?", full_text)
|
| 34 |
weight = match.group(0) if match else "Not detected"
|
| 35 |
|
| 36 |
-
# Get IST time
|
| 37 |
ist = pytz.timezone("Asia/Kolkata")
|
| 38 |
-
|
| 39 |
|
| 40 |
-
return f"Weight: {weight} kg\nCaptured At: {
|
| 41 |
except Exception as e:
|
| 42 |
return f"Error: {str(e)}", image
|
| 43 |
|
| 44 |
-
#
|
| 45 |
interface = gr.Interface(
|
| 46 |
fn=detect_weight,
|
| 47 |
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
|
| 48 |
outputs=[gr.Textbox(label="Weight Info"), gr.Image(label="Snapshot")],
|
| 49 |
-
title="⚖️
|
| 50 |
-
description="Detects weight from
|
| 51 |
)
|
| 52 |
|
| 53 |
interface.launch()
|
|
|
|
| 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 |
try:
|
| 24 |
+
processed = enhance_image(image)
|
| 25 |
+
np_img = np.array(processed)
|
| 26 |
|
| 27 |
+
result = reader.readtext(np_img, detail=0)
|
|
|
|
| 28 |
full_text = " ".join(result)
|
|
|
|
| 29 |
|
|
|
|
| 30 |
match = re.search(r"\d{1,4}(?:\.\d{1,4})?", full_text)
|
| 31 |
weight = match.group(0) if match else "Not detected"
|
| 32 |
|
|
|
|
| 33 |
ist = pytz.timezone("Asia/Kolkata")
|
| 34 |
+
now = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")
|
| 35 |
|
| 36 |
+
return f"Weight: {weight} kg\nCaptured At: {now} (IST)", image
|
| 37 |
except Exception as e:
|
| 38 |
return f"Error: {str(e)}", image
|
| 39 |
|
| 40 |
+
# UI
|
| 41 |
interface = gr.Interface(
|
| 42 |
fn=detect_weight,
|
| 43 |
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
|
| 44 |
outputs=[gr.Textbox(label="Weight Info"), gr.Image(label="Snapshot")],
|
| 45 |
+
title="⚖️ Auto Weight Detector",
|
| 46 |
+
description="Detects weight from image (e.g., 52.75 kg) and shows IST time using EasyOCR (no Tesseract)."
|
| 47 |
)
|
| 48 |
|
| 49 |
interface.launch()
|