Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,69 +9,65 @@ import pytz
|
|
9 |
import numpy as np
|
10 |
import logging
|
11 |
|
12 |
-
# Set up logging for better
|
13 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
14 |
|
15 |
-
#
|
16 |
try:
|
17 |
-
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' #
|
18 |
-
pytesseract.get_tesseract_version() # Confirm Tesseract is
|
19 |
logging.info("Tesseract is configured properly.")
|
20 |
except Exception as e:
|
21 |
logging.error(f"Tesseract not found or misconfigured: {str(e)}")
|
22 |
|
23 |
-
# Image Preprocessing
|
24 |
def preprocess_image(img_cv):
|
25 |
-
"""
|
26 |
try:
|
27 |
-
# Convert to grayscale
|
28 |
gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
|
29 |
|
30 |
-
#
|
31 |
clahe = cv2.createCLAHE(clipLimit=5.0, tileGridSize=(8, 8))
|
32 |
contrast = clahe.apply(gray)
|
33 |
|
34 |
-
#
|
35 |
blurred = cv2.GaussianBlur(contrast, (5, 5), 0)
|
36 |
|
37 |
-
#
|
38 |
thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
|
39 |
|
40 |
-
#
|
41 |
sharpened = cv2.filter2D(thresh, -1, np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]))
|
|
|
42 |
return sharpened
|
43 |
except Exception as e:
|
44 |
logging.error(f"Image preprocessing failed: {str(e)}")
|
45 |
return img_cv
|
46 |
|
47 |
-
# Function to extract weight using
|
48 |
def extract_weight(img):
|
49 |
-
"""Extract weight using Tesseract OCR,
|
50 |
try:
|
51 |
if img is None:
|
52 |
logging.error("No image provided for OCR")
|
53 |
return "Not detected", 0.0, None
|
54 |
|
55 |
-
# Convert PIL image to OpenCV format
|
56 |
img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
57 |
-
|
58 |
-
# Preprocess the image
|
59 |
processed_img = preprocess_image(img_cv)
|
60 |
|
61 |
-
# Tesseract configuration to
|
62 |
custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.'
|
63 |
|
64 |
-
# Run OCR
|
65 |
text = pytesseract.image_to_string(processed_img, config=custom_config)
|
66 |
logging.info(f"OCR result: '{text}'")
|
67 |
|
68 |
-
# Extract the valid weight (numbers and decimal points)
|
69 |
weight = ''.join(filter(lambda x: x in '0123456789.', text.strip()))
|
|
|
70 |
if weight:
|
71 |
try:
|
72 |
weight_float = float(weight)
|
73 |
-
if weight_float >= 0:
|
74 |
-
confidence = 95.0 # High confidence
|
75 |
logging.info(f"Weight detected: {weight} (Confidence: {confidence:.2f}%)")
|
76 |
return weight, confidence, processed_img
|
77 |
except ValueError:
|
@@ -83,25 +79,24 @@ def extract_weight(img):
|
|
83 |
logging.error(f"OCR processing failed: {str(e)}")
|
84 |
return "Not detected", 0.0, None
|
85 |
|
86 |
-
# Main function to process
|
87 |
def process_image(img):
|
88 |
-
"""Process the uploaded image and
|
89 |
if img is None:
|
90 |
logging.error("No image uploaded")
|
91 |
return "No image uploaded", None, gr.update(visible=False), gr.update(visible=False)
|
92 |
|
93 |
-
# Get the
|
94 |
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
|
95 |
|
96 |
-
# Extract weight and confidence
|
97 |
weight, confidence, processed_img = extract_weight(img)
|
98 |
|
99 |
-
# If
|
100 |
if weight == "Not detected" or confidence < 95.0:
|
101 |
logging.warning(f"Weight detection failed: {weight} (Confidence: {confidence:.2f}%)")
|
102 |
return f"{weight} (Confidence: {confidence:.2f}%)", ist_time, gr.update(visible=True), gr.update(visible=False)
|
103 |
|
104 |
-
# Convert processed image to base64 for
|
105 |
pil_image = Image.fromarray(processed_img)
|
106 |
buffered = io.BytesIO()
|
107 |
pil_image.save(buffered, format="PNG")
|
@@ -109,7 +104,7 @@ def process_image(img):
|
|
109 |
|
110 |
return f"{weight} kg (Confidence: {confidence:.2f}%)", ist_time, img_base64, gr.update(visible=True)
|
111 |
|
112 |
-
# Gradio Interface
|
113 |
with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
|
114 |
gr.Markdown("## ⚖️ Auto Weight Logger")
|
115 |
gr.Markdown("📷 Upload or capture an image of a digital weight scale (max 5MB).")
|
|
|
9 |
import numpy as np
|
10 |
import logging
|
11 |
|
12 |
+
# Set up logging for better visibility
|
13 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
14 |
|
15 |
+
# Configure Tesseract path (ensure it’s correctly set to your Tesseract installation)
|
16 |
try:
|
17 |
+
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' # Change path if necessary
|
18 |
+
pytesseract.get_tesseract_version() # Confirm Tesseract is properly set
|
19 |
logging.info("Tesseract is configured properly.")
|
20 |
except Exception as e:
|
21 |
logging.error(f"Tesseract not found or misconfigured: {str(e)}")
|
22 |
|
23 |
+
# Image Preprocessing function
|
24 |
def preprocess_image(img_cv):
|
25 |
+
"""Enhance the image to improve OCR performance."""
|
26 |
try:
|
|
|
27 |
gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
|
28 |
|
29 |
+
# Contrast enhancement using CLAHE
|
30 |
clahe = cv2.createCLAHE(clipLimit=5.0, tileGridSize=(8, 8))
|
31 |
contrast = clahe.apply(gray)
|
32 |
|
33 |
+
# Applying Gaussian blur
|
34 |
blurred = cv2.GaussianBlur(contrast, (5, 5), 0)
|
35 |
|
36 |
+
# Adaptive thresholding
|
37 |
thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
|
38 |
|
39 |
+
# Sharpening the image
|
40 |
sharpened = cv2.filter2D(thresh, -1, np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]))
|
41 |
+
|
42 |
return sharpened
|
43 |
except Exception as e:
|
44 |
logging.error(f"Image preprocessing failed: {str(e)}")
|
45 |
return img_cv
|
46 |
|
47 |
+
# Function to extract weight using OCR
|
48 |
def extract_weight(img):
|
49 |
+
"""Extract weight using Tesseract OCR, focusing on digits and decimals."""
|
50 |
try:
|
51 |
if img is None:
|
52 |
logging.error("No image provided for OCR")
|
53 |
return "Not detected", 0.0, None
|
54 |
|
|
|
55 |
img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
|
|
|
|
56 |
processed_img = preprocess_image(img_cv)
|
57 |
|
58 |
+
# Tesseract configuration to extract digits and decimals
|
59 |
custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.'
|
60 |
|
|
|
61 |
text = pytesseract.image_to_string(processed_img, config=custom_config)
|
62 |
logging.info(f"OCR result: '{text}'")
|
63 |
|
|
|
64 |
weight = ''.join(filter(lambda x: x in '0123456789.', text.strip()))
|
65 |
+
|
66 |
if weight:
|
67 |
try:
|
68 |
weight_float = float(weight)
|
69 |
+
if weight_float >= 0:
|
70 |
+
confidence = 95.0 # High confidence
|
71 |
logging.info(f"Weight detected: {weight} (Confidence: {confidence:.2f}%)")
|
72 |
return weight, confidence, processed_img
|
73 |
except ValueError:
|
|
|
79 |
logging.error(f"OCR processing failed: {str(e)}")
|
80 |
return "Not detected", 0.0, None
|
81 |
|
82 |
+
# Main function to process uploaded image and display results
|
83 |
def process_image(img):
|
84 |
+
"""Process the uploaded image, extract weight, and display results."""
|
85 |
if img is None:
|
86 |
logging.error("No image uploaded")
|
87 |
return "No image uploaded", None, gr.update(visible=False), gr.update(visible=False)
|
88 |
|
89 |
+
# Get the timestamp for IST (Indian Standard Time)
|
90 |
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
|
91 |
|
|
|
92 |
weight, confidence, processed_img = extract_weight(img)
|
93 |
|
94 |
+
# If detection failed
|
95 |
if weight == "Not detected" or confidence < 95.0:
|
96 |
logging.warning(f"Weight detection failed: {weight} (Confidence: {confidence:.2f}%)")
|
97 |
return f"{weight} (Confidence: {confidence:.2f}%)", ist_time, gr.update(visible=True), gr.update(visible=False)
|
98 |
|
99 |
+
# Convert processed image to base64 for displaying in Gradio
|
100 |
pil_image = Image.fromarray(processed_img)
|
101 |
buffered = io.BytesIO()
|
102 |
pil_image.save(buffered, format="PNG")
|
|
|
104 |
|
105 |
return f"{weight} kg (Confidence: {confidence:.2f}%)", ist_time, img_base64, gr.update(visible=True)
|
106 |
|
107 |
+
# Gradio Interface Setup for Hugging Face
|
108 |
with gr.Blocks(title="⚖️ Auto Weight Logger") as demo:
|
109 |
gr.Markdown("## ⚖️ Auto Weight Logger")
|
110 |
gr.Markdown("📷 Upload or capture an image of a digital weight scale (max 5MB).")
|