Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,28 +4,33 @@ 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 |
-
#
|
| 11 |
img = img.filter(ImageFilter.SHARPEN)
|
| 12 |
enhancer = ImageEnhance.Contrast(img)
|
| 13 |
img = enhancer.enhance(2.0)
|
| 14 |
|
| 15 |
-
# Convert to OpenCV
|
| 16 |
-
|
| 17 |
-
return
|
| 18 |
|
|
|
|
| 19 |
def process_image(img):
|
| 20 |
if img is None:
|
| 21 |
return "No image uploaded", None, None, "No OCR output"
|
| 22 |
|
| 23 |
preprocessed_cv = preprocess_image(img)
|
|
|
|
|
|
|
| 24 |
weight, confidence, raw_text = extract_weight_from_image(preprocessed_cv)
|
| 25 |
|
| 26 |
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
|
| 27 |
return f"{weight} kg (Confidence: {confidence}%)", ist_time, img, raw_text
|
| 28 |
|
|
|
|
| 29 |
with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
|
| 30 |
gr.Markdown("## ⚖️ Auto Weight Logger")
|
| 31 |
gr.Markdown("📷 **Upload or capture an image of a digital weight scale.** The app will auto-detect the weight in kilograms using OCR.")
|
|
|
|
| 4 |
import pytz
|
| 5 |
import numpy as np
|
| 6 |
import cv2
|
| 7 |
+
from ocr_engine import extract_weight_from_image # Modify if needed
|
| 8 |
|
| 9 |
+
# Preprocess image to improve OCR accuracy
|
| 10 |
def preprocess_image(img):
|
| 11 |
+
# PIL enhancements
|
| 12 |
img = img.filter(ImageFilter.SHARPEN)
|
| 13 |
enhancer = ImageEnhance.Contrast(img)
|
| 14 |
img = enhancer.enhance(2.0)
|
| 15 |
|
| 16 |
+
# Convert to OpenCV format
|
| 17 |
+
cv_img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
| 18 |
+
return cv_img
|
| 19 |
|
| 20 |
+
# Process image and extract weight using OCR
|
| 21 |
def process_image(img):
|
| 22 |
if img is None:
|
| 23 |
return "No image uploaded", None, None, "No OCR output"
|
| 24 |
|
| 25 |
preprocessed_cv = preprocess_image(img)
|
| 26 |
+
|
| 27 |
+
# Extract data from your OCR engine (should handle OpenCV input)
|
| 28 |
weight, confidence, raw_text = extract_weight_from_image(preprocessed_cv)
|
| 29 |
|
| 30 |
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
|
| 31 |
return f"{weight} kg (Confidence: {confidence}%)", ist_time, img, raw_text
|
| 32 |
|
| 33 |
+
# Gradio UI setup
|
| 34 |
with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
|
| 35 |
gr.Markdown("## ⚖️ Auto Weight Logger")
|
| 36 |
gr.Markdown("📷 **Upload or capture an image of a digital weight scale.** The app will auto-detect the weight in kilograms using OCR.")
|